@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.
- package/README.md +2 -2
- package/dist/FlagshipContext.d.ts +99 -0
- package/dist/FlagshipContext.js +312 -0
- package/dist/FlagshipContext.js.map +1 -0
- package/dist/FlagshipErrorBoundary.d.ts +34 -0
- package/dist/FlagshipErrorBoundary.js +95 -0
- package/dist/FlagshipErrorBoundary.js.map +1 -0
- package/dist/FlagshipHooks.d.ts +33 -0
- package/dist/FlagshipHooks.js +198 -0
- package/dist/FlagshipHooks.js.map +1 -0
- package/dist/components/ReactErrorBoundaryContainer/index.d.ts +10 -0
- package/dist/components/ReactErrorBoundaryContainer/index.js +65 -0
- package/dist/components/ReactErrorBoundaryContainer/index.js.map +1 -0
- package/dist/{types/index.d.ts → index.d.ts} +0 -1
- package/dist/index.js +14 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/loggerHelper.d.ts +5 -0
- package/dist/lib/loggerHelper.js +14 -0
- package/dist/lib/loggerHelper.js.map +1 -0
- package/dist/lib/utils.d.ts +1 -0
- package/dist/lib/utils.js +7 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/setupTests.d.ts +1 -0
- package/dist/setupTests.js +9 -0
- package/dist/setupTests.js.map +1 -0
- package/dist/tests/mock/apiAnswers/oneModifInMoreThanOneCampaign.d.ts +3 -0
- package/dist/tests/mock/apiAnswers/oneModifInMoreThanOneCampaign.js +51 -0
- package/dist/tests/mock/apiAnswers/oneModifInMoreThanOneCampaign.js.map +1 -0
- package/dist/tests/mock/env.d.ts +5 -0
- package/dist/tests/mock/env.js +8 -0
- package/dist/tests/mock/env.js.map +1 -0
- package/dist/tests/mock/index.d.ts +74 -0
- package/dist/tests/mock/index.js +90 -0
- package/dist/tests/mock/index.js.map +1 -0
- package/package.json +2 -2
- package/dist/es/FlagshipContext.js +0 -243
- package/dist/es/FlagshipHooks.js +0 -341
- package/dist/es/index.js +0 -3
- package/dist/es/utils.js +0 -46
- package/dist/esm/FlagshipContext.js +0 -265
- package/dist/esm/FlagshipHooks.js +0 -481
- package/dist/esm/index.js +0 -3
- package/dist/esm/utils.js +0 -46
- package/dist/index.browser.js +0 -2
- package/dist/index.browser.js.map +0 -1
- package/dist/types/FlagshipContext.d.ts +0 -79
- package/dist/types/FlagshipHooks.d.ts +0 -91
- package/dist/types/utils.d.ts +0 -5
package/dist/es/FlagshipHooks.js
DELETED
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
import { useContext } from 'react';
|
|
2
|
-
import { FlagshipContext } from './FlagshipContext';
|
|
3
|
-
import { logError, logWarn } from './utils';
|
|
4
|
-
|
|
5
|
-
const checkType = (value, defaultValue) => typeof value === 'object' && typeof defaultValue === 'object' && Array.isArray(value) === Array.isArray(defaultValue) || typeof value === typeof defaultValue;
|
|
6
|
-
|
|
7
|
-
const fsModificationsSync = args => {
|
|
8
|
-
const {
|
|
9
|
-
visitor,
|
|
10
|
-
params,
|
|
11
|
-
activateAll,
|
|
12
|
-
state,
|
|
13
|
-
functionName,
|
|
14
|
-
config
|
|
15
|
-
} = args;
|
|
16
|
-
|
|
17
|
-
if (visitor) {
|
|
18
|
-
return visitor.getModificationsSync(params, activateAll);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const check = !state.status.isSdkReady && !!state.modifications && state.modifications.size > 0;
|
|
22
|
-
const flags = {};
|
|
23
|
-
|
|
24
|
-
if (check) {
|
|
25
|
-
params.forEach(item => {
|
|
26
|
-
var _state$modifications;
|
|
27
|
-
|
|
28
|
-
const modification = (_state$modifications = state.modifications) === null || _state$modifications === void 0 ? void 0 : _state$modifications.get(item.key);
|
|
29
|
-
|
|
30
|
-
if (modification && checkType(modification === null || modification === void 0 ? void 0 : modification.value, item.defaultValue)) {
|
|
31
|
-
flags[item.key] = modification.value;
|
|
32
|
-
} else {
|
|
33
|
-
flags[item.key] = item.defaultValue;
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
return flags;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
logWarn(config, noVisitorDefault, functionName);
|
|
40
|
-
params.forEach(item => {
|
|
41
|
-
flags[item.key] = item.defaultValue;
|
|
42
|
-
});
|
|
43
|
-
return flags;
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* Retrieve a modification value by its key. If no modification match the given key or if the stored value type and default value type do not match, default value will be returned.
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
export const useFsModifications = (params, activateAll) => {
|
|
51
|
-
const {
|
|
52
|
-
state
|
|
53
|
-
} = useContext(FlagshipContext);
|
|
54
|
-
const {
|
|
55
|
-
visitor,
|
|
56
|
-
config
|
|
57
|
-
} = state;
|
|
58
|
-
const functionName = 'useFsModifications';
|
|
59
|
-
return fsModificationsSync({
|
|
60
|
-
functionName,
|
|
61
|
-
state,
|
|
62
|
-
visitor,
|
|
63
|
-
config,
|
|
64
|
-
params,
|
|
65
|
-
activateAll
|
|
66
|
-
});
|
|
67
|
-
};
|
|
68
|
-
/**
|
|
69
|
-
* Retrieve a modification value by its key. If no modification match the given key or if the stored value type and default value type do not match, default value will be returned.
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
export const useFsModification = params => {
|
|
73
|
-
var _state$modifications2;
|
|
74
|
-
|
|
75
|
-
const {
|
|
76
|
-
state
|
|
77
|
-
} = useContext(FlagshipContext);
|
|
78
|
-
const {
|
|
79
|
-
visitor,
|
|
80
|
-
config
|
|
81
|
-
} = state;
|
|
82
|
-
const functionName = 'useFsModifications';
|
|
83
|
-
|
|
84
|
-
if (visitor) {
|
|
85
|
-
return visitor.getModificationSync(params);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const modification = (_state$modifications2 = state.modifications) === null || _state$modifications2 === void 0 ? void 0 : _state$modifications2.get(params.key);
|
|
89
|
-
|
|
90
|
-
if (!state.status.isSdkReady && modification && checkType(modification === null || modification === void 0 ? void 0 : modification.value, params.defaultValue)) {
|
|
91
|
-
return modification.value;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
logWarn(config, noVisitorDefault, functionName);
|
|
95
|
-
return params.defaultValue;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const fsModificationInfoSync = args => {
|
|
99
|
-
var _state$modifications3;
|
|
100
|
-
|
|
101
|
-
const {
|
|
102
|
-
key,
|
|
103
|
-
visitor,
|
|
104
|
-
state
|
|
105
|
-
} = args;
|
|
106
|
-
|
|
107
|
-
if (visitor) {
|
|
108
|
-
return visitor.getModificationInfoSync(key);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const modification = (_state$modifications3 = state.modifications) === null || _state$modifications3 === void 0 ? void 0 : _state$modifications3.get(key);
|
|
112
|
-
|
|
113
|
-
if (!state.status.isSdkReady && modification) {
|
|
114
|
-
return modification;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return null;
|
|
118
|
-
};
|
|
119
|
-
/**
|
|
120
|
-
* Get the campaign modification information value matching the given key.
|
|
121
|
-
* @param {string} key key which identify the modification.
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
export const useFsModificationInfo = key => {
|
|
126
|
-
const {
|
|
127
|
-
state
|
|
128
|
-
} = useContext(FlagshipContext);
|
|
129
|
-
const {
|
|
130
|
-
visitor
|
|
131
|
-
} = state;
|
|
132
|
-
return fsModificationInfoSync({
|
|
133
|
-
key,
|
|
134
|
-
state,
|
|
135
|
-
visitor
|
|
136
|
-
});
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
const fsSynchronizeModifications = async (functionName, visitor, config) => {
|
|
140
|
-
try {
|
|
141
|
-
if (!visitor) {
|
|
142
|
-
reportNoVisitor(config, functionName);
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
await visitor.synchronizeModifications(); // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
147
|
-
} catch (error) {
|
|
148
|
-
logError(config, error.message || error, functionName);
|
|
149
|
-
}
|
|
150
|
-
};
|
|
151
|
-
/**
|
|
152
|
-
* This function calls the decision api and update all the campaigns modifications from the server according to the visitor context.
|
|
153
|
-
*/
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
export const useFsSynchronizeModifications = async () => {
|
|
157
|
-
const {
|
|
158
|
-
state
|
|
159
|
-
} = useContext(FlagshipContext);
|
|
160
|
-
const {
|
|
161
|
-
visitor,
|
|
162
|
-
config
|
|
163
|
-
} = state;
|
|
164
|
-
const functionName = 'useFsSynchronizeModifications';
|
|
165
|
-
await fsSynchronizeModifications(functionName, visitor, config);
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
const fsActivate = async (params, functionName, visitor, config) => {
|
|
169
|
-
try {
|
|
170
|
-
if (!visitor) {
|
|
171
|
-
logWarn(config, noVisitorMessage, functionName);
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
await visitor.activateModifications(params); // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
176
|
-
} catch (error) {
|
|
177
|
-
logWarn(config, error.message || error, functionName);
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
/**
|
|
181
|
-
* Report this user has seen this modification. Report this user has seen these modifications.
|
|
182
|
-
* @param params
|
|
183
|
-
* @returns
|
|
184
|
-
*/
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
export const useFsActivate = async params => {
|
|
188
|
-
const {
|
|
189
|
-
state
|
|
190
|
-
} = useContext(FlagshipContext);
|
|
191
|
-
const {
|
|
192
|
-
visitor,
|
|
193
|
-
config
|
|
194
|
-
} = state;
|
|
195
|
-
const functionName = 'useFsModifications';
|
|
196
|
-
await fsActivate(params, functionName, visitor, config);
|
|
197
|
-
};
|
|
198
|
-
export const useFlagship = () => {
|
|
199
|
-
const {
|
|
200
|
-
state
|
|
201
|
-
} = useContext(FlagshipContext);
|
|
202
|
-
const {
|
|
203
|
-
visitor,
|
|
204
|
-
config
|
|
205
|
-
} = state;
|
|
206
|
-
|
|
207
|
-
const fsUpdateContext = context => {
|
|
208
|
-
const functionName = 'updateContext';
|
|
209
|
-
|
|
210
|
-
if (!visitor) {
|
|
211
|
-
logError(config, noVisitorMessage, functionName);
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
visitor.clearContext();
|
|
216
|
-
visitor.updateContext(context);
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
const fsClearContext = () => {
|
|
220
|
-
const functionName = 'cleanContext';
|
|
221
|
-
|
|
222
|
-
if (!visitor) {
|
|
223
|
-
logError(config, noVisitorMessage, functionName);
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
visitor.clearContext();
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
const fsAuthenticate = visitorId => {
|
|
231
|
-
const functionName = 'authenticate';
|
|
232
|
-
|
|
233
|
-
if (!visitor) {
|
|
234
|
-
logError(config, noVisitorMessage, functionName);
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
visitor.authenticate(visitorId);
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
const fsUnauthenticate = () => {
|
|
242
|
-
const functionName = 'unauthenticate';
|
|
243
|
-
|
|
244
|
-
if (!visitor) {
|
|
245
|
-
logError(config, noVisitorMessage, functionName);
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
visitor.unauthenticate();
|
|
250
|
-
};
|
|
251
|
-
/**
|
|
252
|
-
* Send a Hit to Flagship servers for reporting.
|
|
253
|
-
*/
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const fsSendHit = hit => {
|
|
257
|
-
const functionName = 'sendHit';
|
|
258
|
-
|
|
259
|
-
if (!visitor) {
|
|
260
|
-
logError(config, noVisitorMessage, functionName);
|
|
261
|
-
return Promise.resolve();
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return visitor.sendHit(hit);
|
|
265
|
-
};
|
|
266
|
-
/**
|
|
267
|
-
* Send a Hit to Flagship servers for reporting.
|
|
268
|
-
*/
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const fsSendHits = hit => {
|
|
272
|
-
const functionName = 'sendHits';
|
|
273
|
-
|
|
274
|
-
if (!visitor) {
|
|
275
|
-
logError(config, noVisitorMessage, functionName);
|
|
276
|
-
return Promise.resolve();
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return visitor.sendHits(hit);
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
let modifications = visitor === null || visitor === void 0 ? void 0 : visitor.getModificationsArray();
|
|
283
|
-
|
|
284
|
-
if (!state.status.isSdkReady && state.modifications) {
|
|
285
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
286
|
-
modifications = Array.from(state.modifications, ([_key, item]) => item);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const activateModification = async params => {
|
|
290
|
-
const functionName = 'activateModification';
|
|
291
|
-
await fsActivate(params, functionName, visitor, config);
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
const synchronizeModifications = async () => {
|
|
295
|
-
const functionName = 'synchronizeModifications';
|
|
296
|
-
await fsSynchronizeModifications(functionName, visitor, config);
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
const getModifications = (params, activateAll) => {
|
|
300
|
-
const functionName = 'getModifications';
|
|
301
|
-
return fsModificationsSync({
|
|
302
|
-
functionName,
|
|
303
|
-
state,
|
|
304
|
-
visitor,
|
|
305
|
-
config,
|
|
306
|
-
params,
|
|
307
|
-
activateAll
|
|
308
|
-
});
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
const getModificationInfo = key => {
|
|
312
|
-
return fsModificationInfoSync({
|
|
313
|
-
key,
|
|
314
|
-
state,
|
|
315
|
-
visitor
|
|
316
|
-
});
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
return {
|
|
320
|
-
updateContext: fsUpdateContext,
|
|
321
|
-
clearContext: fsClearContext,
|
|
322
|
-
authenticate: fsAuthenticate,
|
|
323
|
-
unauthenticate: fsUnauthenticate,
|
|
324
|
-
status: state.status,
|
|
325
|
-
activateModification,
|
|
326
|
-
synchronizeModifications,
|
|
327
|
-
getModifications,
|
|
328
|
-
modifications: modifications || [],
|
|
329
|
-
getModificationInfo,
|
|
330
|
-
hit: {
|
|
331
|
-
send: fsSendHit,
|
|
332
|
-
sendMultiple: fsSendHits
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
};
|
|
336
|
-
export const noVisitorMessage = 'sdk not correctly initialized... Make sure fsVisitor is ready.';
|
|
337
|
-
export const noVisitorDefault = 'fsVisitor not initialized, returns default value';
|
|
338
|
-
|
|
339
|
-
const reportNoVisitor = (config, tag) => {
|
|
340
|
-
logError(config, noVisitorMessage, tag);
|
|
341
|
-
};
|
package/dist/es/index.js
DELETED
package/dist/es/utils.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { LogLevel } from '@flagship.io/js-sdk';
|
|
2
|
-
export function logError(config, message, tag) {
|
|
3
|
-
if (!config || !config.logManager || typeof config.logManager.error !== 'function' || !config.logLevel || config.logLevel < LogLevel.ERROR) {
|
|
4
|
-
return;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
config.logManager.error(message, tag);
|
|
8
|
-
}
|
|
9
|
-
export function logInfo(config, message, tag) {
|
|
10
|
-
if (!config || !config.logManager || typeof config.logManager.info !== 'function' || !config.logLevel || config.logLevel < LogLevel.INFO) {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
config.logManager.info(message, tag);
|
|
15
|
-
}
|
|
16
|
-
export function logWarn(config, message, tag) {
|
|
17
|
-
if (!config || !config.logManager || typeof config.logManager.warning !== 'function' || !config.logLevel || config.logLevel < LogLevel.WARNING) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
config.logManager.warning(message, tag);
|
|
22
|
-
}
|
|
23
|
-
export const getModificationsFromCampaigns = campaigns => {
|
|
24
|
-
const modifications = new Map();
|
|
25
|
-
|
|
26
|
-
if (!campaigns || !Array.isArray(campaigns)) {
|
|
27
|
-
return modifications;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
campaigns.forEach(campaign => {
|
|
31
|
-
const object = campaign.variation.modifications.value;
|
|
32
|
-
|
|
33
|
-
for (const key in object) {
|
|
34
|
-
const value = object[key];
|
|
35
|
-
modifications.set(key, {
|
|
36
|
-
key,
|
|
37
|
-
campaignId: campaign.id,
|
|
38
|
-
variationGroupId: campaign.variationGroupId,
|
|
39
|
-
variationId: campaign.variation.id,
|
|
40
|
-
isReference: campaign.variation.reference,
|
|
41
|
-
value
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
return modifications;
|
|
46
|
-
};
|
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
-
|
|
3
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
-
|
|
5
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
6
|
-
|
|
7
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
8
|
-
|
|
9
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
10
|
-
|
|
11
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
12
|
-
|
|
13
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
14
|
-
|
|
15
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
16
|
-
|
|
17
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
18
|
-
|
|
19
|
-
// eslint-disable-next-line no-use-before-define
|
|
20
|
-
import React, { useState, useEffect, createContext, useRef } from 'react';
|
|
21
|
-
import { Flagship, FlagshipStatus } from '@flagship.io/js-sdk';
|
|
22
|
-
import { getModificationsFromCampaigns, logError } from './utils';
|
|
23
|
-
var initStat = {
|
|
24
|
-
status: {
|
|
25
|
-
isLoading: true,
|
|
26
|
-
isSdkReady: false
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
export var FlagshipContext = /*#__PURE__*/createContext({
|
|
30
|
-
state: _objectSpread({}, initStat)
|
|
31
|
-
});
|
|
32
|
-
export var FlagshipProvider = function FlagshipProvider(_ref) {
|
|
33
|
-
var children = _ref.children,
|
|
34
|
-
fetchNow = _ref.fetchNow,
|
|
35
|
-
envId = _ref.envId,
|
|
36
|
-
apiKey = _ref.apiKey,
|
|
37
|
-
decisionMode = _ref.decisionMode,
|
|
38
|
-
decisionApiUrl = _ref.decisionApiUrl,
|
|
39
|
-
timeout = _ref.timeout,
|
|
40
|
-
logLevel = _ref.logLevel,
|
|
41
|
-
statusChangedCallback = _ref.statusChangedCallback,
|
|
42
|
-
logManager = _ref.logManager,
|
|
43
|
-
pollingInterval = _ref.pollingInterval,
|
|
44
|
-
visitorData = _ref.visitorData,
|
|
45
|
-
onInitStart = _ref.onInitStart,
|
|
46
|
-
onInitDone = _ref.onInitDone,
|
|
47
|
-
onBucketingSuccess = _ref.onBucketingSuccess,
|
|
48
|
-
onBucketingFail = _ref.onBucketingFail,
|
|
49
|
-
loadingComponent = _ref.loadingComponent,
|
|
50
|
-
onBucketingUpdated = _ref.onBucketingUpdated,
|
|
51
|
-
onUpdate = _ref.onUpdate,
|
|
52
|
-
enableClientCache = _ref.enableClientCache,
|
|
53
|
-
initialBucketing = _ref.initialBucketing,
|
|
54
|
-
initialCampaigns = _ref.initialCampaigns,
|
|
55
|
-
initialModifications = _ref.initialModifications,
|
|
56
|
-
synchronizeOnBucketingUpdated = _ref.synchronizeOnBucketingUpdated,
|
|
57
|
-
activateDeduplicationTime = _ref.activateDeduplicationTime,
|
|
58
|
-
hitDeduplicationTime = _ref.hitDeduplicationTime,
|
|
59
|
-
visitorCacheImplementation = _ref.visitorCacheImplementation,
|
|
60
|
-
hitCacheImplementation = _ref.hitCacheImplementation,
|
|
61
|
-
disableCache = _ref.disableCache;
|
|
62
|
-
var modifications = new Map();
|
|
63
|
-
|
|
64
|
-
if (initialModifications) {
|
|
65
|
-
initialModifications.forEach(function (modification) {
|
|
66
|
-
modifications.set(modification.key, modification);
|
|
67
|
-
});
|
|
68
|
-
} else if (initialCampaigns) {
|
|
69
|
-
modifications = getModificationsFromCampaigns(initialCampaigns);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
var _useState = useState(_objectSpread(_objectSpread({}, initStat), {}, {
|
|
73
|
-
modifications: modifications
|
|
74
|
-
})),
|
|
75
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
76
|
-
state = _useState2[0],
|
|
77
|
-
setState = _useState2[1];
|
|
78
|
-
|
|
79
|
-
var _useState3 = useState(),
|
|
80
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
81
|
-
lastModified = _useState4[0],
|
|
82
|
-
setLastModified = _useState4[1];
|
|
83
|
-
|
|
84
|
-
var stateRef = useRef();
|
|
85
|
-
stateRef.current = state;
|
|
86
|
-
useEffect(function () {
|
|
87
|
-
if (synchronizeOnBucketingUpdated) {
|
|
88
|
-
var _state$visitor;
|
|
89
|
-
|
|
90
|
-
(_state$visitor = state.visitor) === null || _state$visitor === void 0 ? void 0 : _state$visitor.synchronizeModifications();
|
|
91
|
-
}
|
|
92
|
-
}, [lastModified]);
|
|
93
|
-
useEffect(function () {
|
|
94
|
-
updateVisitor();
|
|
95
|
-
}, [JSON.stringify(visitorData)]);
|
|
96
|
-
useEffect(function () {
|
|
97
|
-
initSdk();
|
|
98
|
-
}, [envId, apiKey, decisionMode]);
|
|
99
|
-
|
|
100
|
-
var updateVisitor = function updateVisitor() {
|
|
101
|
-
if (!state.visitor) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (visitorData.context) {
|
|
106
|
-
state.visitor.clearContext();
|
|
107
|
-
state.visitor.updateContext(visitorData.context);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (typeof visitorData.hasConsented === 'boolean') {
|
|
111
|
-
state.visitor.setConsent(visitorData.hasConsented);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (state.visitor.anonymousId && !visitorData.isAuthenticated) {
|
|
115
|
-
state.visitor.unauthenticate();
|
|
116
|
-
} else if (!state.visitor.anonymousId && visitorData.isAuthenticated) {
|
|
117
|
-
state.visitor.authenticate(visitorData.id);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (visitorData.id) {
|
|
121
|
-
state.visitor.visitorId = visitorData.id;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
state.visitor.synchronizeModifications();
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
function initializeState(param) {
|
|
128
|
-
var newStatus = {
|
|
129
|
-
isSdkReady: param.isSdkReady,
|
|
130
|
-
isLoading: param.isLoading,
|
|
131
|
-
isVisitorDefined: !!param.fsVisitor,
|
|
132
|
-
lastRefresh: new Date().toISOString()
|
|
133
|
-
};
|
|
134
|
-
setState(function (currentState) {
|
|
135
|
-
if (!currentState.status.firstInitSuccess) {
|
|
136
|
-
newStatus.firstInitSuccess = new Date().toISOString();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return _objectSpread(_objectSpread({}, currentState), {}, {
|
|
140
|
-
visitor: param.fsVisitor,
|
|
141
|
-
modifications: param.fsVisitor.modifications,
|
|
142
|
-
config: Flagship.getConfig(),
|
|
143
|
-
status: _objectSpread(_objectSpread({}, currentState.status), newStatus)
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
return newStatus;
|
|
147
|
-
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
var onVisitorReady = function onVisitorReady(fsVisitor, error) {
|
|
151
|
-
if (error) {
|
|
152
|
-
logError(Flagship.getConfig(), error.message || error, 'onReady');
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
var newStatus = initializeState({
|
|
157
|
-
fsVisitor: fsVisitor,
|
|
158
|
-
isSdkReady: true,
|
|
159
|
-
isLoading: false
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
if (onUpdate) {
|
|
163
|
-
onUpdate({
|
|
164
|
-
fsModifications: fsVisitor.modifications,
|
|
165
|
-
config: Flagship.getConfig(),
|
|
166
|
-
status: newStatus
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
var statusChanged = function statusChanged(status) {
|
|
172
|
-
if (statusChangedCallback) {
|
|
173
|
-
statusChangedCallback(status);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (status === FlagshipStatus.STARTING && onInitStart) {
|
|
177
|
-
onInitStart();
|
|
178
|
-
} else if (status === FlagshipStatus.READY_PANIC_ON || status === FlagshipStatus.READY) {
|
|
179
|
-
var _stateRef$current;
|
|
180
|
-
|
|
181
|
-
if (onInitDone) {
|
|
182
|
-
onInitDone();
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if ((_stateRef$current = stateRef.current) !== null && _stateRef$current !== void 0 && _stateRef$current.visitor) {
|
|
186
|
-
var _stateRef$current2;
|
|
187
|
-
|
|
188
|
-
(_stateRef$current2 = stateRef.current) === null || _stateRef$current2 === void 0 ? void 0 : _stateRef$current2.visitor.synchronizeModifications();
|
|
189
|
-
} else {
|
|
190
|
-
var fsVisitor = Flagship.newVisitor({
|
|
191
|
-
visitorId: visitorData.id,
|
|
192
|
-
context: visitorData.context,
|
|
193
|
-
isAuthenticated: visitorData.isAuthenticated,
|
|
194
|
-
hasConsented: visitorData.hasConsented,
|
|
195
|
-
initialCampaigns: initialCampaigns,
|
|
196
|
-
initialModifications: initialModifications
|
|
197
|
-
});
|
|
198
|
-
fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.on('ready', function (error) {
|
|
199
|
-
onVisitorReady(fsVisitor, error);
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
if (!fetchNow && fsVisitor) {
|
|
203
|
-
initializeState({
|
|
204
|
-
fsVisitor: fsVisitor,
|
|
205
|
-
isSdkReady: true,
|
|
206
|
-
isLoading: false
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
var onBucketingLastModified = function onBucketingLastModified(lastUpdate) {
|
|
214
|
-
if (onBucketingUpdated) {
|
|
215
|
-
onBucketingUpdated(lastUpdate);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
setLastModified(lastUpdate);
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
var initSdk = function initSdk() {
|
|
222
|
-
Flagship.start(envId, apiKey, {
|
|
223
|
-
decisionMode: decisionMode,
|
|
224
|
-
fetchNow: fetchNow,
|
|
225
|
-
timeout: timeout,
|
|
226
|
-
logLevel: logLevel,
|
|
227
|
-
statusChangedCallback: statusChanged,
|
|
228
|
-
logManager: logManager,
|
|
229
|
-
pollingInterval: pollingInterval,
|
|
230
|
-
onBucketingFail: onBucketingFail,
|
|
231
|
-
onBucketingSuccess: onBucketingSuccess,
|
|
232
|
-
enableClientCache: enableClientCache,
|
|
233
|
-
decisionApiUrl: decisionApiUrl,
|
|
234
|
-
onBucketingUpdated: onBucketingLastModified,
|
|
235
|
-
initialBucketing: initialBucketing,
|
|
236
|
-
activateDeduplicationTime: activateDeduplicationTime,
|
|
237
|
-
hitDeduplicationTime: hitDeduplicationTime,
|
|
238
|
-
visitorCacheImplementation: visitorCacheImplementation,
|
|
239
|
-
hitCacheImplementation: hitCacheImplementation,
|
|
240
|
-
disableCache: disableCache
|
|
241
|
-
});
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
var handleDisplay = function handleDisplay() {
|
|
245
|
-
var isFirstInit = !state.visitor;
|
|
246
|
-
|
|
247
|
-
if (state.status.isLoading && loadingComponent && isFirstInit) {
|
|
248
|
-
return /*#__PURE__*/React.createElement(React.Fragment, null, loadingComponent);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return /*#__PURE__*/React.createElement(React.Fragment, null, children);
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
return /*#__PURE__*/React.createElement(FlagshipContext.Provider, {
|
|
255
|
-
value: {
|
|
256
|
-
state: state,
|
|
257
|
-
setState: setState
|
|
258
|
-
}
|
|
259
|
-
}, handleDisplay());
|
|
260
|
-
};
|
|
261
|
-
FlagshipProvider.defaultProps = {
|
|
262
|
-
nodeEnv: 'production',
|
|
263
|
-
activateDeduplicationTime: 10,
|
|
264
|
-
hitDeduplicationTime: 10
|
|
265
|
-
};
|