@statsig/react-bindings 3.14.1 → 3.15.0

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/package.json CHANGED
@@ -1,9 +1,16 @@
1
1
  {
2
2
  "name": "@statsig/react-bindings",
3
- "version": "3.14.1",
3
+ "version": "3.15.0",
4
+ "license": "ISC",
5
+ "homepage": "https://github.com/statsig-io/js-client-monorepo",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "github:statsig-io/js-client-monorepo",
9
+ "directory": "packages/react-bindings"
10
+ },
4
11
  "dependencies": {
5
- "@statsig/client-core": "3.14.1",
6
- "@statsig/js-client": "3.14.1"
12
+ "@statsig/client-core": "3.15.0",
13
+ "@statsig/js-client": "3.15.0"
7
14
  },
8
15
  "peerDependencies": {
9
16
  "react": "^16.6.3 || ^17.0.0 || ^18.0.0 || ^19.0.0"
@@ -10,9 +10,10 @@ type WithConfiguration = {
10
10
  user: StatsigUser;
11
11
  options?: StatsigOptions;
12
12
  };
13
- export type StatsigProviderProps<T extends StatsigClient> = {
13
+ type ProviderChildrenProps = {
14
14
  children: ReactNode | ReactNode[];
15
15
  loadingComponent?: ReactNode | ReactNode[];
16
- } & (WithClient<T> | WithConfiguration);
16
+ };
17
+ export type StatsigProviderProps<T extends StatsigClient> = ProviderChildrenProps & (WithClient<T> | WithConfiguration);
17
18
  export declare function StatsigProvider(props: StatsigProviderProps<StatsigClient>): React.ReactElement;
18
19
  export {};
@@ -7,10 +7,34 @@ const client_core_1 = require("@statsig/client-core");
7
7
  const StatsigContext_1 = require("./StatsigContext");
8
8
  const useClientAsyncInit_1 = require("./useClientAsyncInit");
9
9
  function StatsigProvider(props) {
10
+ if (!('client' in props)) {
11
+ return (0, jsx_runtime_1.jsx)(ConfigBasedStatsigProvider, Object.assign({}, props));
12
+ }
13
+ if ('sdkKey' in props || 'user' in props) {
14
+ client_core_1.Log.warn('Both client and configuration props (sdkKey, user) were provided to StatsigProvider. The client prop will be used and the configuration props will be ignored.');
15
+ }
16
+ return (0, jsx_runtime_1.jsx)(ClientBasedStatsigProvider, Object.assign({}, props));
17
+ }
18
+ exports.StatsigProvider = StatsigProvider;
19
+ function ConfigBasedStatsigProvider(props) {
20
+ const [renderVersion, setRenderVersion] = (0, react_1.useState)(0);
21
+ const client = (0, useClientAsyncInit_1.useClientAsyncInit)(props.sdkKey, props.user, props.options).client;
22
+ useStatsigClientSetup(client, setRenderVersion);
23
+ const contextValue = (0, react_1.useMemo)(() => ({ renderVersion, client }), [renderVersion, client]);
24
+ return ((0, jsx_runtime_1.jsx)(StatsigContext_1.default.Provider, { value: contextValue, children: props.loadingComponent == null || _isReady(client)
25
+ ? props.children
26
+ : props.loadingComponent }));
27
+ }
28
+ function ClientBasedStatsigProvider(props) {
10
29
  const [renderVersion, setRenderVersion] = (0, react_1.useState)(0);
11
- const client = 'client' in props
12
- ? props.client
13
- : (0, useClientAsyncInit_1.useClientAsyncInit)(props.sdkKey, props.user, props.options).client;
30
+ const client = props.client;
31
+ useStatsigClientSetup(client, setRenderVersion);
32
+ const contextValue = (0, react_1.useMemo)(() => ({ renderVersion, client }), [renderVersion, client]);
33
+ return ((0, jsx_runtime_1.jsx)(StatsigContext_1.default.Provider, { value: contextValue, children: props.loadingComponent == null || _isReady(client)
34
+ ? props.children
35
+ : props.loadingComponent }));
36
+ }
37
+ function useStatsigClientSetup(client, setRenderVersion) {
14
38
  (0, react_1.useEffect)(() => {
15
39
  const onValuesUpdated = () => {
16
40
  setRenderVersion((v) => v + 1);
@@ -20,16 +44,11 @@ function StatsigProvider(props) {
20
44
  return () => {
21
45
  client
22
46
  .flush()
23
- .catch((err) => client_core_1.Log.error('An error occured during flush', err));
47
+ .catch((err) => client_core_1.Log.error('An error occurred during flush', err));
24
48
  client.off('values_updated', onValuesUpdated);
25
49
  };
26
- }, [client]);
27
- const contextValue = (0, react_1.useMemo)(() => ({ renderVersion, client }), [renderVersion, client]);
28
- return ((0, jsx_runtime_1.jsx)(StatsigContext_1.default.Provider, { value: contextValue, children: props.loadingComponent == null || _isReady(client)
29
- ? props.children
30
- : props.loadingComponent }));
50
+ }, [client, setRenderVersion]);
31
51
  }
32
- exports.StatsigProvider = StatsigProvider;
33
52
  function _isReady(client) {
34
53
  if ('isNoop' in client) {
35
54
  return true;
@@ -1,5 +1,5 @@
1
1
  import { PrecomputedEvaluationsInterface } from '@statsig/client-core';
2
- type HositedFuncs = Pick<PrecomputedEvaluationsInterface, 'checkGate' | 'getFeatureGate' | 'getDynamicConfig' | 'getExperiment' | 'getLayer' | 'logEvent'>;
2
+ type HositedFuncs = Pick<PrecomputedEvaluationsInterface, 'checkGate' | 'getFeatureGate' | 'getDynamicConfig' | 'getExperiment' | 'getLayer' | 'getParameterStore' | 'logEvent'>;
3
3
  type Output = HositedFuncs & {
4
4
  client: PrecomputedEvaluationsInterface;
5
5
  };
@@ -30,6 +30,9 @@ function useStatsigClient() {
30
30
  const getLayer = (0, react_1.useCallback)((name, options) => {
31
31
  return client.getLayer(name, options);
32
32
  }, deps);
33
+ const getParameterStore = (0, react_1.useCallback)((name, options) => {
34
+ return client.getParameterStore(name, options);
35
+ }, deps);
33
36
  const logEvent = (0, react_1.useCallback)((eventName, value, metadata) => {
34
37
  if (typeof eventName === 'string') {
35
38
  return client.logEvent(eventName, value, metadata);
@@ -44,6 +47,7 @@ function useStatsigClient() {
44
47
  getDynamicConfig,
45
48
  getExperiment,
46
49
  getLayer,
50
+ getParameterStore,
47
51
  logEvent,
48
52
  };
49
53
  }, [
@@ -53,6 +57,7 @@ function useStatsigClient() {
53
57
  getDynamicConfig,
54
58
  getExperiment,
55
59
  getLayer,
60
+ getParameterStore,
56
61
  logEvent,
57
62
  ]);
58
63
  }
@@ -4,24 +4,18 @@ exports.useStatsigInternalClientFactoryAsync = void 0;
4
4
  const react_1 = require("react");
5
5
  const client_core_1 = require("@statsig/client-core");
6
6
  function useStatsigInternalClientFactoryAsync(factory, args) {
7
- const [isLoading, setIsLoading] = (0, react_1.useState)(true);
8
- const clientRef = (0, react_1.useRef)((0, client_core_1._getInstance)(args.sdkKey));
9
- const client = (0, react_1.useMemo)(() => {
10
- if (clientRef.current) {
11
- // Repeat calls to initializeAsync return the same promise
12
- clientRef.current
7
+ const client = (0, react_1.useMemo)(() => { var _a; return (_a = (0, client_core_1._getInstance)(args.sdkKey)) !== null && _a !== void 0 ? _a : factory(args); }, []);
8
+ const [isLoading, setIsLoading] = (0, react_1.useState)(client.loadingStatus !== 'Ready');
9
+ (0, react_1.useMemo)(() => {
10
+ if (client.loadingStatus !== 'Ready') {
11
+ // Repeat calls to initializeAsync return the same promise.
12
+ // But if the client is already loaded, we don't want the promise
13
+ // resolution to trigger an extra render on `setIsLoading(false)`
14
+ client
13
15
  .initializeAsync()
14
16
  .catch(client_core_1.Log.error)
15
17
  .finally(() => setIsLoading(false));
16
- return clientRef.current;
17
18
  }
18
- const inst = factory(args);
19
- clientRef.current = inst;
20
- inst
21
- .initializeAsync()
22
- .catch(client_core_1.Log.error)
23
- .finally(() => setIsLoading(false));
24
- return inst;
25
19
  }, []);
26
20
  return { client, isLoading };
27
21
  }