@statsig/react-bindings 0.0.1-beta.9 → 1.0.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/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.9",
3
+ "version": "1.0.0",
4
4
  "dependencies": {
5
- "@statsig/client-core": "0.0.1-beta.9"
5
+ "@statsig/client-core": "1.0.0"
6
6
  },
7
7
  "peerDependencies": {
8
8
  "react": "^16.6.3 || ^17.0.0 || ^18.0.0"
@@ -1,44 +1,77 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.NoopEvaluationsClient = void 0;
4
- var client_core_1 = require("@statsig/client-core");
5
- var noop = function () {
13
+ const client_core_1 = require("@statsig/client-core");
14
+ const _noop = () => {
6
15
  // noop
7
16
  };
8
- var NOOP_DETAILS = { reason: 'Error' };
9
- var defaultEvaluation = function (type) {
10
- return function () {
11
- var args = [];
12
- for (var _i = 0; _i < arguments.length; _i++) {
13
- args[_i] = arguments[_i];
14
- }
15
- var name = typeof args[0] === 'string' ? args[0] : args[1];
17
+ const _noopAsync = () => Promise.resolve();
18
+ const NOOP_DETAILS = { reason: 'Error' };
19
+ const _defaultEvaluation = (type) => {
20
+ return (...args) => {
21
+ const name = typeof args[0] === 'string' ? args[0] : args[1];
16
22
  switch (type) {
17
23
  case 'gate':
18
- return (0, client_core_1.makeFeatureGate)(name, NOOP_DETAILS);
24
+ return (0, client_core_1._makeFeatureGate)(name, NOOP_DETAILS, null);
19
25
  case 'config':
20
- return (0, client_core_1.makeDynamicConfig)(name, NOOP_DETAILS);
26
+ return (0, client_core_1._makeDynamicConfig)(name, NOOP_DETAILS, null);
21
27
  case 'layer':
22
- return (0, client_core_1.makeLayer)(name, NOOP_DETAILS);
28
+ return (0, client_core_1._makeLayer)(name, NOOP_DETAILS, null);
23
29
  }
24
30
  };
25
31
  };
26
- var client = {
32
+ const _noopDataAdapter = {
33
+ __primeInMemoryCache: _noop,
34
+ attach: _noop,
35
+ getDataSync: () => null,
36
+ getDataAsync: () => Promise.resolve(null),
37
+ setData: _noop,
38
+ setDataLegacy: _noop,
39
+ prefetchData: _noopAsync,
40
+ };
41
+ const context = {
42
+ sdkKey: '',
43
+ options: {},
44
+ values: null,
45
+ user: { userID: '' },
46
+ errorBoundary: {},
47
+ };
48
+ const _client = {
27
49
  isNoop: true,
28
50
  loadingStatus: 'Uninitialized',
29
- initializeSync: noop,
30
- initializeAsync: function () { return Promise.resolve(); },
31
- shutdown: function () { return Promise.resolve(); },
32
- updateUserSync: noop,
33
- updateUserAsync: function (_u) { return Promise.resolve(); },
34
- getCurrentUser: function () { return ({ userID: '' }); },
35
- checkGate: function () { return false; },
36
- getFeatureGate: defaultEvaluation('gate'),
37
- getDynamicConfig: defaultEvaluation('config'),
38
- getExperiment: defaultEvaluation('config'),
39
- getLayer: defaultEvaluation('layer'),
40
- logEvent: noop,
41
- on: noop,
42
- off: noop,
51
+ initializeSync: _noop,
52
+ initializeAsync: _noopAsync,
53
+ shutdown: _noopAsync,
54
+ flush: _noopAsync,
55
+ updateRuntimeOptions: _noop,
56
+ updateUserSync: _noop,
57
+ updateUserAsync: _noopAsync,
58
+ getContext: () => (Object.assign({}, context)),
59
+ getAsyncContext: () => __awaiter(void 0, void 0, void 0, function* () {
60
+ return (Object.assign(Object.assign({}, context), { session: {
61
+ data: { sessionID: '', startTime: 0, lastUpdate: 0 },
62
+ sdkKey: '',
63
+ }, stableID: '' }));
64
+ }),
65
+ checkGate: () => false,
66
+ getFeatureGate: _defaultEvaluation('gate'),
67
+ getDynamicConfig: _defaultEvaluation('config'),
68
+ getExperiment: _defaultEvaluation('config'),
69
+ getLayer: _defaultEvaluation('layer'),
70
+ logEvent: _noop,
71
+ on: _noop,
72
+ off: _noop,
73
+ $on: _noop,
74
+ $emt: _noop,
75
+ dataAdapter: _noopDataAdapter,
43
76
  };
44
- exports.NoopEvaluationsClient = client;
77
+ exports.NoopEvaluationsClient = _client;
@@ -1,5 +1,4 @@
1
1
  import { OnDeviceEvaluationsInterface, PrecomputedEvaluationsInterface } from '@statsig/client-core';
2
- export declare function isPrecomputedEvaluationsClient(client: OnDeviceEvaluationsInterface | PrecomputedEvaluationsInterface | {
3
- isNoop: true;
4
- }): client is PrecomputedEvaluationsInterface;
2
+ export declare function isPrecomputedEvalClient(client: OnDeviceEvaluationsInterface | PrecomputedEvaluationsInterface): client is PrecomputedEvaluationsInterface;
3
+ export declare function isNoopClient(client: OnDeviceEvaluationsInterface | PrecomputedEvaluationsInterface): boolean;
5
4
  export declare function logMissingStatsigUserWarning(): void;
@@ -1,14 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.logMissingStatsigUserWarning = exports.isPrecomputedEvaluationsClient = void 0;
4
- var client_core_1 = require("@statsig/client-core");
5
- function isPrecomputedEvaluationsClient(client) {
6
- if ('isNoop' in client) {
3
+ exports.logMissingStatsigUserWarning = exports.isNoopClient = exports.isPrecomputedEvalClient = void 0;
4
+ const client_core_1 = require("@statsig/client-core");
5
+ function isPrecomputedEvalClient(client) {
6
+ if (isNoopClient(client)) {
7
7
  return false;
8
8
  }
9
9
  return 'updateUserSync' in client;
10
10
  }
11
- exports.isPrecomputedEvaluationsClient = isPrecomputedEvaluationsClient;
11
+ exports.isPrecomputedEvalClient = isPrecomputedEvalClient;
12
+ function isNoopClient(client) {
13
+ return 'isNoop' in client;
14
+ }
15
+ exports.isNoopClient = isNoopClient;
12
16
  function logMissingStatsigUserWarning() {
13
17
  client_core_1.Log.warn('StatsigUser not provided for On Device Evaluation. Returning default value.');
14
18
  }
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var react_1 = require("react");
3
+ const react_1 = require("react");
4
+ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
4
5
  exports.default = (0, react_1.createContext)({
5
6
  renderVersion: 0,
6
- client: {},
7
+ client: NoopEvaluationsClient_1.NoopEvaluationsClient,
7
8
  });
@@ -1,8 +1,7 @@
1
1
  import { ReactNode } from 'react';
2
2
  import { StatsigClientInterface } from '@statsig/client-core';
3
- type Props = {
3
+ export type StatsigProviderProps = {
4
4
  children: ReactNode | ReactNode[];
5
5
  client: StatsigClientInterface;
6
6
  };
7
- export default function StatsigProvider(props: Props): JSX.Element;
8
- export {};
7
+ export declare function StatsigProvider({ client, children, }: StatsigProviderProps): JSX.Element;
@@ -1,30 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var jsx_runtime_1 = require("react/jsx-runtime");
4
- var react_1 = require("react");
5
- var client_core_1 = require("@statsig/client-core");
6
- var StatsigContext_1 = require("./StatsigContext");
7
- function StatsigProvider(props) {
8
- var _a = (0, react_1.useState)(0), renderVersion = _a[0], setRenderVersion = _a[1];
9
- var client = props.client, children = props.children;
10
- (0, react_1.useEffect)(function () {
11
- var onStatusChange = function (data) {
12
- if (data.event === 'status_change') {
13
- setRenderVersion(function (v) { return v + 1; });
14
- }
3
+ exports.StatsigProvider = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const react_1 = require("react");
6
+ const client_core_1 = require("@statsig/client-core");
7
+ const StatsigContext_1 = require("./StatsigContext");
8
+ function StatsigProvider({ client, children, }) {
9
+ const [renderVersion, setRenderVersion] = (0, react_1.useState)(0);
10
+ (0, react_1.useEffect)(() => {
11
+ const onValuesUpdated = () => {
12
+ setRenderVersion((v) => v + 1);
15
13
  };
16
- client.on('status_change', onStatusChange);
17
- client.initializeSync();
18
- return function () {
19
- client.shutdown().catch(function (error) {
14
+ client_core_1.SDKType._setBindingType('react');
15
+ client.$on('values_updated', onValuesUpdated);
16
+ return () => {
17
+ client.shutdown().catch((error) => {
20
18
  client_core_1.Log.error('An error occured during shutdown', error);
21
19
  });
22
- client.off('status_change', onStatusChange);
20
+ client.off('values_updated', onValuesUpdated);
23
21
  };
24
22
  }, [client]);
25
- return ((0, jsx_runtime_1.jsx)(StatsigContext_1.default.Provider, { value: { renderVersion: renderVersion, client: client }, children: _shouldRender(client) ? children : null }));
23
+ return ((0, jsx_runtime_1.jsx)(StatsigContext_1.default.Provider, { value: { renderVersion, client }, children: _shouldRender(client) ? children : null }));
26
24
  }
27
- exports.default = StatsigProvider;
25
+ exports.StatsigProvider = StatsigProvider;
28
26
  function _shouldRender(client) {
29
27
  if ('isNoop' in client) {
30
28
  return true;
package/src/index.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import StatsigContext from './StatsigContext';
2
- import StatsigProvider from './StatsigProvider';
2
+ import { StatsigProvider, StatsigProviderProps } from './StatsigProvider';
3
3
  import useDynamicConfig from './useDynamicConfig';
4
4
  import useExperiment from './useExperiment';
5
- import useGate from './useGate';
5
+ import useFeatureGate from './useFeatureGate';
6
+ import useGateValue from './useGateValue';
6
7
  import useLayer from './useLayer';
7
- import useStatsigOnDeviceEvaluationsClient from './useStatsigOnDeviceEvaluationsClient';
8
- import useStatsigPrecomputedEvaluationsClient from './useStatsigPrecomputedEvaluationsClient';
9
- import useStatsigUser from './useStatsigUser';
10
- export { StatsigContext, StatsigProvider, useGate, useDynamicConfig, useExperiment, useLayer, useStatsigUser, useStatsigOnDeviceEvaluationsClient, useStatsigPrecomputedEvaluationsClient, };
8
+ import { useStatsigClient } from './useStatsigClient';
9
+ import { useStatsigOnDeviceEvalClient } from './useStatsigOnDeviceEvalClient';
10
+ import { useStatsigUser } from './useStatsigUser';
11
+ export type { StatsigProviderProps };
12
+ export { StatsigContext, StatsigProvider, useDynamicConfig, useExperiment, useGateValue, useFeatureGate, useLayer, useStatsigOnDeviceEvalClient, useStatsigClient, useStatsigUser, };
package/src/index.js CHANGED
@@ -1,33 +1,32 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.useStatsigPrecomputedEvaluationsClient = exports.useStatsigOnDeviceEvaluationsClient = exports.useStatsigUser = exports.useLayer = exports.useExperiment = exports.useDynamicConfig = exports.useGate = exports.StatsigProvider = exports.StatsigContext = void 0;
15
- var StatsigContext_1 = require("./StatsigContext");
3
+ exports.useStatsigUser = exports.useStatsigClient = exports.useStatsigOnDeviceEvalClient = exports.useLayer = exports.useFeatureGate = exports.useGateValue = exports.useExperiment = exports.useDynamicConfig = exports.StatsigProvider = exports.StatsigContext = void 0;
4
+ const StatsigContext_1 = require("./StatsigContext");
16
5
  exports.StatsigContext = StatsigContext_1.default;
17
- var StatsigProvider_1 = require("./StatsigProvider");
18
- exports.StatsigProvider = StatsigProvider_1.default;
19
- var useDynamicConfig_1 = require("./useDynamicConfig");
6
+ const StatsigProvider_1 = require("./StatsigProvider");
7
+ Object.defineProperty(exports, "StatsigProvider", { enumerable: true, get: function () { return StatsigProvider_1.StatsigProvider; } });
8
+ const useDynamicConfig_1 = require("./useDynamicConfig");
20
9
  exports.useDynamicConfig = useDynamicConfig_1.default;
21
- var useExperiment_1 = require("./useExperiment");
10
+ const useExperiment_1 = require("./useExperiment");
22
11
  exports.useExperiment = useExperiment_1.default;
23
- var useGate_1 = require("./useGate");
24
- exports.useGate = useGate_1.default;
25
- var useLayer_1 = require("./useLayer");
12
+ const useFeatureGate_1 = require("./useFeatureGate");
13
+ exports.useFeatureGate = useFeatureGate_1.default;
14
+ const useGateValue_1 = require("./useGateValue");
15
+ exports.useGateValue = useGateValue_1.default;
16
+ const useLayer_1 = require("./useLayer");
26
17
  exports.useLayer = useLayer_1.default;
27
- var useStatsigOnDeviceEvaluationsClient_1 = require("./useStatsigOnDeviceEvaluationsClient");
28
- exports.useStatsigOnDeviceEvaluationsClient = useStatsigOnDeviceEvaluationsClient_1.default;
29
- var useStatsigPrecomputedEvaluationsClient_1 = require("./useStatsigPrecomputedEvaluationsClient");
30
- exports.useStatsigPrecomputedEvaluationsClient = useStatsigPrecomputedEvaluationsClient_1.default;
31
- var useStatsigUser_1 = require("./useStatsigUser");
32
- exports.useStatsigUser = useStatsigUser_1.default;
33
- __STATSIG__ = __assign(__assign({}, __STATSIG__), { StatsigContext: StatsigContext_1.default, StatsigProvider: StatsigProvider_1.default, useGate: useGate_1.default, useDynamicConfig: useDynamicConfig_1.default, useExperiment: useExperiment_1.default, useLayer: useLayer_1.default, useStatsigOnDeviceEvaluationsClient: useStatsigOnDeviceEvaluationsClient_1.default, useStatsigPrecomputedEvaluationsClient: useStatsigPrecomputedEvaluationsClient_1.default });
18
+ const useStatsigClient_1 = require("./useStatsigClient");
19
+ Object.defineProperty(exports, "useStatsigClient", { enumerable: true, get: function () { return useStatsigClient_1.useStatsigClient; } });
20
+ const useStatsigOnDeviceEvalClient_1 = require("./useStatsigOnDeviceEvalClient");
21
+ Object.defineProperty(exports, "useStatsigOnDeviceEvalClient", { enumerable: true, get: function () { return useStatsigOnDeviceEvalClient_1.useStatsigOnDeviceEvalClient; } });
22
+ const useStatsigUser_1 = require("./useStatsigUser");
23
+ Object.defineProperty(exports, "useStatsigUser", { enumerable: true, get: function () { return useStatsigUser_1.useStatsigUser; } });
24
+ __STATSIG__ = Object.assign(Object.assign({}, (__STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {})), { StatsigContext: StatsigContext_1.default,
25
+ StatsigProvider: StatsigProvider_1.StatsigProvider,
26
+ useDynamicConfig: useDynamicConfig_1.default,
27
+ useExperiment: useExperiment_1.default,
28
+ useGateValue: useGateValue_1.default,
29
+ useFeatureGate: useFeatureGate_1.default,
30
+ useLayer: useLayer_1.default,
31
+ useStatsigOnDeviceEvalClient: useStatsigOnDeviceEvalClient_1.useStatsigOnDeviceEvalClient,
32
+ useStatsigClient: useStatsigClient_1.useStatsigClient });
@@ -1,4 +1,5 @@
1
- import { DynamicConfig } from '@statsig/client-core';
2
- import { UseConfigOptions } from './useConfigImpl';
3
- export type UseDynamicConfigOptions = UseConfigOptions;
1
+ import { DynamicConfig, DynamicConfigEvaluationOptions, StatsigUser } from '@statsig/client-core';
2
+ export type UseDynamicConfigOptions = DynamicConfigEvaluationOptions & {
3
+ user: StatsigUser | null;
4
+ };
4
5
  export default function (configName: string, options?: UseDynamicConfigOptions): DynamicConfig;
@@ -1,20 +1,21 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
- var client_core_1 = require("@statsig/client-core");
15
- var useConfigImpl_1 = require("./useConfigImpl");
3
+ const react_1 = require("react");
4
+ const client_core_1 = require("@statsig/client-core");
5
+ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
6
+ const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
7
+ const StatsigContext_1 = require("./StatsigContext");
16
8
  function default_1(configName, options) {
17
- if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
18
- return (0, useConfigImpl_1.useConfigImpl)('useDynamicConfig', configName, options);
9
+ const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
10
+ return (0, react_1.useMemo)(() => {
11
+ if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(client)) {
12
+ return client.getDynamicConfig(configName, options);
13
+ }
14
+ if ((options === null || options === void 0 ? void 0 : options.user) != null) {
15
+ return client.getDynamicConfig(configName, options.user, options);
16
+ }
17
+ client_core_1.Log.warn(`useDynamicConfig hook failed to find a valid Statsig client for dynamic config '${configName}'.`);
18
+ return NoopEvaluationsClient_1.NoopEvaluationsClient.getDynamicConfig(configName, options);
19
+ }, [configName, client, renderVersion, options]);
19
20
  }
20
21
  exports.default = default_1;
@@ -1,4 +1,5 @@
1
- import { Experiment } from '@statsig/client-core';
2
- import { UseConfigOptions } from './useConfigImpl';
3
- export type UseExperimentOptions = UseConfigOptions;
1
+ import { Experiment, ExperimentEvaluationOptions, StatsigUser } from '@statsig/client-core';
2
+ export type UseExperimentOptions = ExperimentEvaluationOptions & {
3
+ user: StatsigUser | null;
4
+ };
4
5
  export default function (experimentName: string, options?: UseExperimentOptions): Experiment;
@@ -1,20 +1,21 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
- var client_core_1 = require("@statsig/client-core");
15
- var useConfigImpl_1 = require("./useConfigImpl");
3
+ const react_1 = require("react");
4
+ const client_core_1 = require("@statsig/client-core");
5
+ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
6
+ const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
7
+ const StatsigContext_1 = require("./StatsigContext");
16
8
  function default_1(experimentName, options) {
17
- if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
18
- return (0, useConfigImpl_1.useConfigImpl)('useExperiment', experimentName, options);
9
+ const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
10
+ return (0, react_1.useMemo)(() => {
11
+ if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(client)) {
12
+ return client.getExperiment(experimentName, options);
13
+ }
14
+ if ((options === null || options === void 0 ? void 0 : options.user) != null) {
15
+ return client.getExperiment(experimentName, options.user, options);
16
+ }
17
+ client_core_1.Log.warn(`useExperiment hook failed to find a valid Statsig client for experiment '${experimentName}'.`);
18
+ return NoopEvaluationsClient_1.NoopEvaluationsClient.getExperiment(experimentName, options);
19
+ }, [experimentName, client, renderVersion, options]);
19
20
  }
20
21
  exports.default = default_1;
@@ -0,0 +1,5 @@
1
+ import { FeatureGate, FeatureGateEvaluationOptions, StatsigUser } from '@statsig/client-core';
2
+ export type useFeatureGateOptions = FeatureGateEvaluationOptions & {
3
+ user: StatsigUser | null;
4
+ };
5
+ export default function (gateName: string, options?: useFeatureGateOptions): FeatureGate;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const client_core_1 = require("@statsig/client-core");
5
+ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
6
+ const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
7
+ const StatsigContext_1 = require("./StatsigContext");
8
+ function default_1(gateName, options) {
9
+ const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
10
+ const gate = (0, react_1.useMemo)(() => {
11
+ if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(client)) {
12
+ return client.getFeatureGate(gateName, options);
13
+ }
14
+ if ((options === null || options === void 0 ? void 0 : options.user) != null) {
15
+ return client.getFeatureGate(gateName, options.user, options);
16
+ }
17
+ client_core_1.Log.warn(`useFeatureGate hook failed to find a valid Statsig client for gate '${gateName}'.`);
18
+ return NoopEvaluationsClient_1.NoopEvaluationsClient.getFeatureGate(gateName, options);
19
+ }, [gateName, client, renderVersion, options]);
20
+ return gate;
21
+ }
22
+ exports.default = default_1;
@@ -0,0 +1,5 @@
1
+ import { FeatureGateEvaluationOptions, StatsigUser } from '@statsig/client-core';
2
+ export type useFeatureGateOptions = FeatureGateEvaluationOptions & {
3
+ user: StatsigUser | null;
4
+ };
5
+ export default function (gateName: string, options?: useFeatureGateOptions): boolean;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const client_core_1 = require("@statsig/client-core");
5
+ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
6
+ const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
7
+ const StatsigContext_1 = require("./StatsigContext");
8
+ function default_1(gateName, options) {
9
+ const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
10
+ const gate = (0, react_1.useMemo)(() => {
11
+ if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(client)) {
12
+ return client.checkGate(gateName, options);
13
+ }
14
+ if ((options === null || options === void 0 ? void 0 : options.user) != null) {
15
+ return client.checkGate(gateName, options.user, options);
16
+ }
17
+ client_core_1.Log.warn(`useFeatureGate hook failed to find a valid Statsig client for gate '${gateName}'.`);
18
+ return NoopEvaluationsClient_1.NoopEvaluationsClient.checkGate(gateName, options);
19
+ }, [gateName, client, renderVersion, options]);
20
+ return gate;
21
+ }
22
+ exports.default = default_1;
package/src/useLayer.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { EvaluationOptions, Layer, StatsigUser } from '@statsig/client-core';
2
- export type UseLayerOptions = EvaluationOptions & {
1
+ import { Layer, LayerEvaluationOptions, StatsigUser } from '@statsig/client-core';
2
+ export type UseLayerOptions = LayerEvaluationOptions & {
3
3
  user: StatsigUser | null;
4
4
  };
5
5
  export default function (layerName: string, options?: UseLayerOptions): Layer;
package/src/useLayer.js CHANGED
@@ -1,34 +1,22 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
- var react_1 = require("react");
15
- var client_core_1 = require("@statsig/client-core");
16
- var NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
17
- var OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
18
- var StatsigContext_1 = require("./StatsigContext");
3
+ const react_1 = require("react");
4
+ const client_core_1 = require("@statsig/client-core");
5
+ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
6
+ const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
7
+ const StatsigContext_1 = require("./StatsigContext");
19
8
  function default_1(layerName, options) {
20
- if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
21
- var client = (0, react_1.useContext)(StatsigContext_1.default).client;
22
- var layer = (0, react_1.useMemo)(function () {
23
- if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvaluationsClient)(client)) {
9
+ const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
10
+ const layer = (0, react_1.useMemo)(() => {
11
+ if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvalClient)(client)) {
24
12
  return client.getLayer(layerName, options);
25
13
  }
26
- if (options.user != null) {
14
+ if ((options === null || options === void 0 ? void 0 : options.user) != null) {
27
15
  return client.getLayer(layerName, options.user, options);
28
16
  }
29
- client_core_1.Log.warn("useLayer hook failed to find a valid Statsig client for layer '".concat(layerName, "'."));
17
+ client_core_1.Log.warn(`useLayer hook failed to find a valid Statsig client for layer '${layerName}'.`);
30
18
  return NoopEvaluationsClient_1.NoopEvaluationsClient.getLayer(layerName, options);
31
- }, [client.loadingStatus, options]);
19
+ }, [layerName, client, renderVersion, options]);
32
20
  return layer;
33
21
  }
34
22
  exports.default = default_1;
@@ -0,0 +1,6 @@
1
+ import { PrecomputedEvaluationsInterface } from '@statsig/client-core';
2
+ type HositedFuncs = Pick<PrecomputedEvaluationsInterface, 'checkGate' | 'getFeatureGate' | 'getDynamicConfig' | 'getExperiment' | 'getLayer' | 'logEvent'>;
3
+ export declare function useStatsigClient(): HositedFuncs & {
4
+ client: PrecomputedEvaluationsInterface;
5
+ };
6
+ export {};
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useStatsigClient = void 0;
4
+ const react_1 = require("react");
5
+ const client_core_1 = require("@statsig/client-core");
6
+ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
7
+ const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
8
+ const StatsigContext_1 = require("./StatsigContext");
9
+ function useStatsigClient() {
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
+ };
43
+ }
44
+ exports.useStatsigClient = useStatsigClient;
@@ -0,0 +1,6 @@
1
+ import { OnDeviceEvaluationsInterface } from '@statsig/client-core';
2
+ type HositedFuncs = Pick<OnDeviceEvaluationsInterface, 'checkGate' | 'getFeatureGate' | 'getDynamicConfig' | 'getExperiment' | 'getLayer' | 'logEvent'>;
3
+ export declare function useStatsigOnDeviceEvalClient(): HositedFuncs & {
4
+ client: OnDeviceEvaluationsInterface;
5
+ };
6
+ export {};
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useStatsigOnDeviceEvalClient = void 0;
4
+ const react_1 = require("react");
5
+ const client_core_1 = require("@statsig/client-core");
6
+ const NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
7
+ const OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
8
+ const StatsigContext_1 = require("./StatsigContext");
9
+ function useStatsigOnDeviceEvalClient() {
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
+ };
43
+ }
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
- export default function (): UseStatsigUserResult;
11
+ export declare function useStatsigUser(): UseStatsigUserResult;
12
+ export {};
@@ -1,18 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var useStatsigPrecomputedEvaluationsClient_1 = require("./useStatsigPrecomputedEvaluationsClient");
4
- function default_1() {
5
- var client = (0, useStatsigPrecomputedEvaluationsClient_1.default)();
3
+ exports.useStatsigUser = void 0;
4
+ const react_1 = require("react");
5
+ const StatsigContext_1 = require("./StatsigContext");
6
+ const useStatsigClient_1 = require("./useStatsigClient");
7
+ function useStatsigUser() {
8
+ const { client } = (0, useStatsigClient_1.useStatsigClient)();
9
+ const { renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
10
+ const deps = [client, renderVersion];
11
+ const memoUser = (0, react_1.useMemo)(() => {
12
+ const context = client.getContext();
13
+ return context.user;
14
+ }, deps);
6
15
  return {
7
- user: client.getCurrentUser(),
8
- updateUserSync: function (fn) {
9
- var user = fn(client.getCurrentUser());
10
- client.updateUserSync(user);
11
- },
12
- updateUserAsync: function (fn) {
13
- var user = fn(client.getCurrentUser());
14
- return client.updateUserAsync(user);
15
- },
16
+ user: memoUser,
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),
16
29
  };
17
30
  }
18
- exports.default = default_1;
31
+ exports.useStatsigUser = useStatsigUser;
@@ -1,5 +0,0 @@
1
- import { DynamicConfig, EvaluationOptions, StatsigUser } from '@statsig/client-core';
2
- export type UseConfigOptions = EvaluationOptions & {
3
- user: StatsigUser | null;
4
- };
5
- export declare function useConfigImpl(hook: 'useExperiment' | 'useDynamicConfig', configName: string, options?: UseConfigOptions): DynamicConfig;
@@ -1,36 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.useConfigImpl = void 0;
15
- var react_1 = require("react");
16
- var client_core_1 = require("@statsig/client-core");
17
- var NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
18
- var OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
19
- var StatsigContext_1 = require("./StatsigContext");
20
- function useConfigImpl(hook, configName, options) {
21
- if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
22
- var client = (0, react_1.useContext)(StatsigContext_1.default).client;
23
- var config = (0, react_1.useMemo)(function () {
24
- if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvaluationsClient)(client)) {
25
- return client.getDynamicConfig(configName, options);
26
- }
27
- if (options.user != null) {
28
- return client.getDynamicConfig(configName, options.user, options);
29
- }
30
- var type = hook === 'useDynamicConfig' ? 'dynamic config' : 'experiment';
31
- client_core_1.Log.warn("".concat(hook, " hook failed to find a valid Statsig client for ").concat(type, " '").concat(configName, "'."));
32
- return NoopEvaluationsClient_1.NoopEvaluationsClient.getDynamicConfig(configName, options);
33
- }, [client.loadingStatus, options]);
34
- return config;
35
- }
36
- exports.useConfigImpl = useConfigImpl;
package/src/useGate.d.ts DELETED
@@ -1,5 +0,0 @@
1
- import { EvaluationOptions, FeatureGate, StatsigUser } from '@statsig/client-core';
2
- export type UseGateOptions = EvaluationOptions & {
3
- user: StatsigUser | null;
4
- };
5
- export default function (gateName: string, options?: UseGateOptions): FeatureGate;
package/src/useGate.js DELETED
@@ -1,34 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- var react_1 = require("react");
15
- var client_core_1 = require("@statsig/client-core");
16
- var NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
17
- var OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
18
- var StatsigContext_1 = require("./StatsigContext");
19
- function default_1(gateName, options) {
20
- if (options === void 0) { options = __assign(__assign({}, client_core_1.DEFAULT_EVAL_OPTIONS), { user: null }); }
21
- var client = (0, react_1.useContext)(StatsigContext_1.default).client;
22
- var gate = (0, react_1.useMemo)(function () {
23
- if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvaluationsClient)(client)) {
24
- return client.getFeatureGate(gateName, options);
25
- }
26
- if (options.user != null) {
27
- return client.getFeatureGate(gateName, options.user, options);
28
- }
29
- client_core_1.Log.warn("useGate hook failed to find a valid Statsig client for gate '".concat(gateName, "'."));
30
- return NoopEvaluationsClient_1.NoopEvaluationsClient.getFeatureGate(gateName, options);
31
- }, [client.loadingStatus, options]);
32
- return gate;
33
- }
34
- exports.default = default_1;
@@ -1,2 +0,0 @@
1
- import { OnDeviceEvaluationsInterface } from '@statsig/client-core';
2
- export default function (): OnDeviceEvaluationsInterface;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var react_1 = require("react");
4
- var client_core_1 = require("@statsig/client-core");
5
- var NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
6
- var OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
7
- var StatsigContext_1 = require("./StatsigContext");
8
- function default_1() {
9
- var client = (0, react_1.useContext)(StatsigContext_1.default).client;
10
- if (!(0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvaluationsClient)(client)) {
11
- return client;
12
- }
13
- client_core_1.Log.warn('Attempting to retrive a Statsig OnDeviceEvaluationsClient but none was set.');
14
- return NoopEvaluationsClient_1.NoopEvaluationsClient;
15
- }
16
- exports.default = default_1;
@@ -1,2 +0,0 @@
1
- import { PrecomputedEvaluationsInterface } from '@statsig/client-core';
2
- export default function (): PrecomputedEvaluationsInterface;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var react_1 = require("react");
4
- var client_core_1 = require("@statsig/client-core");
5
- var NoopEvaluationsClient_1 = require("./NoopEvaluationsClient");
6
- var OnDeviceVsPrecomputedUtils_1 = require("./OnDeviceVsPrecomputedUtils");
7
- var StatsigContext_1 = require("./StatsigContext");
8
- function default_1() {
9
- var client = (0, react_1.useContext)(StatsigContext_1.default).client;
10
- if ((0, OnDeviceVsPrecomputedUtils_1.isPrecomputedEvaluationsClient)(client)) {
11
- return client;
12
- }
13
- client_core_1.Log.warn('Attempting to retrive a Statsig PrecomputedEvaluationsClient but none was set.');
14
- return NoopEvaluationsClient_1.NoopEvaluationsClient;
15
- }
16
- exports.default = default_1;