Skip to content

Commit

Permalink
feat: add react component
Browse files Browse the repository at this point in the history
  • Loading branch information
doomsower committed Sep 11, 2019
1 parent dcb8705 commit 35d0ded
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 39 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,25 @@ Otherwise, run

## Usage

```javascript
Render startup time badge somewhere on your first screen:

```jsx
import { StartupTime } from 'react-native-startup-time';
...

<StartupTime
ready={true /* optional, defaults to true */}
style={styles.startupTime /* optional*/}
/>

```

Or use imperative call:

```jsx
import { getTimeSinceStartup } from 'react-native-startup-time';

// somewhere where you app is ready:
// when you app is ready:
getTimeSinceStartup().then((time) => {
console.log(`Time since startup: ${time} ms`);
});
Expand Down
131 changes: 103 additions & 28 deletions example/App.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,112 @@
import React from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import { getTimeSinceStartup } from 'react-native-startup-time';
import React, { Fragment } from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
View,
} from 'react-native';

const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
alignItems: 'center',
justifyContent: 'center',
},
label: {
marginTop: 16,
},
});
import {
Colors,
DebugInstructions,
Header,
LearnMoreLinks,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import { StartupTime } from 'react-native-startup-time';

const App = () => {
const [label, setLabel] = React.useState('');
const refreshLabel = React.useCallback(() => {
getTimeSinceStartup().then((time) => {
console.log(`Time since startup: ${time} ms`);
setLabel(`Time since startup: ${time} ms`);
});
}, []);
React.useEffect(() => {
refreshLabel();
}, [refreshLabel]);
return (
<View style={styles.container}>
<Button title="Refresh" onPress={refreshLabel} />
<Text style={styles.label}>{label}</Text>
</View>
<Fragment>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}
>
<Header />
{global.HermesInternal == null ? null : (
<View style={styles.engine}>
<Text style={styles.footer}>Engine: Hermes</Text>
</View>
)}
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Step One</Text>
<Text style={styles.sectionDescription}>
Edit <Text style={styles.highlight}>App.js</Text> to change this
screen and then come back to see your edits.
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>See Your Changes</Text>
<Text style={styles.sectionDescription}>
<ReloadInstructions />
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Debug</Text>
<Text style={styles.sectionDescription}>
<DebugInstructions />
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Learn More</Text>
<Text style={styles.sectionDescription}>
Read the docs to discover what to do next:
</Text>
</View>
<LearnMoreLinks />
</View>
</ScrollView>
<StartupTime ready={true} style={styles.startupTime} />
</SafeAreaView>
</Fragment>
);
};

App.displayName = 'App';
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
footer: {
color: Colors.dark,
fontSize: 12,
fontWeight: '600',
padding: 4,
paddingRight: 12,
textAlign: 'right',
},
startupTime: {
top: 24,
},
});

export default App;
14 changes: 7 additions & 7 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3047,9 +3047,9 @@ minimist@^1.1.1, minimist@^1.2.0:
integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=

minipass@^2.2.1, minipass@^2.3.5:
version "2.5.0"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.5.0.tgz#dddb1d001976978158a05badfcbef4a771612857"
integrity sha512-9FwMVYhn6ERvMR8XFdOavRz4QK/VJV8elU1x50vYexf9lslDcWe/f4HBRxCPd185ekRSjU6CfYyJCECa/CQy7Q==
version "2.5.1"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.5.1.tgz#cf435a9bf9408796ca3a3525a8b851464279c9b8"
integrity sha512-dmpSnLJtNQioZFI5HfQ55Ad0DzzsMAb+HfokwRTNXwEQjepbTkl5mtIlSVxGIkOkxlpX7wIn5ET/oAd9fZ/Y/Q==
dependencies:
safe-buffer "^5.1.2"
yallist "^3.0.0"
Expand Down Expand Up @@ -3644,7 +3644,7 @@ react-is@^16.8.1, react-is@^16.8.4:
integrity sha512-tJBzzzIgnnRfEm046qRcURvwQnZVXmuCbscxUO5RWrGTXpon2d4c8mI0D8WE6ydVIm29JiLB6+RslkIvym9Rjw==

"react-native-startup-time@file:..":
version "1.0.0-alpha1"
version "1.0.0"

[email protected]:
version "0.60.5"
Expand Down Expand Up @@ -3689,9 +3689,9 @@ react-proxy@^1.1.7:
react-deep-force-update "^1.0.0"

react-refresh@^0.4.0:
version "0.4.1"
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.4.1.tgz#2b1e4b1fe932de30c32aec25a0ac777e60bc156e"
integrity sha512-My+vtJ3hCT2sOog3g0h9a9J96IaHkIAcencxhe7HtMVQYpNNyJnr+bE9bjcwfwTBi13tkb7oDDCbRtfvK7iiKQ==
version "0.4.2"
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.4.2.tgz#54a277a6caaac2803d88f1d6f13c1dcfbd81e334"
integrity sha512-kv5QlFFSZWo7OlJFNYbxRtY66JImuP2LcrFgyJfQaf85gSP+byzG21UbDQEYjU7f//ny8rwiEkO6py2Y+fEgAQ==

react-transform-hmr@^1.0.4:
version "1.0.4"
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
import * as React from 'react';
import { StyleProp, ViewStyle } from 'react-native';

export declare const getTimeSinceStartup: () => Promise<number>;
export declare const StartupTime: React.FC<{
style?: StyleProp<ViewStyle>;
ready?: boolean;
}>;
31 changes: 30 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import { NativeModules } from 'react-native';
import React from 'react';
import { NativeModules, StyleSheet, Text, View } from 'react-native';

const { RNStartupTime } = NativeModules;

const styles = StyleSheet.create({
container: {
position: 'absolute',
backgroundColor: '#FFF',
top: 8,
right: 8,
padding: 8,
},
});

export const getTimeSinceStartup = () => RNStartupTime.getTimeSinceStartup();

export const StartupTime = ({ style, ready = true }) => {
const [time, setTime] = React.useState('');
const [layoutComplete, setLayoutComplete] = React.useState('');
const onLayout = React.useCallback(() => setLayoutComplete(true), [
setLayoutComplete,
]);
React.useEffect(() => {
if (ready && layoutComplete) {
getTimeSinceStartup().then((t) => setTime(t));
}
}, [ready, layoutComplete, setTime]);
return (
<View style={[styles.container, style]} onLayout={onLayout}>
<Text>{`startup time: ${time} ms`}</Text>
</View>
);
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
"@react-native-community/eslint-config": "^0.0.5",
"@semantic-release/changelog": "^3.0.4",
"@semantic-release/git": "^7.0.16",
"@types/react": "^16.9.2",
"@types/react-native": "^0.60.13",
"commitlint": "^8.1.0",
"eslint": "^6.3.0",
"eslint-plugin-prettier": "^3.1.0",
Expand All @@ -51,6 +53,7 @@
"pretty-quick": "^1.11.1",
"react": "^16.8.3",
"react-native": "^0.59.10",
"semantic-release": "^15.13.24"
"semantic-release": "^15.13.24",
"typescript": "^3.6.3"
}
}
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,27 @@
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA==

"@types/prop-types@*":
version "15.7.2"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.2.tgz#0e58ae66773d7fd7c372a493aff740878ec9ceaa"
integrity sha512-f8JzJNWVhKtc9dg/dyDNfliTKNOJSLa7Oht/ElZdF/UbMUmAH3rLmAk3ODNjw0mZajDEgatA03tRjB4+Dp/tzA==

"@types/react-native@^0.60.13":
version "0.60.13"
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.60.13.tgz#381703a787db0f0d49895f3a17b50474f4024551"
integrity sha512-iEydUatGCGE6s5DSUcqnBGjruxor+XUb+52FTjKXvUDaisXoFZHei4yvAMAVrI7LMvVU0DQUlmHUAkoveO/fNQ==
dependencies:
"@types/prop-types" "*"
"@types/react" "*"

"@types/react@*", "@types/react@^16.9.2":
version "16.9.2"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.2.tgz#6d1765431a1ad1877979013906731aae373de268"
integrity sha512-jYP2LWwlh+FTqGd9v7ynUKZzjj98T8x7Yclz479QdRhHfuW9yQ+0jjnD31eXSXutmBpppj5PYNLYLRfnZJvcfg==
dependencies:
"@types/prop-types" "*"
csstype "^2.2.0"

"@types/retry@^0.12.0":
version "0.12.0"
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
Expand Down Expand Up @@ -2336,6 +2357,11 @@ crypto-random-string@^1.0.0:
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=

csstype@^2.2.0:
version "2.6.6"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.6.tgz#c34f8226a94bbb10c32cc0d714afdf942291fc41"
integrity sha512-RpFbQGUE74iyPgvr46U9t1xoQBM8T4BL8SxrN66Le2xYAPSaDJJKeztV3awugusb3g3G9iL8StmkBBXhcbbXhg==

currently-unhandled@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
Expand Down Expand Up @@ -8264,6 +8290,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^3.6.3:
version "3.6.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.3.tgz#fea942fabb20f7e1ca7164ff626f1a9f3f70b4da"
integrity sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw==

ua-parser-js@^0.7.18:
version "0.7.20"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.20.tgz#7527178b82f6a62a0f243d1f94fd30e3e3c21098"
Expand Down

0 comments on commit 35d0ded

Please sign in to comment.