@flagship.io/react-sdk 2.2.0-beta.6 → 3.0.2-alpha.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.
Files changed (48) hide show
  1. package/README.md +2 -2
  2. package/dist/FlagshipContext.d.ts +99 -0
  3. package/dist/FlagshipContext.js +312 -0
  4. package/dist/FlagshipContext.js.map +1 -0
  5. package/dist/FlagshipErrorBoundary.d.ts +34 -0
  6. package/dist/FlagshipErrorBoundary.js +95 -0
  7. package/dist/FlagshipErrorBoundary.js.map +1 -0
  8. package/dist/FlagshipHooks.d.ts +33 -0
  9. package/dist/FlagshipHooks.js +198 -0
  10. package/dist/FlagshipHooks.js.map +1 -0
  11. package/dist/components/ReactErrorBoundaryContainer/index.d.ts +10 -0
  12. package/dist/components/ReactErrorBoundaryContainer/index.js +65 -0
  13. package/dist/components/ReactErrorBoundaryContainer/index.js.map +1 -0
  14. package/dist/{types/index.d.ts → index.d.ts} +0 -1
  15. package/dist/index.js +14 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/lib/loggerHelper.d.ts +5 -0
  18. package/dist/lib/loggerHelper.js +14 -0
  19. package/dist/lib/loggerHelper.js.map +1 -0
  20. package/dist/lib/utils.d.ts +1 -0
  21. package/dist/lib/utils.js +7 -0
  22. package/dist/lib/utils.js.map +1 -0
  23. package/dist/setupTests.d.ts +1 -0
  24. package/dist/setupTests.js +9 -0
  25. package/dist/setupTests.js.map +1 -0
  26. package/dist/tests/mock/apiAnswers/oneModifInMoreThanOneCampaign.d.ts +3 -0
  27. package/dist/tests/mock/apiAnswers/oneModifInMoreThanOneCampaign.js +51 -0
  28. package/dist/tests/mock/apiAnswers/oneModifInMoreThanOneCampaign.js.map +1 -0
  29. package/dist/tests/mock/env.d.ts +5 -0
  30. package/dist/tests/mock/env.js +8 -0
  31. package/dist/tests/mock/env.js.map +1 -0
  32. package/dist/tests/mock/index.d.ts +74 -0
  33. package/dist/tests/mock/index.js +90 -0
  34. package/dist/tests/mock/index.js.map +1 -0
  35. package/package.json +2 -2
  36. package/dist/es/FlagshipContext.js +0 -243
  37. package/dist/es/FlagshipHooks.js +0 -341
  38. package/dist/es/index.js +0 -3
  39. package/dist/es/utils.js +0 -46
  40. package/dist/esm/FlagshipContext.js +0 -265
  41. package/dist/esm/FlagshipHooks.js +0 -481
  42. package/dist/esm/index.js +0 -3
  43. package/dist/esm/utils.js +0 -46
  44. package/dist/index.browser.js +0 -2
  45. package/dist/index.browser.js.map +0 -1
  46. package/dist/types/FlagshipContext.d.ts +0 -79
  47. package/dist/types/FlagshipHooks.d.ts +0 -91
  48. package/dist/types/utils.d.ts +0 -5
@@ -0,0 +1,5 @@
1
+ import { LogConfig, FsLogger } from '@flagship.io/js-sdk-logs';
2
+ declare const FsLogger: {
3
+ getLogger: (config?: LogConfig) => FsLogger;
4
+ };
5
+ export default FsLogger;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var js_sdk_logs_1 = __importDefault(require("@flagship.io/js-sdk-logs"));
7
+ var FsLogger = {
8
+ getLogger: function (config) {
9
+ if (config === void 0) { config = { enableConsoleLogs: false, nodeEnv: 'unknown' }; }
10
+ return js_sdk_logs_1.default.getLogger(config, 'Flagship React SDK');
11
+ }
12
+ };
13
+ exports.default = FsLogger;
14
+ //# sourceMappingURL=loggerHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loggerHelper.js","sourceRoot":"","sources":["../../src/lib/loggerHelper.ts"],"names":[],"mappings":";;;;;AAAA,yEAA+E;AAE/E,IAAM,QAAQ,GAAG;IACb,SAAS,EAAE,UAAC,MAAoE;QAApE,uBAAA,EAAA,WAAsB,iBAAiB,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE;QAC5E,OAAO,qBAAc,CAAC,SAAS,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC;IAClE,CAAC;CACJ,CAAC;AAEF,kBAAe,QAAQ,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const areWeTestingWithJest: () => boolean;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.areWeTestingWithJest = void 0;
4
+ exports.areWeTestingWithJest = function () {
5
+ return process.env.JEST_WORKER_ID !== undefined;
6
+ };
7
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/lib/utils.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAAG;IAChC,OAAO,OAAO,CAAC,GAAG,CAAC,cAAc,KAAK,SAAS,CAAC;AACpD,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var enzyme_1 = require("enzyme");
7
+ var enzyme_adapter_react_16_1 = __importDefault(require("enzyme-adapter-react-16"));
8
+ enzyme_1.configure({ adapter: new enzyme_adapter_react_16_1.default() });
9
+ //# sourceMappingURL=setupTests.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setupTests.js","sourceRoot":"","sources":["../src/setupTests.ts"],"names":[],"mappings":";;;;;AAAA,iCAAmC;AACnC,oFAA8C;AAE9C,kBAAS,CAAC,EAAE,OAAO,EAAE,IAAI,iCAAO,EAAE,EAAE,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { DecisionApiResponseData } from '@flagship.io/js-sdk';
2
+ declare const oneModifInMoreThanOneCampaign: DecisionApiResponseData;
3
+ export default oneModifInMoreThanOneCampaign;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var env_1 = require("../env");
4
+ var oneModifInMoreThanOneCampaign = {
5
+ visitorId: env_1.vId,
6
+ campaigns: [
7
+ {
8
+ id: 'blntcamqmdvg04g371f0',
9
+ variationGroupId: 'blntcamqmdvg04g371h0',
10
+ variation: {
11
+ id: 'blntcamqmdvg04g371hg',
12
+ modifications: {
13
+ type: 'FLAG',
14
+ value: {
15
+ psp: 'dalenys'
16
+ }
17
+ }
18
+ }
19
+ },
20
+ {
21
+ id: 'bmjdprsjan0g01uq2crg',
22
+ variationGroupId: 'bmjdprsjan0g01uq2csg',
23
+ variation: {
24
+ id: 'bmjdprsjan0g01uq2ctg',
25
+ modifications: {
26
+ type: 'JSON',
27
+ value: {
28
+ algorithmVersion: 'new'
29
+ }
30
+ }
31
+ }
32
+ },
33
+ {
34
+ id: 'bmjdprsjan0g01uq2ceg',
35
+ variationGroupId: 'bmjdprsjan0g01uq2ceg',
36
+ variation: {
37
+ id: 'bmjdprsjan0g01uq1ctg',
38
+ modifications: {
39
+ type: 'JSON',
40
+ value: {
41
+ algorithmVersion: 'yolo2',
42
+ psp: 'yolo',
43
+ hello: 'world'
44
+ }
45
+ }
46
+ }
47
+ }
48
+ ]
49
+ };
50
+ exports.default = oneModifInMoreThanOneCampaign;
51
+ //# sourceMappingURL=oneModifInMoreThanOneCampaign.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oneModifInMoreThanOneCampaign.js","sourceRoot":"","sources":["../../../../src/tests/mock/apiAnswers/oneModifInMoreThanOneCampaign.tsx"],"names":[],"mappings":";;AACA,8BAA6B;AAE7B,IAAM,6BAA6B,GAA4B;IAC3D,SAAS,EAAE,SAAG;IACd,SAAS,EAAE;QACP;YACI,EAAE,EAAE,sBAAsB;YAC1B,gBAAgB,EAAE,sBAAsB;YACxC,SAAS,EAAE;gBACP,EAAE,EAAE,sBAAsB;gBAC1B,aAAa,EAAE;oBACX,IAAI,EAAE,MAAM;oBACZ,KAAK,EAAE;wBACH,GAAG,EAAE,SAAS;qBACjB;iBACJ;aACJ;SACJ;QACD;YACI,EAAE,EAAE,sBAAsB;YAC1B,gBAAgB,EAAE,sBAAsB;YACxC,SAAS,EAAE;gBACP,EAAE,EAAE,sBAAsB;gBAC1B,aAAa,EAAE;oBACX,IAAI,EAAE,MAAM;oBACZ,KAAK,EAAE;wBACH,gBAAgB,EAAE,KAAK;qBAC1B;iBACJ;aACJ;SACJ;QACD;YACI,EAAE,EAAE,sBAAsB;YAC1B,gBAAgB,EAAE,sBAAsB;YACxC,SAAS,EAAE;gBACP,EAAE,EAAE,sBAAsB;gBAC1B,aAAa,EAAE;oBACX,IAAI,EAAE,MAAM;oBACZ,KAAK,EAAE;wBACH,gBAAgB,EAAE,OAAO;wBACzB,GAAG,EAAE,MAAM;wBACX,KAAK,EAAE,OAAO;qBACjB;iBACJ;aACJ;SACJ;KACJ;CACJ,CAAC;AAEF,kBAAe,6BAA6B,CAAC"}
@@ -0,0 +1,5 @@
1
+ export declare const vId = "test-vid";
2
+ declare const _default: {
3
+ vId: string;
4
+ };
5
+ export default _default;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vId = void 0;
4
+ exports.vId = 'test-vid';
5
+ exports.default = {
6
+ vId: exports.vId
7
+ };
8
+ //# sourceMappingURL=env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.js","sourceRoot":"","sources":["../../../src/tests/mock/env.tsx"],"names":[],"mappings":";;;AAAa,QAAA,GAAG,GAAG,UAAU,CAAC;AAE9B,kBAAe;IACX,GAAG,aAAA;CACN,CAAC"}
@@ -0,0 +1,74 @@
1
+ import React from 'react';
2
+ import { FlagshipSdkConfig } from '@flagship.io/js-sdk';
3
+ import { FsState } from '../../FlagshipContext';
4
+ export declare const apiAnswers: {
5
+ oneModifInMoreThanOneCampaign: import("@flagship.io/js-sdk").DecisionApiResponseData;
6
+ };
7
+ export declare const defaultConfig: FlagshipSdkConfig;
8
+ export declare const DefaultLoadingComponent: React.SFC;
9
+ export declare const defaultContext: {
10
+ hasError: boolean;
11
+ state: FsState;
12
+ };
13
+ export declare const anonymousIdForTest = "anonymousTest";
14
+ export declare const providerProps: {
15
+ visitorData: {
16
+ id: string;
17
+ context: {
18
+ isAuth: boolean;
19
+ isVip: boolean;
20
+ };
21
+ };
22
+ config: {
23
+ fetchNow: boolean;
24
+ enableConsoleLogs: boolean;
25
+ enableSafeMode: boolean;
26
+ enableErrorLayout: boolean;
27
+ nodeEnv: string;
28
+ apiKey: string;
29
+ };
30
+ envId: string;
31
+ };
32
+ export declare const fetchedModifications: ({
33
+ id: string;
34
+ slug: null;
35
+ type: string;
36
+ variation: {
37
+ id: string;
38
+ modifications: {
39
+ type: string;
40
+ value: {
41
+ array: number[];
42
+ complex: {
43
+ carray: {
44
+ cobject: number;
45
+ }[];
46
+ };
47
+ object: {
48
+ value: number;
49
+ };
50
+ discount?: undefined;
51
+ };
52
+ };
53
+ reference: boolean;
54
+ };
55
+ variationGroupId: string;
56
+ } | {
57
+ id: string;
58
+ slug: null;
59
+ type: string;
60
+ variation: {
61
+ id: string;
62
+ modifications: {
63
+ type: string;
64
+ value: {
65
+ discount: string;
66
+ array?: undefined;
67
+ complex?: undefined;
68
+ object?: undefined;
69
+ };
70
+ };
71
+ reference: boolean;
72
+ };
73
+ variationGroupId: string;
74
+ })[];
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.fetchedModifications = exports.providerProps = exports.anonymousIdForTest = exports.defaultContext = exports.DefaultLoadingComponent = exports.defaultConfig = exports.apiAnswers = void 0;
7
+ var react_1 = __importDefault(require("react"));
8
+ var oneModifInMoreThanOneCampaign_1 = __importDefault(require("./apiAnswers/oneModifInMoreThanOneCampaign"));
9
+ var env_1 = require("./env");
10
+ exports.apiAnswers = {
11
+ oneModifInMoreThanOneCampaign: oneModifInMoreThanOneCampaign_1.default
12
+ };
13
+ exports.defaultConfig = {};
14
+ exports.DefaultLoadingComponent = function () { return react_1.default.createElement("div", null, "Loading Flagship React SDK"); };
15
+ exports.defaultContext = {
16
+ hasError: false,
17
+ state: {
18
+ fsSdk: null,
19
+ fsVisitor: null,
20
+ log: null,
21
+ fsModifications: null,
22
+ status: {
23
+ isSdkReady: false,
24
+ isLoading: true,
25
+ isVisitorDefined: false,
26
+ lastRefresh: null,
27
+ firstInitSuccess: null,
28
+ hasError: false
29
+ },
30
+ private: {
31
+ previousFetchedModifications: undefined
32
+ }
33
+ }
34
+ };
35
+ exports.anonymousIdForTest = 'anonymousTest';
36
+ exports.providerProps = {
37
+ visitorData: {
38
+ id: env_1.vId,
39
+ context: { isAuth: true, isVip: false }
40
+ },
41
+ config: {
42
+ fetchNow: true,
43
+ enableConsoleLogs: true,
44
+ enableSafeMode: true,
45
+ enableErrorLayout: true,
46
+ nodeEnv: 'production',
47
+ apiKey: 'M2FYdfXsJ12tjJQuadw7y9DZojqNGBvecpjGXY93'
48
+ },
49
+ envId: 'bn1ab7m56qolupi5sa0g'
50
+ };
51
+ exports.fetchedModifications = [
52
+ {
53
+ id: 'bsq046crms2g1jsvtb20',
54
+ slug: null,
55
+ type: 'ab',
56
+ variation: {
57
+ id: 'bsq046crms2g1jsvtb40',
58
+ modifications: {
59
+ type: 'JSON',
60
+ value: {
61
+ array: [1, 2, 3],
62
+ complex: {
63
+ carray: [
64
+ {
65
+ cobject: 0
66
+ }
67
+ ]
68
+ },
69
+ object: {
70
+ value: 123456
71
+ }
72
+ }
73
+ },
74
+ reference: false
75
+ },
76
+ variationGroupId: 'bsq046crms2g1jsvtb30'
77
+ },
78
+ {
79
+ id: 'bqjfstuirtfg01mctmn0',
80
+ slug: null,
81
+ type: 'perso',
82
+ variation: {
83
+ id: 'bqjfstuirtfg01mctmp0',
84
+ modifications: { type: 'JSON', value: { discount: '10%' } },
85
+ reference: false
86
+ },
87
+ variationGroupId: 'bqjfstuirtfg01mctmo0'
88
+ }
89
+ ];
90
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/tests/mock/index.tsx"],"names":[],"mappings":";;;;;;AAAA,gDAAwD;AAIxD,6GAAuF;AACvF,6BAA4B;AAEf,QAAA,UAAU,GAAG;IAEtB,6BAA6B,yCAAA;CAChC,CAAC;AAEW,QAAA,aAAa,GAAsB,EAE/C,CAAC;AAEW,QAAA,uBAAuB,GAAc,cAAM,OAAA,wEAAqC,EAArC,CAAqC,CAAC;AAEjF,QAAA,cAAc,GAGvB;IACA,QAAQ,EAAE,KAAK;IACf,KAAK,EAAE;QACH,KAAK,EAAE,IAAI;QACX,SAAS,EAAE,IAAI;QACf,GAAG,EAAE,IAAI;QACT,eAAe,EAAE,IAAI;QACrB,MAAM,EAAE;YACJ,UAAU,EAAE,KAAK;YACjB,SAAS,EAAE,IAAI;YACf,gBAAgB,EAAE,KAAK;YACvB,WAAW,EAAE,IAAI;YACjB,gBAAgB,EAAE,IAAI;YACtB,QAAQ,EAAE,KAAK;SAClB;QACD,OAAO,EAAE;YACL,4BAA4B,EAAE,SAAS;SAC1C;KACJ;CACJ,CAAC;AAEW,QAAA,kBAAkB,GAAG,eAAe,CAAC;AAErC,QAAA,aAAa,GAAG;IACzB,WAAW,EAAE;QACT,EAAE,EAAE,SAAG;QACP,OAAO,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE;KAC1C;IACD,MAAM,EAAE;QACJ,QAAQ,EAAE,IAAI;QACd,iBAAiB,EAAE,IAAI;QACvB,cAAc,EAAE,IAAI;QACpB,iBAAiB,EAAE,IAAI;QACvB,OAAO,EAAE,YAAY;QACrB,MAAM,EAAE,0CAA0C;KACrD;IACD,KAAK,EAAE,sBAAsB;CAChC,CAAC;AAEW,QAAA,oBAAoB,GAAG;IAChC;QACI,EAAE,EAAE,sBAAsB;QAC1B,IAAI,EAAE,IAAI;QACV,IAAI,EAAE,IAAI;QACV,SAAS,EAAE;YACP,EAAE,EAAE,sBAAsB;YAC1B,aAAa,EAAE;gBACX,IAAI,EAAE,MAAM;gBACZ,KAAK,EAAE;oBACH,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;oBAChB,OAAO,EAAE;wBACL,MAAM,EAAE;4BACJ;gCACI,OAAO,EAAE,CAAC;6BACb;yBACJ;qBACJ;oBACD,MAAM,EAAE;wBACJ,KAAK,EAAE,MAAM;qBAChB;iBACJ;aACJ;YACD,SAAS,EAAE,KAAK;SACnB;QACD,gBAAgB,EAAE,sBAAsB;KAC3C;IACD;QACI,EAAE,EAAE,sBAAsB;QAC1B,IAAI,EAAE,IAAI;QACV,IAAI,EAAE,OAAO;QACb,SAAS,EAAE;YACP,EAAE,EAAE,sBAAsB;YAC1B,aAAa,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE;YAC3D,SAAS,EAAE,KAAK;SACnB;QACD,gBAAgB,EAAE,sBAAsB;KAC3C;CACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flagship.io/react-sdk",
3
- "version": "2.2.0-beta.6",
3
+ "version": "3.0.2-alpha.0",
4
4
  "description": "Flagship REACT SDK",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -11,7 +11,7 @@
11
11
  "README.md"
12
12
  ],
13
13
  "dependencies": {
14
- "@flagship.io/js-sdk": "^2.3.1-beta.9",
14
+ "@flagship.io/js-sdk": "^3.0.5-alpha.0",
15
15
  "encoding": "^0.1.13"
16
16
  },
17
17
  "peerDependencies": {
@@ -1,243 +0,0 @@
1
- // eslint-disable-next-line no-use-before-define
2
- import React, { useState, useEffect, createContext, useRef } from 'react';
3
- import { Flagship, FlagshipStatus } from '@flagship.io/js-sdk';
4
- import { getModificationsFromCampaigns, logError } from './utils';
5
- const initStat = {
6
- status: {
7
- isLoading: true,
8
- isSdkReady: false
9
- }
10
- };
11
- export const FlagshipContext = /*#__PURE__*/createContext({
12
- state: { ...initStat
13
- }
14
- });
15
- export const FlagshipProvider = ({
16
- children,
17
- fetchNow,
18
- envId,
19
- apiKey,
20
- decisionMode,
21
- decisionApiUrl,
22
- timeout,
23
- logLevel,
24
- statusChangedCallback,
25
- logManager,
26
- pollingInterval,
27
- visitorData,
28
- onInitStart,
29
- onInitDone,
30
- onBucketingSuccess,
31
- onBucketingFail,
32
- loadingComponent,
33
- onBucketingUpdated,
34
- onUpdate,
35
- enableClientCache,
36
- initialBucketing,
37
- initialCampaigns,
38
- initialModifications,
39
- synchronizeOnBucketingUpdated,
40
- activateDeduplicationTime,
41
- hitDeduplicationTime,
42
- visitorCacheImplementation,
43
- hitCacheImplementation,
44
- disableCache
45
- }) => {
46
- let modifications = new Map();
47
-
48
- if (initialModifications) {
49
- initialModifications.forEach(modification => {
50
- modifications.set(modification.key, modification);
51
- });
52
- } else if (initialCampaigns) {
53
- modifications = getModificationsFromCampaigns(initialCampaigns);
54
- }
55
-
56
- const [state, setState] = useState({ ...initStat,
57
- modifications
58
- });
59
- const [lastModified, setLastModified] = useState();
60
- const stateRef = useRef();
61
- stateRef.current = state;
62
- useEffect(() => {
63
- if (synchronizeOnBucketingUpdated) {
64
- var _state$visitor;
65
-
66
- (_state$visitor = state.visitor) === null || _state$visitor === void 0 ? void 0 : _state$visitor.synchronizeModifications();
67
- }
68
- }, [lastModified]);
69
- useEffect(() => {
70
- updateVisitor();
71
- }, [JSON.stringify(visitorData)]);
72
- useEffect(() => {
73
- initSdk();
74
- }, [envId, apiKey, decisionMode]);
75
-
76
- const updateVisitor = () => {
77
- if (!state.visitor) {
78
- return;
79
- }
80
-
81
- if (visitorData.context) {
82
- state.visitor.clearContext();
83
- state.visitor.updateContext(visitorData.context);
84
- }
85
-
86
- if (typeof visitorData.hasConsented === 'boolean') {
87
- state.visitor.setConsent(visitorData.hasConsented);
88
- }
89
-
90
- if (state.visitor.anonymousId && !visitorData.isAuthenticated) {
91
- state.visitor.unauthenticate();
92
- } else if (!state.visitor.anonymousId && visitorData.isAuthenticated) {
93
- state.visitor.authenticate(visitorData.id);
94
- }
95
-
96
- if (visitorData.id) {
97
- state.visitor.visitorId = visitorData.id;
98
- }
99
-
100
- state.visitor.synchronizeModifications();
101
- };
102
-
103
- function initializeState(param) {
104
- const newStatus = {
105
- isSdkReady: param.isSdkReady,
106
- isLoading: param.isLoading,
107
- isVisitorDefined: !!param.fsVisitor,
108
- lastRefresh: new Date().toISOString()
109
- };
110
- setState(currentState => {
111
- if (!currentState.status.firstInitSuccess) {
112
- newStatus.firstInitSuccess = new Date().toISOString();
113
- }
114
-
115
- return { ...currentState,
116
- visitor: param.fsVisitor,
117
- modifications: param.fsVisitor.modifications,
118
- config: Flagship.getConfig(),
119
- status: { ...currentState.status,
120
- ...newStatus
121
- }
122
- };
123
- });
124
- return newStatus;
125
- } // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
-
127
-
128
- const onVisitorReady = (fsVisitor, error) => {
129
- if (error) {
130
- logError(Flagship.getConfig(), error.message || error, 'onReady');
131
- return;
132
- }
133
-
134
- const newStatus = initializeState({
135
- fsVisitor,
136
- isSdkReady: true,
137
- isLoading: false
138
- });
139
-
140
- if (onUpdate) {
141
- onUpdate({
142
- fsModifications: fsVisitor.modifications,
143
- config: Flagship.getConfig(),
144
- status: newStatus
145
- });
146
- }
147
- };
148
-
149
- const statusChanged = status => {
150
- if (statusChangedCallback) {
151
- statusChangedCallback(status);
152
- }
153
-
154
- if (status === FlagshipStatus.STARTING && onInitStart) {
155
- onInitStart();
156
- } else if (status === FlagshipStatus.READY_PANIC_ON || status === FlagshipStatus.READY) {
157
- var _stateRef$current;
158
-
159
- if (onInitDone) {
160
- onInitDone();
161
- }
162
-
163
- if ((_stateRef$current = stateRef.current) !== null && _stateRef$current !== void 0 && _stateRef$current.visitor) {
164
- var _stateRef$current2;
165
-
166
- (_stateRef$current2 = stateRef.current) === null || _stateRef$current2 === void 0 ? void 0 : _stateRef$current2.visitor.synchronizeModifications();
167
- } else {
168
- const fsVisitor = Flagship.newVisitor({
169
- visitorId: visitorData.id,
170
- context: visitorData.context,
171
- isAuthenticated: visitorData.isAuthenticated,
172
- hasConsented: visitorData.hasConsented,
173
- initialCampaigns,
174
- initialModifications
175
- });
176
- fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.on('ready', error => {
177
- onVisitorReady(fsVisitor, error);
178
- });
179
-
180
- if (!fetchNow && fsVisitor) {
181
- initializeState({
182
- fsVisitor,
183
- isSdkReady: true,
184
- isLoading: false
185
- });
186
- }
187
- }
188
- }
189
- };
190
-
191
- const onBucketingLastModified = lastUpdate => {
192
- if (onBucketingUpdated) {
193
- onBucketingUpdated(lastUpdate);
194
- }
195
-
196
- setLastModified(lastUpdate);
197
- };
198
-
199
- const initSdk = () => {
200
- Flagship.start(envId, apiKey, {
201
- decisionMode,
202
- fetchNow,
203
- timeout,
204
- logLevel,
205
- statusChangedCallback: statusChanged,
206
- logManager,
207
- pollingInterval,
208
- onBucketingFail,
209
- onBucketingSuccess,
210
- enableClientCache,
211
- decisionApiUrl,
212
- onBucketingUpdated: onBucketingLastModified,
213
- initialBucketing,
214
- activateDeduplicationTime,
215
- hitDeduplicationTime,
216
- visitorCacheImplementation,
217
- hitCacheImplementation,
218
- disableCache
219
- });
220
- };
221
-
222
- const handleDisplay = () => {
223
- const isFirstInit = !state.visitor;
224
-
225
- if (state.status.isLoading && loadingComponent && isFirstInit) {
226
- return /*#__PURE__*/React.createElement(React.Fragment, null, loadingComponent);
227
- }
228
-
229
- return /*#__PURE__*/React.createElement(React.Fragment, null, children);
230
- };
231
-
232
- return /*#__PURE__*/React.createElement(FlagshipContext.Provider, {
233
- value: {
234
- state,
235
- setState
236
- }
237
- }, handleDisplay());
238
- };
239
- FlagshipProvider.defaultProps = {
240
- nodeEnv: 'production',
241
- activateDeduplicationTime: 10,
242
- hitDeduplicationTime: 10
243
- };