@statsig/react-bindings 0.0.1-beta.35 → 0.0.1-beta.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # Statsig - React
2
2
 
3
- > [!IMPORTANT]
4
- > This version of the SDK is still in beta. The non-beta version can be found [here](https://github.com/statsig-io/react-sdk).
5
-
6
3
  ReactJS specific additions for use with a Statsig Javascript SDK.
7
4
 
8
5
  Learn more by visiting: https://docs.statsig.com/client/javascript-sdk
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@statsig/react-bindings",
3
- "version": "0.0.1-beta.35",
3
+ "version": "0.0.1-beta.37",
4
4
  "dependencies": {
5
- "@statsig/client-core": "0.0.1-beta.35"
5
+ "@statsig/client-core": "0.0.1-beta.37"
6
6
  },
7
7
  "peerDependencies": {
8
8
  "react": "^16.6.3 || ^17.0.0 || ^18.0.0"
@@ -56,7 +56,10 @@ const _client = {
56
56
  updateUserAsync: _noopAsync,
57
57
  getContext: () => (Object.assign({}, context)),
58
58
  getAsyncContext: () => __awaiter(void 0, void 0, void 0, function* () {
59
- return (Object.assign(Object.assign({}, context), { sessionID: '', stableID: '' }));
59
+ return (Object.assign(Object.assign({}, context), { session: {
60
+ data: { sessionID: '', startTime: 0, lastUpdate: 0 },
61
+ sdkKey: '',
62
+ }, stableID: '' }));
60
63
  }),
61
64
  checkGate: () => false,
62
65
  getFeatureGate: _defaultEvaluation('gate'),
@@ -66,7 +69,8 @@ const _client = {
66
69
  logEvent: _noop,
67
70
  on: _noop,
68
71
  off: _noop,
69
- __on: _noop,
72
+ $on: _noop,
73
+ $emt: _noop,
70
74
  dataAdapter: _noopDataAdapter,
71
75
  };
72
76
  exports.NoopEvaluationsClient = _client;
@@ -12,7 +12,7 @@ function StatsigProvider({ client, children, }) {
12
12
  setRenderVersion((v) => v + 1);
13
13
  };
14
14
  client_core_1.SDKType._setBindingType('react');
15
- client.__on('values_updated', onValuesUpdated);
15
+ client.$on('values_updated', onValuesUpdated);
16
16
  return () => {
17
17
  client.shutdown().catch((error) => {
18
18
  client_core_1.Log.error('An error occured during shutdown', error);
@@ -16,6 +16,6 @@ function default_1(configName, options) {
16
16
  }
17
17
  client_core_1.Log.warn(`useDynamicConfig hook failed to find a valid Statsig client for dynamic config '${configName}'.`);
18
18
  return NoopEvaluationsClient_1.NoopEvaluationsClient.getDynamicConfig(configName, options);
19
- }, [configName, renderVersion, options]);
19
+ }, [configName, client, renderVersion, options]);
20
20
  }
21
21
  exports.default = default_1;
@@ -16,6 +16,6 @@ function default_1(experimentName, options) {
16
16
  }
17
17
  client_core_1.Log.warn(`useExperiment hook failed to find a valid Statsig client for experiment '${experimentName}'.`);
18
18
  return NoopEvaluationsClient_1.NoopEvaluationsClient.getExperiment(experimentName, options);
19
- }, [experimentName, renderVersion, options]);
19
+ }, [experimentName, client, renderVersion, options]);
20
20
  }
21
21
  exports.default = default_1;
@@ -16,7 +16,7 @@ function default_1(gateName, options) {
16
16
  }
17
17
  client_core_1.Log.warn(`useFeatureGate hook failed to find a valid Statsig client for gate '${gateName}'.`);
18
18
  return NoopEvaluationsClient_1.NoopEvaluationsClient.getFeatureGate(gateName, options);
19
- }, [gateName, renderVersion, options]);
19
+ }, [gateName, client, renderVersion, options]);
20
20
  return gate;
21
21
  }
22
22
  exports.default = default_1;
@@ -16,7 +16,7 @@ function default_1(gateName, options) {
16
16
  }
17
17
  client_core_1.Log.warn(`useFeatureGate hook failed to find a valid Statsig client for gate '${gateName}'.`);
18
18
  return NoopEvaluationsClient_1.NoopEvaluationsClient.checkGate(gateName, options);
19
- }, [gateName, renderVersion, options]);
19
+ }, [gateName, client, renderVersion, options]);
20
20
  return gate;
21
21
  }
22
22
  exports.default = default_1;
package/src/useLayer.js CHANGED
@@ -16,7 +16,7 @@ function default_1(layerName, options) {
16
16
  }
17
17
  client_core_1.Log.warn(`useLayer hook failed to find a valid Statsig client for layer '${layerName}'.`);
18
18
  return NoopEvaluationsClient_1.NoopEvaluationsClient.getLayer(layerName, options);
19
- }, [layerName, renderVersion, options]);
19
+ }, [layerName, client, renderVersion, options]);
20
20
  return layer;
21
21
  }
22
22
  exports.default = default_1;
@@ -1,2 +1,6 @@
1
1
  import { PrecomputedEvaluationsInterface } from '@statsig/client-core';
2
- export declare function useStatsigClient(): PrecomputedEvaluationsInterface;
2
+ type HositedFuncs = Pick<PrecomputedEvaluationsInterface, 'checkGate' | 'getFeatureGate' | 'getDynamicConfig' | 'getExperiment' | 'getLayer' | 'logEvent'>;
3
+ export declare function useStatsigClient(): HositedFuncs & {
4
+ client: PrecomputedEvaluationsInterface;
5
+ };
6
+ export {};
@@ -7,11 +7,38 @@ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
7
7
  const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
8
8
  const StatsigContext_1 = require("./StatsigContext");
9
9
  function useStatsigClient() {
10
- const { client } = (0, react_1.useContext)(StatsigContext_1.default);
11
- if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(client)) {
12
- return client;
13
- }
14
- client_core_1.Log.warn('Attempting to retrive a Statsig StatsigClient but none was set.');
15
- return NoopEvaluationsClient_1.NoopEvaluationsClient;
10
+ const { client: anyClient, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
11
+ const client = (0, react_1.useMemo)(() => {
12
+ if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(anyClient)) {
13
+ return anyClient;
14
+ }
15
+ client_core_1.Log.warn('Attempting to retrieve a StatsigClient but none was set.');
16
+ return NoopEvaluationsClient_1.NoopEvaluationsClient;
17
+ }, [anyClient, renderVersion]);
18
+ const deps = [client, renderVersion];
19
+ return {
20
+ client,
21
+ checkGate: (0, react_1.useCallback)((name, options) => {
22
+ return client.checkGate(name, options);
23
+ }, deps),
24
+ getFeatureGate: (0, react_1.useCallback)((name, options) => {
25
+ return client.getFeatureGate(name, options);
26
+ }, deps),
27
+ getDynamicConfig: (0, react_1.useCallback)((name, options) => {
28
+ return client.getDynamicConfig(name, options);
29
+ }, deps),
30
+ getExperiment: (0, react_1.useCallback)((name, options) => {
31
+ return client.getExperiment(name, options);
32
+ }, deps),
33
+ getLayer: (0, react_1.useCallback)((name, options) => {
34
+ return client.getLayer(name, options);
35
+ }, deps),
36
+ logEvent: (0, react_1.useCallback)((eventName, value, metadata) => {
37
+ if (typeof eventName === 'string') {
38
+ return client.logEvent(eventName, value, metadata);
39
+ }
40
+ return client.logEvent(eventName);
41
+ }, deps),
42
+ };
16
43
  }
17
44
  exports.useStatsigClient = useStatsigClient;
@@ -1,2 +1,6 @@
1
1
  import { OnDeviceEvaluationsInterface } from '@statsig/client-core';
2
- export declare function useStatsigOnDeviceEvalClient(): OnDeviceEvaluationsInterface;
2
+ type HositedFuncs = Pick<OnDeviceEvaluationsInterface, 'checkGate' | 'getFeatureGate' | 'getDynamicConfig' | 'getExperiment' | 'getLayer' | 'logEvent'>;
3
+ export declare function useStatsigOnDeviceEvalClient(): HositedFuncs & {
4
+ client: OnDeviceEvaluationsInterface;
5
+ };
6
+ export {};
@@ -7,11 +7,38 @@ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
7
7
  const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
8
8
  const StatsigContext_1 = require("./StatsigContext");
9
9
  function useStatsigOnDeviceEvalClient() {
10
- const { client } = (0, react_1.useContext)(StatsigContext_1.default);
11
- if (!(0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(client)) {
12
- return client;
13
- }
14
- client_core_1.Log.warn('Attempting to retrive a Statsig StatsigOnDeviceEvalClient but none was set.');
15
- return NoopEvaluationsClient_1.NoopEvaluationsClient;
10
+ const { client: anyClient, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
11
+ const client = (0, react_1.useMemo)(() => {
12
+ if (!(0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(anyClient)) {
13
+ return anyClient;
14
+ }
15
+ client_core_1.Log.warn('Attempting to retrieve a StatsigOnDeviceEvalClient but none was set.');
16
+ return NoopEvaluationsClient_1.NoopEvaluationsClient;
17
+ }, [anyClient, renderVersion]);
18
+ const deps = [client, renderVersion];
19
+ return {
20
+ client,
21
+ checkGate: (0, react_1.useCallback)((name, options) => {
22
+ return client.checkGate(name, options);
23
+ }, deps),
24
+ getFeatureGate: (0, react_1.useCallback)((name, options) => {
25
+ return client.getFeatureGate(name, options);
26
+ }, deps),
27
+ getDynamicConfig: (0, react_1.useCallback)((name, options) => {
28
+ return client.getDynamicConfig(name, options);
29
+ }, deps),
30
+ getExperiment: (0, react_1.useCallback)((name, options) => {
31
+ return client.getExperiment(name, options);
32
+ }, deps),
33
+ getLayer: (0, react_1.useCallback)((name, options) => {
34
+ return client.getLayer(name, options);
35
+ }, deps),
36
+ logEvent: (0, react_1.useCallback)((eventName, user, value, metadata) => {
37
+ if (typeof eventName === 'string') {
38
+ return client.logEvent(eventName, user, value, metadata);
39
+ }
40
+ return client.logEvent(eventName, user);
41
+ }, deps),
42
+ };
16
43
  }
17
44
  exports.useStatsigOnDeviceEvalClient = useStatsigOnDeviceEvalClient;
@@ -1,7 +1,12 @@
1
1
  import { StatsigUser } from '@statsig/client-core';
2
+ type UpdaterArg = StatsigUser | ((previous: StatsigUser) => StatsigUser);
3
+ type UpdaterFunc<T> = (updated: UpdaterArg) => T;
4
+ type SyncUpdateFunc = UpdaterFunc<void>;
5
+ type AsyncUpdateFunc = UpdaterFunc<Promise<void>>;
2
6
  export type UseStatsigUserResult = {
3
7
  user: StatsigUser;
4
- updateUserSync: (fn: (prevState: StatsigUser) => StatsigUser) => void;
5
- updateUserAsync: (fn: (prevState: StatsigUser) => StatsigUser) => Promise<void>;
8
+ updateUserSync: SyncUpdateFunc;
9
+ updateUserAsync: AsyncUpdateFunc;
6
10
  };
7
11
  export declare function useStatsigUser(): UseStatsigUserResult;
12
+ export {};
@@ -5,22 +5,27 @@ const react_1 = require("react");
5
5
  const StatsigContext_1 = require("./StatsigContext");
6
6
  const useStatsigClient_1 = require("./useStatsigClient");
7
7
  function useStatsigUser() {
8
- const client = (0, useStatsigClient_1.useStatsigClient)();
8
+ const { client } = (0, useStatsigClient_1.useStatsigClient)();
9
9
  const { renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
10
+ const deps = [client, renderVersion];
10
11
  const memoUser = (0, react_1.useMemo)(() => {
11
12
  const context = client.getContext();
12
13
  return context.user;
13
- }, [client, renderVersion]);
14
+ }, deps);
14
15
  return {
15
16
  user: memoUser,
16
- updateUserSync: (fn) => {
17
- const user = fn(memoUser);
18
- client.updateUserSync(user);
19
- },
20
- updateUserAsync: (fn) => {
21
- const user = fn(memoUser);
22
- return client.updateUserAsync(user);
23
- },
17
+ updateUserSync: (0, react_1.useCallback)((arg) => {
18
+ if (typeof arg === 'function') {
19
+ arg = arg(memoUser);
20
+ }
21
+ client.updateUserSync(arg);
22
+ }, deps),
23
+ updateUserAsync: (0, react_1.useCallback)((arg) => {
24
+ if (typeof arg === 'function') {
25
+ arg = arg(memoUser);
26
+ }
27
+ return client.updateUserAsync(arg);
28
+ }, deps),
24
29
  };
25
30
  }
26
31
  exports.useStatsigUser = useStatsigUser;