@statsig/react-bindings-on-device-eval 3.0.0-beta.1
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 +49 -0
- package/package.json +14 -0
- package/src/NoopOnDeviceEvalClient.js +71 -0
- package/src/StatsigContext.js +8 -0
- package/src/StatsigProviderOnDeviceEval.js +43 -0
- package/src/index.js +49 -0
- package/src/useDynamicConfig.js +17 -0
- package/src/useExperiment.js +17 -0
- package/src/useFeatureGate.js +18 -0
- package/src/useGateValue.js +18 -0
- package/src/useLayer.js +18 -0
- package/src/useOnDeviceClientAsyncInit.js +19 -0
- package/src/useOnDeviceClientBootstrapInit.js +15 -0
- package/src/useStatsigOnDeviceEvalClient.js +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Statsig helps you move faster with feature gates (feature flags), and/or dynamic configs. It also allows you to run A/B/n tests to validate your new features and understand their impact on your KPIs. If you're new to Statsig, check out our product and create an account at [statsig.com](https://www.statsig.com/?ref=gh_jsm).
|
|
2
|
+
|
|
3
|
+
<h1 align="center">
|
|
4
|
+
<a href="https://statsig.com/?ref=gh_jsm">
|
|
5
|
+
<img src="https://github.com/statsig-io/js-client-monorepo/assets/95646168/ae5499ed-20ff-4584-bf21-8857f800d485" />
|
|
6
|
+
</a>
|
|
7
|
+
<div />
|
|
8
|
+
<a href="https://statsig.com/?ref=gh_jsm">Statsig</a>
|
|
9
|
+
</h1>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://github.com/statsig-io/js-client-monorepo/blob/main/LICENSE">
|
|
13
|
+
<img src="https://img.shields.io/badge/license-ISC-blue.svg?colorA=1b2528&colorB=ccfbc7&style=for-the-badge">
|
|
14
|
+
</a>
|
|
15
|
+
<a href="https://www.npmjs.com/package/@statsig/js-client">
|
|
16
|
+
<img src="https://img.shields.io/npm/v/@statsig/js-client.svg?colorA=1b2528&colorB=b2d3ff&style=for-the-badge">
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://statsig.com/community?ref=gh_jsm">
|
|
19
|
+
<img src="https://img.shields.io/badge/slack-statsig-brightgreen.svg?logo=slack&colorA=1b2528&colorB=FFF8BA&style=for-the-badge">
|
|
20
|
+
</a>
|
|
21
|
+
</p>
|
|
22
|
+
|
|
23
|
+
## Getting Started
|
|
24
|
+
|
|
25
|
+
Read through the [Documentation](https://docs.statsig.com/client/javascript-sdk?ref=gh_jsm) or check out the [Samples](samples/).
|
|
26
|
+
|
|
27
|
+
## Packages
|
|
28
|
+
|
|
29
|
+
Clients
|
|
30
|
+
|
|
31
|
+
- Precomputed Evaluations (Recommended) [[npm](https://www.npmjs.com/package/@statsig/js-client)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/js-client)]
|
|
32
|
+
- On Device Evaluations [[npm](https://www.npmjs.com/package/@statsig/js-on-device-eval-client)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/js-on-device-eval-client)]
|
|
33
|
+
|
|
34
|
+
Product Bundles
|
|
35
|
+
|
|
36
|
+
- Session Replay [[npm](https://www.npmjs.com/package/@statsig/session-replay)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/session-replay)]
|
|
37
|
+
- Web Analytics [[npm](https://www.npmjs.com/package/@statsig/web-analytics)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/web-analytics)]
|
|
38
|
+
|
|
39
|
+
Framework Specific Bindings
|
|
40
|
+
|
|
41
|
+
- React [[npm](https://www.npmjs.com/package/@statsig/react-bindings)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/react-bindings)]
|
|
42
|
+
|
|
43
|
+
- React Native [[npm](https://www.npmjs.com/package/@statsig/react-native-bindings)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/react-native-bindings)]
|
|
44
|
+
|
|
45
|
+
- React Native (Expo) [[npm](https://www.npmjs.com/package/@statsig/expo-bindings)] [[source](https://github.com/statsig-io/js-client-monorepo/tree/main/packages/expo-bindings)]
|
|
46
|
+
|
|
47
|
+
## Community
|
|
48
|
+
|
|
49
|
+
If you need any assitance or just have a question, feel free to reach out to us on [Slack](https://statsig.com/community?ref=gh_jsm).
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@statsig/react-bindings-on-device-eval",
|
|
3
|
+
"version": "3.0.0-beta.1",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@statsig/client-core": "3.0.0-beta.1",
|
|
6
|
+
"@statsig/js-on-device-eval-client": "3.0.0-beta.1"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"react": "^16.6.3 || ^17.0.0 || ^18.0.0"
|
|
10
|
+
},
|
|
11
|
+
"type": "commonjs",
|
|
12
|
+
"main": "./src/index.js",
|
|
13
|
+
"typings": "./src/index.d.ts"
|
|
14
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNoopClient = exports.NoopOnDeviceEvalClient = void 0;
|
|
4
|
+
const client_core_1 = require("@statsig/client-core");
|
|
5
|
+
const _noop = () => {
|
|
6
|
+
// noop
|
|
7
|
+
};
|
|
8
|
+
const _noopAsync = () => Promise.resolve();
|
|
9
|
+
const NOOP_DETAILS = { reason: 'Error:NoClient' };
|
|
10
|
+
const _defaultEvaluation = (type) => {
|
|
11
|
+
return (...args) => {
|
|
12
|
+
const name = typeof args[0] === 'string' ? args[0] : args[1];
|
|
13
|
+
switch (type) {
|
|
14
|
+
case 'gate':
|
|
15
|
+
return (0, client_core_1._makeFeatureGate)(name, NOOP_DETAILS, null);
|
|
16
|
+
case 'config':
|
|
17
|
+
return (0, client_core_1._makeDynamicConfig)(name, NOOP_DETAILS, null);
|
|
18
|
+
case 'layer':
|
|
19
|
+
return (0, client_core_1._makeLayer)(name, NOOP_DETAILS, null);
|
|
20
|
+
case 'param_store':
|
|
21
|
+
return { name };
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
const _noopDataAdapter = {
|
|
26
|
+
attach: _noop,
|
|
27
|
+
getDataSync: () => null,
|
|
28
|
+
getDataAsync: () => Promise.resolve(null),
|
|
29
|
+
setData: _noop,
|
|
30
|
+
setDataLegacy: _noop,
|
|
31
|
+
prefetchData: _noopAsync,
|
|
32
|
+
};
|
|
33
|
+
const context = {
|
|
34
|
+
sdkKey: '',
|
|
35
|
+
options: {},
|
|
36
|
+
values: null,
|
|
37
|
+
user: { userID: '' },
|
|
38
|
+
errorBoundary: {},
|
|
39
|
+
session: {
|
|
40
|
+
data: { sessionID: '', startTime: 0, lastUpdate: 0 },
|
|
41
|
+
sdkKey: '',
|
|
42
|
+
},
|
|
43
|
+
stableID: '',
|
|
44
|
+
storageProvider: client_core_1.Storage,
|
|
45
|
+
};
|
|
46
|
+
const _client = {
|
|
47
|
+
isNoop: true,
|
|
48
|
+
loadingStatus: 'Uninitialized',
|
|
49
|
+
initializeSync: _noop,
|
|
50
|
+
initializeAsync: _noopAsync,
|
|
51
|
+
shutdown: _noopAsync,
|
|
52
|
+
flush: _noopAsync,
|
|
53
|
+
updateRuntimeOptions: _noop,
|
|
54
|
+
getContext: () => (Object.assign({}, context)),
|
|
55
|
+
checkGate: () => false,
|
|
56
|
+
getFeatureGate: _defaultEvaluation('gate'),
|
|
57
|
+
getDynamicConfig: _defaultEvaluation('config'),
|
|
58
|
+
getExperiment: _defaultEvaluation('config'),
|
|
59
|
+
getLayer: _defaultEvaluation('layer'),
|
|
60
|
+
logEvent: _noop,
|
|
61
|
+
on: _noop,
|
|
62
|
+
off: _noop,
|
|
63
|
+
$on: _noop,
|
|
64
|
+
$emt: _noop,
|
|
65
|
+
dataAdapter: _noopDataAdapter,
|
|
66
|
+
};
|
|
67
|
+
exports.NoopOnDeviceEvalClient = _client;
|
|
68
|
+
function isNoopClient(client) {
|
|
69
|
+
return 'isNoop' in client;
|
|
70
|
+
}
|
|
71
|
+
exports.isNoopClient = isNoopClient;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("react");
|
|
4
|
+
const NoopOnDeviceEvalClient_1 = require("./NoopOnDeviceEvalClient");
|
|
5
|
+
exports.default = (0, react_1.createContext)({
|
|
6
|
+
renderVersion: 0,
|
|
7
|
+
client: NoopOnDeviceEvalClient_1.NoopOnDeviceEvalClient,
|
|
8
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StatsigProviderOnDeviceEval = 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
|
+
const useOnDeviceClientAsyncInit_1 = require("./useOnDeviceClientAsyncInit");
|
|
9
|
+
function StatsigProviderOnDeviceEval(props) {
|
|
10
|
+
const [renderVersion, setRenderVersion] = (0, react_1.useState)(0);
|
|
11
|
+
const client = 'client' in props
|
|
12
|
+
? props.client
|
|
13
|
+
: (0, useOnDeviceClientAsyncInit_1.useOnDeviceClientAsyncInit)(props.sdkKey, props.options).client;
|
|
14
|
+
(0, react_1.useEffect)(() => {
|
|
15
|
+
const onValuesUpdated = () => {
|
|
16
|
+
setRenderVersion((v) => v + 1);
|
|
17
|
+
};
|
|
18
|
+
client_core_1.SDKType._setBindingType('react');
|
|
19
|
+
client.$on('values_updated', onValuesUpdated);
|
|
20
|
+
return () => {
|
|
21
|
+
client
|
|
22
|
+
.flush()
|
|
23
|
+
.catch((err) => client_core_1.Log.error('An error occured during flush', err));
|
|
24
|
+
client.off('values_updated', onValuesUpdated);
|
|
25
|
+
};
|
|
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 }));
|
|
31
|
+
}
|
|
32
|
+
exports.StatsigProviderOnDeviceEval = StatsigProviderOnDeviceEval;
|
|
33
|
+
function _isReady(client) {
|
|
34
|
+
if ('isNoop' in client) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
switch (client.loadingStatus) {
|
|
38
|
+
case 'Ready':
|
|
39
|
+
return true;
|
|
40
|
+
default:
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.useStatsigOnDeviceEvalClient = exports.useLayer = exports.useGateValue = exports.useFeatureGate = exports.useExperiment = exports.useDynamicConfig = exports.useOnDeviceClientBootstrapInit = exports.useOnDeviceClientAsyncInit = exports.StatsigProviderOnDeviceEval = exports.StatsigContext = void 0;
|
|
18
|
+
const StatsigContext_1 = require("./StatsigContext");
|
|
19
|
+
exports.StatsigContext = StatsigContext_1.default;
|
|
20
|
+
const StatsigProviderOnDeviceEval_1 = require("./StatsigProviderOnDeviceEval");
|
|
21
|
+
Object.defineProperty(exports, "StatsigProviderOnDeviceEval", { enumerable: true, get: function () { return StatsigProviderOnDeviceEval_1.StatsigProviderOnDeviceEval; } });
|
|
22
|
+
const useDynamicConfig_1 = require("./useDynamicConfig");
|
|
23
|
+
exports.useDynamicConfig = useDynamicConfig_1.default;
|
|
24
|
+
const useExperiment_1 = require("./useExperiment");
|
|
25
|
+
exports.useExperiment = useExperiment_1.default;
|
|
26
|
+
const useFeatureGate_1 = require("./useFeatureGate");
|
|
27
|
+
exports.useFeatureGate = useFeatureGate_1.default;
|
|
28
|
+
const useGateValue_1 = require("./useGateValue");
|
|
29
|
+
exports.useGateValue = useGateValue_1.default;
|
|
30
|
+
const useLayer_1 = require("./useLayer");
|
|
31
|
+
exports.useLayer = useLayer_1.default;
|
|
32
|
+
const useOnDeviceClientAsyncInit_1 = require("./useOnDeviceClientAsyncInit");
|
|
33
|
+
Object.defineProperty(exports, "useOnDeviceClientAsyncInit", { enumerable: true, get: function () { return useOnDeviceClientAsyncInit_1.useOnDeviceClientAsyncInit; } });
|
|
34
|
+
const useOnDeviceClientBootstrapInit_1 = require("./useOnDeviceClientBootstrapInit");
|
|
35
|
+
Object.defineProperty(exports, "useOnDeviceClientBootstrapInit", { enumerable: true, get: function () { return useOnDeviceClientBootstrapInit_1.useOnDeviceClientBootstrapInit; } });
|
|
36
|
+
const useStatsigOnDeviceEvalClient_1 = require("./useStatsigOnDeviceEvalClient");
|
|
37
|
+
Object.defineProperty(exports, "useStatsigOnDeviceEvalClient", { enumerable: true, get: function () { return useStatsigOnDeviceEvalClient_1.useStatsigOnDeviceEvalClient; } });
|
|
38
|
+
__exportStar(require("@statsig/client-core"), exports);
|
|
39
|
+
__exportStar(require("@statsig/js-on-device-eval-client"), exports);
|
|
40
|
+
__STATSIG__ = Object.assign(Object.assign({}, (__STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {})), { StatsigContext: StatsigContext_1.default,
|
|
41
|
+
StatsigProviderOnDeviceEval: StatsigProviderOnDeviceEval_1.StatsigProviderOnDeviceEval,
|
|
42
|
+
useOnDeviceClientAsyncInit: useOnDeviceClientAsyncInit_1.useOnDeviceClientAsyncInit,
|
|
43
|
+
useOnDeviceClientBootstrapInit: useOnDeviceClientBootstrapInit_1.useOnDeviceClientBootstrapInit,
|
|
44
|
+
useDynamicConfig: useDynamicConfig_1.default,
|
|
45
|
+
useExperiment: useExperiment_1.default,
|
|
46
|
+
useFeatureGate: useFeatureGate_1.default,
|
|
47
|
+
useGateValue: useGateValue_1.default,
|
|
48
|
+
useLayer: useLayer_1.default,
|
|
49
|
+
useStatsigOnDeviceEvalClient: useStatsigOnDeviceEvalClient_1.useStatsigOnDeviceEvalClient });
|
|
@@ -0,0 +1,17 @@
|
|
|
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 NoopOnDeviceEvalClient_1 = require("./NoopOnDeviceEvalClient");
|
|
6
|
+
const StatsigContext_1 = require("./StatsigContext");
|
|
7
|
+
function default_1(configName, user, options) {
|
|
8
|
+
const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
|
|
9
|
+
return (0, react_1.useMemo)(() => {
|
|
10
|
+
if ((0, NoopOnDeviceEvalClient_1.isNoopClient)(client)) {
|
|
11
|
+
client_core_1.Log.warn(`useDynamicConfig hook failed to find a valid StatsigOnDeviceEvalClient for dynamic config '${configName}'.`);
|
|
12
|
+
return NoopOnDeviceEvalClient_1.NoopOnDeviceEvalClient.getDynamicConfig(configName, user, options);
|
|
13
|
+
}
|
|
14
|
+
return client.getDynamicConfig(configName, user, options);
|
|
15
|
+
}, [configName, client, renderVersion, options]);
|
|
16
|
+
}
|
|
17
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,17 @@
|
|
|
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 NoopOnDeviceEvalClient_1 = require("./NoopOnDeviceEvalClient");
|
|
6
|
+
const StatsigContext_1 = require("./StatsigContext");
|
|
7
|
+
function default_1(experimentName, user, options) {
|
|
8
|
+
const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
|
|
9
|
+
return (0, react_1.useMemo)(() => {
|
|
10
|
+
if ((0, NoopOnDeviceEvalClient_1.isNoopClient)(client)) {
|
|
11
|
+
client_core_1.Log.warn(`useExperiment hook failed to find a valid StatsigOnDeviceEvalClient for experiment '${experimentName}'.`);
|
|
12
|
+
return NoopOnDeviceEvalClient_1.NoopOnDeviceEvalClient.getExperiment(experimentName, user, options);
|
|
13
|
+
}
|
|
14
|
+
return client.getExperiment(experimentName, user, options);
|
|
15
|
+
}, [experimentName, client, renderVersion, options]);
|
|
16
|
+
}
|
|
17
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,18 @@
|
|
|
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 NoopOnDeviceEvalClient_1 = require("./NoopOnDeviceEvalClient");
|
|
6
|
+
const StatsigContext_1 = require("./StatsigContext");
|
|
7
|
+
function default_1(gateName, user, options) {
|
|
8
|
+
const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
|
|
9
|
+
const gate = (0, react_1.useMemo)(() => {
|
|
10
|
+
if ((0, NoopOnDeviceEvalClient_1.isNoopClient)(client)) {
|
|
11
|
+
client_core_1.Log.warn(`useFeatureGate hook failed to find a valid StatsigOnDeviceEvalClient for gate '${gateName}'.`);
|
|
12
|
+
return NoopOnDeviceEvalClient_1.NoopOnDeviceEvalClient.getFeatureGate(gateName, user, options);
|
|
13
|
+
}
|
|
14
|
+
return client.getFeatureGate(gateName, user, options);
|
|
15
|
+
}, [gateName, client, renderVersion, options]);
|
|
16
|
+
return gate;
|
|
17
|
+
}
|
|
18
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,18 @@
|
|
|
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 NoopOnDeviceEvalClient_1 = require("./NoopOnDeviceEvalClient");
|
|
6
|
+
const StatsigContext_1 = require("./StatsigContext");
|
|
7
|
+
function default_1(gateName, user, options) {
|
|
8
|
+
const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
|
|
9
|
+
const gate = (0, react_1.useMemo)(() => {
|
|
10
|
+
if ((0, NoopOnDeviceEvalClient_1.isNoopClient)(client)) {
|
|
11
|
+
client_core_1.Log.warn(`useGateValue hook failed to find a valid StatsigOnDeviceEvalClient for gate '${gateName}'.`);
|
|
12
|
+
return NoopOnDeviceEvalClient_1.NoopOnDeviceEvalClient.checkGate(gateName, user, options);
|
|
13
|
+
}
|
|
14
|
+
return client.checkGate(gateName, user, options);
|
|
15
|
+
}, [gateName, client, renderVersion, options]);
|
|
16
|
+
return gate;
|
|
17
|
+
}
|
|
18
|
+
exports.default = default_1;
|
package/src/useLayer.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
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 NoopOnDeviceEvalClient_1 = require("./NoopOnDeviceEvalClient");
|
|
6
|
+
const StatsigContext_1 = require("./StatsigContext");
|
|
7
|
+
function default_1(layerName, user, options) {
|
|
8
|
+
const { client, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
|
|
9
|
+
const layer = (0, react_1.useMemo)(() => {
|
|
10
|
+
if ((0, NoopOnDeviceEvalClient_1.isNoopClient)(client)) {
|
|
11
|
+
client_core_1.Log.warn(`useLayer hook failed to find a valid StatsigOnDeviceEvalClient for layer '${layerName}'.`);
|
|
12
|
+
return NoopOnDeviceEvalClient_1.NoopOnDeviceEvalClient.getLayer(layerName, user, options);
|
|
13
|
+
}
|
|
14
|
+
return client.getLayer(layerName, user, options);
|
|
15
|
+
}, [layerName, client, renderVersion, options]);
|
|
16
|
+
return layer;
|
|
17
|
+
}
|
|
18
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOnDeviceClientAsyncInit = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const client_core_1 = require("@statsig/client-core");
|
|
6
|
+
const js_on_device_eval_client_1 = require("@statsig/js-on-device-eval-client");
|
|
7
|
+
function useOnDeviceClientAsyncInit(sdkKey, statsigOptions = null) {
|
|
8
|
+
const [isLoading, setIsLoading] = (0, react_1.useState)(true);
|
|
9
|
+
const [args] = (0, react_1.useState)(() => {
|
|
10
|
+
const client = new js_on_device_eval_client_1.StatsigOnDeviceEvalClient(sdkKey, statsigOptions);
|
|
11
|
+
client
|
|
12
|
+
.initializeAsync()
|
|
13
|
+
.catch(client_core_1.Log.error)
|
|
14
|
+
.finally(() => setIsLoading(false));
|
|
15
|
+
return { client, sdkKey };
|
|
16
|
+
});
|
|
17
|
+
return { client: args.client, isLoading };
|
|
18
|
+
}
|
|
19
|
+
exports.useOnDeviceClientAsyncInit = useOnDeviceClientAsyncInit;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOnDeviceClientBootstrapInit = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const js_on_device_eval_client_1 = require("@statsig/js-on-device-eval-client");
|
|
6
|
+
function useOnDeviceClientBootstrapInit(sdkKey, initialValues, statsigOptions = null) {
|
|
7
|
+
const [args] = (0, react_1.useState)(() => {
|
|
8
|
+
const client = new js_on_device_eval_client_1.StatsigOnDeviceEvalClient(sdkKey, statsigOptions);
|
|
9
|
+
client.dataAdapter.setData(initialValues);
|
|
10
|
+
client.initializeSync();
|
|
11
|
+
return { client, initialValues, sdkKey };
|
|
12
|
+
});
|
|
13
|
+
return args.client;
|
|
14
|
+
}
|
|
15
|
+
exports.useOnDeviceClientBootstrapInit = useOnDeviceClientBootstrapInit;
|
|
@@ -0,0 +1,59 @@
|
|
|
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 NoopOnDeviceEvalClient_1 = require("./NoopOnDeviceEvalClient");
|
|
7
|
+
const StatsigContext_1 = require("./StatsigContext");
|
|
8
|
+
function useStatsigOnDeviceEvalClient() {
|
|
9
|
+
const { client: anyClient, renderVersion } = (0, react_1.useContext)(StatsigContext_1.default);
|
|
10
|
+
const client = (0, react_1.useMemo)(() => {
|
|
11
|
+
if ((0, NoopOnDeviceEvalClient_1.isNoopClient)(anyClient)) {
|
|
12
|
+
client_core_1.Log.warn('Attempting to retrieve a StatsigOnDeviceEvalClient but none was set.');
|
|
13
|
+
return NoopOnDeviceEvalClient_1.NoopOnDeviceEvalClient;
|
|
14
|
+
}
|
|
15
|
+
return anyClient;
|
|
16
|
+
}, [anyClient, renderVersion]);
|
|
17
|
+
const deps = [client, renderVersion];
|
|
18
|
+
const checkGate = (0, react_1.useCallback)((name, user, options) => {
|
|
19
|
+
return client.checkGate(name, user, options);
|
|
20
|
+
}, deps);
|
|
21
|
+
const getFeatureGate = (0, react_1.useCallback)((name, user, options) => {
|
|
22
|
+
return client.getFeatureGate(name, user, options);
|
|
23
|
+
}, deps);
|
|
24
|
+
const getDynamicConfig = (0, react_1.useCallback)((name, user, options) => {
|
|
25
|
+
return client.getDynamicConfig(name, user, options);
|
|
26
|
+
}, deps);
|
|
27
|
+
const getExperiment = (0, react_1.useCallback)((name, user, options) => {
|
|
28
|
+
return client.getExperiment(name, user, options);
|
|
29
|
+
}, deps);
|
|
30
|
+
const getLayer = (0, react_1.useCallback)((name, options) => {
|
|
31
|
+
return client.getLayer(name, options);
|
|
32
|
+
}, deps);
|
|
33
|
+
const logEvent = (0, react_1.useCallback)((eventName, user, value, metadata) => {
|
|
34
|
+
if (typeof eventName === 'string') {
|
|
35
|
+
return client.logEvent(eventName, user, value, metadata);
|
|
36
|
+
}
|
|
37
|
+
return client.logEvent(eventName, user);
|
|
38
|
+
}, deps);
|
|
39
|
+
return (0, react_1.useMemo)(() => {
|
|
40
|
+
return {
|
|
41
|
+
client,
|
|
42
|
+
checkGate,
|
|
43
|
+
getFeatureGate,
|
|
44
|
+
getDynamicConfig,
|
|
45
|
+
getExperiment,
|
|
46
|
+
getLayer,
|
|
47
|
+
logEvent,
|
|
48
|
+
};
|
|
49
|
+
}, [
|
|
50
|
+
client,
|
|
51
|
+
checkGate,
|
|
52
|
+
getFeatureGate,
|
|
53
|
+
getDynamicConfig,
|
|
54
|
+
getExperiment,
|
|
55
|
+
getLayer,
|
|
56
|
+
logEvent,
|
|
57
|
+
]);
|
|
58
|
+
}
|
|
59
|
+
exports.useStatsigOnDeviceEvalClient = useStatsigOnDeviceEvalClient;
|