-
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.
feat: custom storage option for React Native SDK (#539)
This PR fixes #436 by implementing a custom storage option to the React Native package. The main motivation is to get rid of the obsolete async storage package, but we also found that having multiple clients as [recommended by the official docs](https://docs.launchdarkly.com/sdk/features/multiple-environments#react-native) when migrating away from `secondaryMobileKeys` in v9 caused them to overwrite each other's storage and therefore effectively disabling the storage/cache altogether. --------- Co-authored-by: Ryan Lamb <[email protected]>
- Loading branch information
1 parent
e15c7e9
commit 115bd82
Showing
7 changed files
with
147 additions
and
9 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
31 changes: 31 additions & 0 deletions
31
packages/sdk/react-native/src/ReactNativeLDClient.storage.test.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,31 @@ | ||
import { AutoEnvAttributes, LDLogger } from '@launchdarkly/js-client-sdk-common'; | ||
|
||
import ReactNativeLDClient from './ReactNativeLDClient'; | ||
|
||
it('uses custom storage', async () => { | ||
// This test just validates that the custom storage instance is being called. | ||
// Other tests validate how the SDK interacts with storage generally. | ||
const logger: LDLogger = { | ||
error: jest.fn(), | ||
warn: jest.fn(), | ||
info: jest.fn(), | ||
debug: jest.fn(), | ||
}; | ||
const myStorage = { | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
clear: jest.fn(), | ||
}; | ||
const client = new ReactNativeLDClient('mobile-key', AutoEnvAttributes.Enabled, { | ||
sendEvents: false, | ||
initialConnectionMode: 'offline', | ||
logger, | ||
storage: myStorage, | ||
}); | ||
|
||
await client.identify({ key: 'potato', kind: 'user' }, { timeout: 15 }); | ||
expect(myStorage.get).toHaveBeenCalled(); | ||
expect(myStorage.clear).not.toHaveBeenCalled(); | ||
// Ensure the base client is not emitting a warning for this. | ||
expect(logger.warn).not.toHaveBeenCalled(); | ||
}); |
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
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,17 +1,17 @@ | ||
import { LDLogger, Platform } from '@launchdarkly/js-client-sdk-common'; | ||
import { LDLogger, Platform, Storage } from '@launchdarkly/js-client-sdk-common'; | ||
|
||
import PlatformCrypto from './crypto'; | ||
import PlatformEncoding from './PlatformEncoding'; | ||
import PlatformInfo from './PlatformInfo'; | ||
import PlatformRequests from './PlatformRequests'; | ||
import PlatformStorage from './PlatformStorage'; | ||
|
||
const createPlatform = (logger: LDLogger): Platform => ({ | ||
const createPlatform = (logger: LDLogger, storage?: Storage): Platform => ({ | ||
crypto: new PlatformCrypto(), | ||
info: new PlatformInfo(logger), | ||
requests: new PlatformRequests(logger), | ||
encoding: new PlatformEncoding(), | ||
storage: new PlatformStorage(logger), | ||
storage: storage ?? new PlatformStorage(logger), | ||
}); | ||
|
||
export default createPlatform; |