-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: RN provider and hooks attempt 2 (#321)
This is a second attempt of #306 using the newly added event source. - use StreamingProcessor and event source to fetch flags - added LDProvider - added hooks - added typed variation[Detail] functions Please read the [example README](https://github.com/launchdarkly/js-core/blob/9d458f28625c12169df614c8d2289f9c0952cc45/packages/sdk/react-native/example/README.md#L1) and try running the example app. --------- Co-authored-by: LaunchDarklyReleaseBot <[email protected]> Co-authored-by: Ryan Lamb <[email protected]>
- Loading branch information
1 parent
aaaf6d1
commit a343c83
Showing
36 changed files
with
997 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,6 @@ yarn-error.* | |
|
||
# typescript | ||
*.tsbuildinfo | ||
|
||
ios | ||
android |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,18 @@ | ||
import { CLIENT_SIDE_SDK_KEY } from '@env'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { StyleSheet, Text, View } from 'react-native'; | ||
import { MOBILE_KEY } from '@env'; | ||
|
||
import { init, type LDClientImpl } from '@launchdarkly/react-native-client-sdk'; | ||
import { LDProvider, ReactNativeLDClient } from '@launchdarkly/react-native-client-sdk'; | ||
|
||
const context = { kind: 'user', key: 'test-user-1' }; | ||
|
||
export default function App() { | ||
const [ldc, setLdc] = useState<LDClientImpl>(); | ||
const [flag, setFlag] = useState<boolean>(false); | ||
import Welcome from './src/welcome'; | ||
|
||
useEffect(() => { | ||
init(CLIENT_SIDE_SDK_KEY, context) | ||
.then((c) => { | ||
setLdc(c); | ||
}) | ||
.catch((e) => console.log(e)); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const f = ldc?.boolVariation('dev-test-flag', false); | ||
setFlag(f ?? false); | ||
}, [ldc]); | ||
const featureClient = new ReactNativeLDClient(MOBILE_KEY); | ||
const context = { kind: 'user', key: 'test-user-1' }; | ||
|
||
const App = () => { | ||
return ( | ||
<View style={styles.container}> | ||
<Text>{flag ? <>devTestFlag: {`${flag}`}</> : <>loading...</>}</Text> | ||
</View> | ||
<LDProvider client={featureClient} context={context}> | ||
<Welcome /> | ||
</LDProvider> | ||
); | ||
} | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
box: { | ||
width: 60, | ||
height: 60, | ||
marginVertical: 20, | ||
}, | ||
}); | ||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Button, StyleSheet, Text, View } from 'react-native'; | ||
|
||
import { | ||
useBoolVariation, | ||
useLDClient, | ||
useLDDataSourceStatus, | ||
} from '@launchdarkly/react-native-client-sdk'; | ||
|
||
export default function Welcome() { | ||
const { error, status } = useLDDataSourceStatus(); | ||
const flag = useBoolVariation('dev-test-flag', false); | ||
const ldc = useLDClient(); | ||
|
||
const login = () => { | ||
ldc.identify({ kind: 'user', key: 'test-user-2' }); | ||
}; | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<Text>Welcome to LaunchDarkly</Text> | ||
<Text>status: {status ?? 'not connected'}</Text> | ||
{error ? <Text>error: {error.message}</Text> : null} | ||
<Text>devTestFlag: {`${flag}`}</Text> | ||
<Text>context: {JSON.stringify(ldc.getContext())}</Text> | ||
<Button title="Login" onPress={login} /> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}, | ||
box: { | ||
width: 60, | ||
height: 60, | ||
marginVertical: 20, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
declare module '@env' { | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const CLIENT_SIDE_SDK_KEY: string; | ||
export const MOBILE_KEY: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { type LDContext } from '@launchdarkly/js-client-sdk-common'; | ||
|
||
import ReactNativeLDClient from './ReactNativeLDClient'; | ||
|
||
describe('ReactNativeLDClient', () => { | ||
let ldc: ReactNativeLDClient; | ||
|
||
beforeEach(() => { | ||
ldc = new ReactNativeLDClient('mob-test', { sendEvents: false }); | ||
}); | ||
|
||
test('constructor', () => { | ||
expect(ldc.sdkKey).toEqual('mob-test'); | ||
}); | ||
|
||
test('createStreamUriPath', () => { | ||
const context: LDContext = { kind: 'user', key: 'test-user-key-1' }; | ||
|
||
expect(ldc.createStreamUriPath(context)).toEqual( | ||
'/meval/eyJraW5kIjoidXNlciIsImtleSI6InRlc3QtdXNlci1rZXktMSJ9', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
base64UrlEncode, | ||
LDClientImpl, | ||
type LDContext, | ||
type LDOptions, | ||
} from '@launchdarkly/js-client-sdk-common'; | ||
|
||
import platform from './platform'; | ||
|
||
export default class ReactNativeLDClient extends LDClientImpl { | ||
constructor(sdkKey: string, options: LDOptions = {}) { | ||
super(sdkKey, platform, options); | ||
} | ||
|
||
override createStreamUriPath(context: LDContext) { | ||
return `/meval/${base64UrlEncode(JSON.stringify(context), platform.encoding!)}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import useLDClient from './useLDClient'; | ||
import useLDDataSourceStatus from './useLDDataSourceStatus'; | ||
|
||
export * from './variation'; | ||
|
||
export { useLDDataSourceStatus, useLDClient }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { context, ReactContext } from '../provider/reactContext'; | ||
|
||
const useLDClient = () => { | ||
const { client } = useContext<ReactContext>(context); | ||
return client; | ||
}; | ||
|
||
export default useLDClient; |
10 changes: 10 additions & 0 deletions
10
packages/sdk/react-native/src/hooks/useLDDataSourceStatus.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { context, ReactContext } from '../provider/reactContext'; | ||
|
||
const useLDDataSourceStatus = () => { | ||
const { dataSource } = useContext<ReactContext>(context); | ||
return dataSource; | ||
}; | ||
|
||
export default useLDDataSourceStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './useVariation'; | ||
export * from './useTypedVariation'; |
Oops, something went wrong.