Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PODS:
- boost-for-react-native (1.63.0)
- CocoaAsyncSocket (7.6.5)
- DoubleConversion (1.1.6)
- experiment-react-native-client (1.0.0):
- experiment-react-native-client (1.5.2):
- React-Core
- FBLazyVector (0.63.4)
- FBReactNativeSpec (0.63.4):
Expand Down Expand Up @@ -443,11 +443,11 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/yoga"

SPEC CHECKSUMS:
amplitude-react-native: 243eb398faa89b4b88998c536c4e9e0bdcbb4d9d
amplitude-react-native: 317744313df595a17f8638f6586b58bdfa161bcf
boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
DoubleConversion: cde416483dac037923206447da6e1454df403714
experiment-react-native-client: c2af8630b6209c5d4dcebdcd71ba7ad38abdcc38
experiment-react-native-client: 3ad919955ed53e2b3bd4a54341ab244d4fd84eab
FBLazyVector: 3bb422f41b18121b71783a905c10e58606f7dc3e
FBReactNativeSpec: f2c97f2529dd79c083355182cc158c9f98f4bd6e
Flipper: 1bd2db48dcc31e4b167b9a33ec1df01c2ded4893
Expand All @@ -457,19 +457,19 @@ SPEC CHECKSUMS:
Flipper-PeerTalk: 116d8f857dc6ef55c7a5a75ea3ceaafe878aadc9
Flipper-RSocket: 127954abe8b162fcaf68d2134d34dc2bd7076154
FlipperKit: 651f50a42eb95c01b3e89a60996dd6aded529eeb
Folly: b73c3869541e86821df3c387eb0af5f65addfab4
Folly: aeb27d02cdff07ca01f8a8a5a6dd5bcaf6be6f70
glog: 40a13f7840415b9a77023fbcae0f1e6f43192af3
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenSSL-Universal: 1aa4f6a6ee7256b83db99ec1ccdaa80d10f9af9b
RCTRequired: 082f10cd3f905d6c124597fd1c14f6f2655ff65e
RCTTypeSafety: 8c9c544ecbf20337d069e4ae7fd9a377aadf504b
React: b0a957a2c44da4113b0c4c9853d8387f8e64e615
React-callinvoker: c3f44dd3cb195b6aa46621fff95ded79d59043fe
React-Core: d3b2a1ac9a2c13c3bcde712d9281fc1c8a5b315b
React-Core: aaa6ff028f4e8baa828e5b5cfb6da115bc471cf9
React-CoreModules: 0581ff36cb797da0943d424f69e7098e43e9be60
React-cxxreact: c1480d4fda5720086c90df537ee7d285d4c57ac3
React-jsi: a0418934cf48f25b485631deb27c64dc40fb4c31
React-jsiexecutor: 93bd528844ad21dc07aab1c67cb10abae6df6949
React-cxxreact: af6918793c36186153bc09e981bfff8ba2b41d6a
React-jsi: df7455c330f8c1548225004fcc40031d4a7ade47
React-jsiexecutor: f42871dfefc3a3ed2a0e62932f750e09b7ae6e19
React-jsinspector: 58aef7155bc9a9683f5b60b35eccea8722a4f53a
React-RCTActionSheet: 89a0ca9f4a06c1f93c26067af074ccdce0f40336
React-RCTAnimation: 1bde3ecc0c104c55df246eda516e0deb03c4e49b
Expand All @@ -480,11 +480,11 @@ SPEC CHECKSUMS:
React-RCTSettings: 60f0691bba2074ef394f95d4c2265ec284e0a46a
React-RCTText: 5c51df3f08cb9dedc6e790161195d12bac06101c
React-RCTVibration: ae4f914cfe8de7d4de95ae1ea6cc8f6315d73d9d
ReactCommon: 73d79c7039f473b76db6ff7c6b159c478acbbb3b
RNCAsyncStorage: 466b9df1a14bccda91da86e0b7d9a345d78e1673
ReactCommon: 039a2aabebb7641184ba0cc3974c833b4c48d407
RNCAsyncStorage: 8cdcca8a75a7d36c402c02d2f011ff0897e8cb18
Yoga: 4bd86afe9883422a7c4028c00e34790f560923d6
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

PODFILE CHECKSUM: be8ac95a530dede03d7b3bef7b71d1e8fb613dfc

COCOAPODS: 1.11.3
COCOAPODS: 1.16.2
2 changes: 1 addition & 1 deletion src/experimentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class ExperimentClient implements Client {
httpClient,
);
// Storage & Caching
const storage = new LocalStorage();
const storage = this.config.storage || new LocalStorage();
this.variants = getVariantStorage(
this.apiKey,
this.config.instanceName,
Expand Down
8 changes: 3 additions & 5 deletions src/storage/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { EvaluationFlag } from '@amplitude/experiment-core';
import { Storage } from '../types/storage';
import { Variant } from '../types/variant';

import { LocalStorage } from './local-storage';

export const getVariantStorage = (
deploymentKey: string,
instanceName: string,
Expand All @@ -22,7 +20,7 @@ export const getVariantStorage = (
export const getFlagStorage = (
deploymentKey: string,
instanceName: string,
storage: Storage = new LocalStorage(),
storage: Storage,
): LoadStoreCache<EvaluationFlag> => {
const truncatedDeployment = deploymentKey.substring(deploymentKey.length - 6);
const namespace = `amp-exp-${instanceName}-${truncatedDeployment}-flags`;
Expand Down Expand Up @@ -75,7 +73,7 @@ export class LoadStoreCache<V> {
const rawValues = await this.storage.get(this.namespace);
let jsonValues: Record<string, unknown>;
try {
jsonValues = JSON.parse(rawValues) || {};
jsonValues = rawValues ? JSON.parse(rawValues) : {};
} catch {
// Do nothing
return;
Expand Down Expand Up @@ -104,7 +102,7 @@ export class LoadStoreCache<V> {
}

public async store(values: Record<string, V> = this.cache): Promise<void> {
await this.storage.put(this.namespace, JSON.stringify(values));
await this.storage.set(this.namespace, JSON.stringify(values));
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/storage/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
import { Storage } from '../types/storage';

export class LocalStorage implements Storage {
async get(key: string): Promise<string> {
async get(key: string): Promise<string | null> {
return await AsyncStorage.getItem(key);
}

async put(key: string, value: string): Promise<void> {
async set(key: string, value: string): Promise<void> {
// TTL is ignored in the default implementation
await AsyncStorage.setItem(key, value);
return;
}
Expand Down
9 changes: 9 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FetchHttpClient } from '../transport/http';

import { ExposureTrackingProvider } from './exposure';
import { Source } from './source';
import { Storage } from './storage';
import { HttpClient } from './transport';
import { ExperimentUserProvider } from './user';
import { Variant, Variants } from './variant';
Expand Down Expand Up @@ -131,6 +132,12 @@ export interface ExperimentConfig {
* (Advanced) Use your own http client.
*/
httpClient?: HttpClient;

/**
* Custom storage implementation. By default, AsyncStorage is used.
* Implement the Storage interface to use a different storage mechanism.
*/
storage?: Storage;
}

/**
Expand All @@ -156,6 +163,7 @@ export interface ExperimentConfig {
| **userProvider** | `null` |
| **analyticsProvider** | `null` |
| **exposureTrackingProvider** | `null` |
| **storage** | `null` (defaults to AsyncStorage) |

*
* @category Configuration
Expand All @@ -179,4 +187,5 @@ export const Defaults: ExperimentConfig = {
userProvider: null,
exposureTrackingProvider: null,
httpClient: FetchHttpClient,
storage: null,
};
4 changes: 2 additions & 2 deletions src/types/storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Storage {
get(key: string): Promise<string>;
put(key: string, value: string): Promise<void>;
get(key: string): Promise<string | null>;
set(key: string, value: string, ttl?: number): Promise<void>;
delete(key: string): Promise<void>;
}