@flagship.io/react-sdk 2.2.0-beta.5 → 3.0.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 +2 -2
- package/dist/es/Flag.js +36 -0
- package/dist/es/FlagshipContext.js +59 -67
- package/dist/es/FlagshipHooks.js +67 -36
- package/dist/es/constants.js +2 -0
- package/dist/es/utils.js +32 -1
- package/dist/esm/Flag.js +55 -0
- package/dist/esm/FlagshipContext.js +61 -67
- package/dist/esm/FlagshipHooks.js +108 -115
- package/dist/esm/constants.js +2 -0
- package/dist/esm/utils.js +32 -1
- package/dist/index.browser.js +1 -1
- package/dist/index.browser.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types/Flag.d.ts +9 -0
- package/dist/types/FlagshipContext.d.ts +38 -20
- package/dist/types/FlagshipHooks.d.ts +63 -20
- package/dist/types/constants.d.ts +2 -0
- package/dist/types/utils.d.ts +5 -1
- package/package.json +3 -3
|
@@ -17,9 +17,9 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
17
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
18
|
|
|
19
19
|
// eslint-disable-next-line no-use-before-define
|
|
20
|
-
import React, { useState, useEffect, createContext, useRef } from
|
|
21
|
-
import { Flagship, FlagshipStatus } from
|
|
22
|
-
import { getModificationsFromCampaigns, logError } from
|
|
20
|
+
import React, { useState, useEffect, createContext, useRef } from "react";
|
|
21
|
+
import { Flagship, FlagshipStatus } from "@flagship.io/js-sdk";
|
|
22
|
+
import { getModificationsFromCampaigns, logError, useNonInitialEffect } from "./utils";
|
|
23
23
|
var initStat = {
|
|
24
24
|
status: {
|
|
25
25
|
isLoading: true,
|
|
@@ -53,12 +53,20 @@ export var FlagshipProvider = function FlagshipProvider(_ref) {
|
|
|
53
53
|
initialBucketing = _ref.initialBucketing,
|
|
54
54
|
initialCampaigns = _ref.initialCampaigns,
|
|
55
55
|
initialModifications = _ref.initialModifications,
|
|
56
|
+
initialFlagsData = _ref.initialFlagsData,
|
|
56
57
|
synchronizeOnBucketingUpdated = _ref.synchronizeOnBucketingUpdated,
|
|
57
58
|
activateDeduplicationTime = _ref.activateDeduplicationTime,
|
|
58
|
-
hitDeduplicationTime = _ref.hitDeduplicationTime
|
|
59
|
+
hitDeduplicationTime = _ref.hitDeduplicationTime,
|
|
60
|
+
visitorCacheImplementation = _ref.visitorCacheImplementation,
|
|
61
|
+
hitCacheImplementation = _ref.hitCacheImplementation,
|
|
62
|
+
disableCache = _ref.disableCache;
|
|
59
63
|
var modifications = new Map();
|
|
60
64
|
|
|
61
|
-
if (
|
|
65
|
+
if (initialFlagsData && initialFlagsData.forEach) {
|
|
66
|
+
initialFlagsData.forEach(function (flag) {
|
|
67
|
+
modifications.set(flag.key, flag);
|
|
68
|
+
});
|
|
69
|
+
} else if (initialModifications && initialModifications.forEach) {
|
|
62
70
|
initialModifications.forEach(function (modification) {
|
|
63
71
|
modifications.set(modification.key, modification);
|
|
64
72
|
});
|
|
@@ -80,47 +88,20 @@ export var FlagshipProvider = function FlagshipProvider(_ref) {
|
|
|
80
88
|
|
|
81
89
|
var stateRef = useRef();
|
|
82
90
|
stateRef.current = state;
|
|
83
|
-
|
|
91
|
+
useNonInitialEffect(function () {
|
|
84
92
|
if (synchronizeOnBucketingUpdated) {
|
|
85
93
|
var _state$visitor;
|
|
86
94
|
|
|
87
|
-
(_state$visitor = state.visitor) === null || _state$visitor === void 0 ? void 0 : _state$visitor.
|
|
95
|
+
(_state$visitor = state.visitor) === null || _state$visitor === void 0 ? void 0 : _state$visitor.fetchFlags();
|
|
88
96
|
}
|
|
89
97
|
}, [lastModified]);
|
|
90
|
-
|
|
91
|
-
|
|
98
|
+
useNonInitialEffect(function () {
|
|
99
|
+
createVisitor(true);
|
|
92
100
|
}, [JSON.stringify(visitorData)]);
|
|
93
101
|
useEffect(function () {
|
|
94
102
|
initSdk();
|
|
95
103
|
}, [envId, apiKey, decisionMode]);
|
|
96
104
|
|
|
97
|
-
var updateVisitor = function updateVisitor() {
|
|
98
|
-
if (!state.visitor) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (visitorData.context) {
|
|
103
|
-
state.visitor.clearContext();
|
|
104
|
-
state.visitor.updateContext(visitorData.context);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (typeof visitorData.hasConsented === 'boolean') {
|
|
108
|
-
state.visitor.setConsent(visitorData.hasConsented);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (state.visitor.anonymousId && !visitorData.isAuthenticated) {
|
|
112
|
-
state.visitor.unauthenticate();
|
|
113
|
-
} else if (!state.visitor.anonymousId && visitorData.isAuthenticated) {
|
|
114
|
-
state.visitor.authenticate(visitorData.id);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (visitorData.id) {
|
|
118
|
-
state.visitor.visitorId = visitorData.id;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
state.visitor.synchronizeModifications();
|
|
122
|
-
};
|
|
123
|
-
|
|
124
105
|
function initializeState(param) {
|
|
125
106
|
var newStatus = {
|
|
126
107
|
isSdkReady: param.isSdkReady,
|
|
@@ -146,7 +127,7 @@ export var FlagshipProvider = function FlagshipProvider(_ref) {
|
|
|
146
127
|
|
|
147
128
|
var onVisitorReady = function onVisitorReady(fsVisitor, error) {
|
|
148
129
|
if (error) {
|
|
149
|
-
logError(Flagship.getConfig(), error.message || error,
|
|
130
|
+
logError(Flagship.getConfig(), error.message || error, "onReady");
|
|
150
131
|
return;
|
|
151
132
|
}
|
|
152
133
|
|
|
@@ -165,6 +146,39 @@ export var FlagshipProvider = function FlagshipProvider(_ref) {
|
|
|
165
146
|
}
|
|
166
147
|
};
|
|
167
148
|
|
|
149
|
+
var createVisitor = function createVisitor() {
|
|
150
|
+
var fetchFlags = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
151
|
+
|
|
152
|
+
if (!visitorData) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
var fsVisitor = Flagship.newVisitor({
|
|
157
|
+
visitorId: visitorData.id,
|
|
158
|
+
context: visitorData.context,
|
|
159
|
+
isAuthenticated: visitorData.isAuthenticated,
|
|
160
|
+
hasConsented: visitorData.hasConsented,
|
|
161
|
+
initialCampaigns: initialCampaigns,
|
|
162
|
+
initialModifications: initialModifications,
|
|
163
|
+
initialFlagsData: initialFlagsData
|
|
164
|
+
});
|
|
165
|
+
fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.on("ready", function (error) {
|
|
166
|
+
onVisitorReady(fsVisitor, error);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
if (!fetchNow && fsVisitor && !fetchFlags) {
|
|
170
|
+
initializeState({
|
|
171
|
+
fsVisitor: fsVisitor,
|
|
172
|
+
isSdkReady: true,
|
|
173
|
+
isLoading: false
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (fetchFlags && !fetchNow) {
|
|
178
|
+
fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.fetchFlags();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
168
182
|
var statusChanged = function statusChanged(status) {
|
|
169
183
|
if (statusChangedCallback) {
|
|
170
184
|
statusChangedCallback(status);
|
|
@@ -172,38 +186,15 @@ export var FlagshipProvider = function FlagshipProvider(_ref) {
|
|
|
172
186
|
|
|
173
187
|
if (status === FlagshipStatus.STARTING && onInitStart) {
|
|
174
188
|
onInitStart();
|
|
175
|
-
|
|
176
|
-
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
177
191
|
|
|
192
|
+
if (status === FlagshipStatus.READY_PANIC_ON || status === FlagshipStatus.READY) {
|
|
178
193
|
if (onInitDone) {
|
|
179
194
|
onInitDone();
|
|
180
195
|
}
|
|
181
196
|
|
|
182
|
-
|
|
183
|
-
var _stateRef$current2;
|
|
184
|
-
|
|
185
|
-
(_stateRef$current2 = stateRef.current) === null || _stateRef$current2 === void 0 ? void 0 : _stateRef$current2.visitor.synchronizeModifications();
|
|
186
|
-
} else {
|
|
187
|
-
var fsVisitor = Flagship.newVisitor({
|
|
188
|
-
visitorId: visitorData.id,
|
|
189
|
-
context: visitorData.context,
|
|
190
|
-
isAuthenticated: visitorData.isAuthenticated,
|
|
191
|
-
hasConsented: visitorData.hasConsented,
|
|
192
|
-
initialCampaigns: initialCampaigns,
|
|
193
|
-
initialModifications: initialModifications
|
|
194
|
-
});
|
|
195
|
-
fsVisitor === null || fsVisitor === void 0 ? void 0 : fsVisitor.on('ready', function (error) {
|
|
196
|
-
onVisitorReady(fsVisitor, error);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
if (!fetchNow && fsVisitor) {
|
|
200
|
-
initializeState({
|
|
201
|
-
fsVisitor: fsVisitor,
|
|
202
|
-
isSdkReady: true,
|
|
203
|
-
isLoading: false
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
}
|
|
197
|
+
createVisitor();
|
|
207
198
|
}
|
|
208
199
|
};
|
|
209
200
|
|
|
@@ -231,14 +222,18 @@ export var FlagshipProvider = function FlagshipProvider(_ref) {
|
|
|
231
222
|
onBucketingUpdated: onBucketingLastModified,
|
|
232
223
|
initialBucketing: initialBucketing,
|
|
233
224
|
activateDeduplicationTime: activateDeduplicationTime,
|
|
234
|
-
hitDeduplicationTime: hitDeduplicationTime
|
|
225
|
+
hitDeduplicationTime: hitDeduplicationTime,
|
|
226
|
+
visitorCacheImplementation: visitorCacheImplementation,
|
|
227
|
+
hitCacheImplementation: hitCacheImplementation,
|
|
228
|
+
disableCache: disableCache,
|
|
229
|
+
language: 1
|
|
235
230
|
});
|
|
236
231
|
};
|
|
237
232
|
|
|
238
233
|
var handleDisplay = function handleDisplay() {
|
|
239
234
|
var isFirstInit = !state.visitor;
|
|
240
235
|
|
|
241
|
-
if (state.status.isLoading && loadingComponent && isFirstInit) {
|
|
236
|
+
if (state.status.isLoading && loadingComponent && isFirstInit && fetchNow) {
|
|
242
237
|
return /*#__PURE__*/React.createElement(React.Fragment, null, loadingComponent);
|
|
243
238
|
}
|
|
244
239
|
|
|
@@ -253,7 +248,6 @@ export var FlagshipProvider = function FlagshipProvider(_ref) {
|
|
|
253
248
|
}, handleDisplay());
|
|
254
249
|
};
|
|
255
250
|
FlagshipProvider.defaultProps = {
|
|
256
|
-
nodeEnv: 'production',
|
|
257
251
|
activateDeduplicationTime: 10,
|
|
258
252
|
hitDeduplicationTime: 10
|
|
259
253
|
};
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
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; }
|
|
2
|
+
|
|
3
|
+
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; }
|
|
4
|
+
|
|
5
|
+
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; }
|
|
6
|
+
|
|
1
7
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
8
|
|
|
3
9
|
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."); }
|
|
@@ -19,6 +25,8 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
|
|
|
19
25
|
import { useContext } from 'react';
|
|
20
26
|
import { FlagshipContext } from './FlagshipContext';
|
|
21
27
|
import { logError, logWarn } from './utils';
|
|
28
|
+
import { Flag } from './Flag';
|
|
29
|
+
import { noVisitorDefault, noVisitorMessage } from './constants';
|
|
22
30
|
|
|
23
31
|
var checkType = function checkType(value, defaultValue) {
|
|
24
32
|
return _typeof(value) === 'object' && _typeof(defaultValue) === 'object' && Array.isArray(value) === Array.isArray(defaultValue) || _typeof(value) === _typeof(defaultValue);
|
|
@@ -62,6 +70,7 @@ var fsModificationsSync = function fsModificationsSync(args) {
|
|
|
62
70
|
};
|
|
63
71
|
/**
|
|
64
72
|
* 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.
|
|
73
|
+
* @deprecated use useFsGetFlag instead
|
|
65
74
|
*/
|
|
66
75
|
|
|
67
76
|
|
|
@@ -83,6 +92,7 @@ export var useFsModifications = function useFsModifications(params, activateAll)
|
|
|
83
92
|
};
|
|
84
93
|
/**
|
|
85
94
|
* 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.
|
|
95
|
+
* @deprecated use useFsGetFlag instead
|
|
86
96
|
*/
|
|
87
97
|
|
|
88
98
|
export var useFsModification = function useFsModification(params) {
|
|
@@ -131,6 +141,7 @@ var fsModificationInfoSync = function fsModificationInfoSync(args) {
|
|
|
131
141
|
/**
|
|
132
142
|
* Get the campaign modification information value matching the given key.
|
|
133
143
|
* @param {string} key key which identify the modification.
|
|
144
|
+
* @deprecated use useFsGetFlag instead
|
|
134
145
|
*/
|
|
135
146
|
|
|
136
147
|
|
|
@@ -146,8 +157,8 @@ export var useFsModificationInfo = function useFsModificationInfo(key) {
|
|
|
146
157
|
});
|
|
147
158
|
};
|
|
148
159
|
|
|
149
|
-
var
|
|
150
|
-
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(functionName, visitor, config) {
|
|
160
|
+
var fsActivate = /*#__PURE__*/function () {
|
|
161
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(params, functionName, visitor, config) {
|
|
151
162
|
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
152
163
|
while (1) {
|
|
153
164
|
switch (_context.prev = _context.next) {
|
|
@@ -159,12 +170,12 @@ var fsSynchronizeModifications = /*#__PURE__*/function () {
|
|
|
159
170
|
break;
|
|
160
171
|
}
|
|
161
172
|
|
|
162
|
-
|
|
173
|
+
logWarn(config, noVisitorMessage, functionName);
|
|
163
174
|
return _context.abrupt("return");
|
|
164
175
|
|
|
165
176
|
case 4:
|
|
166
177
|
_context.next = 6;
|
|
167
|
-
return visitor.
|
|
178
|
+
return visitor.activateModifications(params);
|
|
168
179
|
|
|
169
180
|
case 6:
|
|
170
181
|
_context.next = 11;
|
|
@@ -173,7 +184,7 @@ var fsSynchronizeModifications = /*#__PURE__*/function () {
|
|
|
173
184
|
case 8:
|
|
174
185
|
_context.prev = 8;
|
|
175
186
|
_context.t0 = _context["catch"](0);
|
|
176
|
-
|
|
187
|
+
logWarn(config, _context.t0.message || _context.t0, functionName);
|
|
177
188
|
|
|
178
189
|
case 11:
|
|
179
190
|
case "end":
|
|
@@ -183,114 +194,61 @@ var fsSynchronizeModifications = /*#__PURE__*/function () {
|
|
|
183
194
|
}, _callee, null, [[0, 8]]);
|
|
184
195
|
}));
|
|
185
196
|
|
|
186
|
-
return function
|
|
197
|
+
return function fsActivate(_x, _x2, _x3, _x4) {
|
|
187
198
|
return _ref.apply(this, arguments);
|
|
188
199
|
};
|
|
189
200
|
}();
|
|
190
201
|
/**
|
|
191
|
-
* This
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
|
|
197
|
-
var _useContext4, state, visitor, config, functionName;
|
|
198
|
-
|
|
199
|
-
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
200
|
-
while (1) {
|
|
201
|
-
switch (_context2.prev = _context2.next) {
|
|
202
|
-
case 0:
|
|
203
|
-
_useContext4 = useContext(FlagshipContext), state = _useContext4.state;
|
|
204
|
-
visitor = state.visitor, config = state.config;
|
|
205
|
-
functionName = 'useFsSynchronizeModifications';
|
|
206
|
-
_context2.next = 5;
|
|
207
|
-
return fsSynchronizeModifications(functionName, visitor, config);
|
|
208
|
-
|
|
209
|
-
case 5:
|
|
210
|
-
case "end":
|
|
211
|
-
return _context2.stop();
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}, _callee2);
|
|
215
|
-
}));
|
|
216
|
-
|
|
217
|
-
return function useFsSynchronizeModifications() {
|
|
218
|
-
return _ref2.apply(this, arguments);
|
|
219
|
-
};
|
|
220
|
-
}();
|
|
221
|
-
|
|
222
|
-
var fsActivate = /*#__PURE__*/function () {
|
|
223
|
-
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(params, functionName, visitor, config) {
|
|
224
|
-
return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
225
|
-
while (1) {
|
|
226
|
-
switch (_context3.prev = _context3.next) {
|
|
227
|
-
case 0:
|
|
228
|
-
_context3.prev = 0;
|
|
229
|
-
|
|
230
|
-
if (visitor) {
|
|
231
|
-
_context3.next = 4;
|
|
232
|
-
break;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
logWarn(config, noVisitorMessage, functionName);
|
|
236
|
-
return _context3.abrupt("return");
|
|
202
|
+
* This hook returns a flag object by its key. If no flag match the given key an empty flag will be returned.
|
|
203
|
+
* @param key
|
|
204
|
+
* @param defaultValue
|
|
205
|
+
* @returns
|
|
206
|
+
*/
|
|
237
207
|
|
|
238
|
-
case 4:
|
|
239
|
-
_context3.next = 6;
|
|
240
|
-
return visitor.activateModifications(params);
|
|
241
208
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
209
|
+
export var useFsFlag = function useFsFlag(key, defaultValue) {
|
|
210
|
+
var _useContext4 = useContext(FlagshipContext),
|
|
211
|
+
state = _useContext4.state;
|
|
245
212
|
|
|
246
|
-
|
|
247
|
-
_context3.prev = 8;
|
|
248
|
-
_context3.t0 = _context3["catch"](0);
|
|
249
|
-
logWarn(config, _context3.t0.message || _context3.t0, functionName);
|
|
213
|
+
var visitor = state.visitor;
|
|
250
214
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}, _callee3, null, [[0, 8]]);
|
|
257
|
-
}));
|
|
215
|
+
if (!visitor) {
|
|
216
|
+
return new Flag(defaultValue);
|
|
217
|
+
}
|
|
258
218
|
|
|
259
|
-
return
|
|
260
|
-
|
|
261
|
-
};
|
|
262
|
-
}();
|
|
219
|
+
return visitor.getFlag(key, defaultValue);
|
|
220
|
+
};
|
|
263
221
|
/**
|
|
264
222
|
* Report this user has seen this modification. Report this user has seen these modifications.
|
|
265
223
|
* @param params
|
|
224
|
+
* @deprecated use useFsGetFlag instead
|
|
266
225
|
* @returns
|
|
267
226
|
*/
|
|
268
227
|
|
|
269
|
-
|
|
270
228
|
export var useFsActivate = /*#__PURE__*/function () {
|
|
271
|
-
var
|
|
229
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(params) {
|
|
272
230
|
var _useContext5, state, visitor, config, functionName;
|
|
273
231
|
|
|
274
|
-
return regeneratorRuntime.wrap(function
|
|
232
|
+
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
275
233
|
while (1) {
|
|
276
|
-
switch (
|
|
234
|
+
switch (_context2.prev = _context2.next) {
|
|
277
235
|
case 0:
|
|
278
236
|
_useContext5 = useContext(FlagshipContext), state = _useContext5.state;
|
|
279
237
|
visitor = state.visitor, config = state.config;
|
|
280
238
|
functionName = 'useFsModifications';
|
|
281
|
-
|
|
239
|
+
_context2.next = 5;
|
|
282
240
|
return fsActivate(params, functionName, visitor, config);
|
|
283
241
|
|
|
284
242
|
case 5:
|
|
285
243
|
case "end":
|
|
286
|
-
return
|
|
244
|
+
return _context2.stop();
|
|
287
245
|
}
|
|
288
246
|
}
|
|
289
|
-
},
|
|
247
|
+
}, _callee2);
|
|
290
248
|
}));
|
|
291
249
|
|
|
292
|
-
return function useFsActivate(
|
|
293
|
-
return
|
|
250
|
+
return function useFsActivate(_x5) {
|
|
251
|
+
return _ref2.apply(this, arguments);
|
|
294
252
|
};
|
|
295
253
|
}();
|
|
296
254
|
export var useFlagship = function useFlagship() {
|
|
@@ -345,8 +303,8 @@ export var useFlagship = function useFlagship() {
|
|
|
345
303
|
visitor.unauthenticate();
|
|
346
304
|
};
|
|
347
305
|
/**
|
|
348
|
-
|
|
349
|
-
|
|
306
|
+
* Send a Hit to Flagship servers for reporting.
|
|
307
|
+
*/
|
|
350
308
|
|
|
351
309
|
|
|
352
310
|
var fsSendHit = function fsSendHit(hit) {
|
|
@@ -360,8 +318,8 @@ export var useFlagship = function useFlagship() {
|
|
|
360
318
|
return visitor.sendHit(hit);
|
|
361
319
|
};
|
|
362
320
|
/**
|
|
363
|
-
|
|
364
|
-
|
|
321
|
+
* Send a Hit to Flagship servers for reporting.
|
|
322
|
+
*/
|
|
365
323
|
|
|
366
324
|
|
|
367
325
|
var fsSendHits = function fsSendHits(hit) {
|
|
@@ -379,60 +337,67 @@ export var useFlagship = function useFlagship() {
|
|
|
379
337
|
|
|
380
338
|
if (!state.status.isSdkReady && state.modifications) {
|
|
381
339
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
382
|
-
modifications = Array.from(state.modifications, function (
|
|
383
|
-
var
|
|
384
|
-
_key =
|
|
385
|
-
item =
|
|
340
|
+
modifications = Array.from(state.modifications, function (_ref3) {
|
|
341
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
342
|
+
_key = _ref4[0],
|
|
343
|
+
item = _ref4[1];
|
|
386
344
|
|
|
387
345
|
return item;
|
|
388
346
|
});
|
|
389
347
|
}
|
|
390
348
|
|
|
391
349
|
var activateModification = /*#__PURE__*/function () {
|
|
392
|
-
var
|
|
350
|
+
var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(params) {
|
|
393
351
|
var functionName;
|
|
394
|
-
return regeneratorRuntime.wrap(function
|
|
352
|
+
return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
395
353
|
while (1) {
|
|
396
|
-
switch (
|
|
354
|
+
switch (_context3.prev = _context3.next) {
|
|
397
355
|
case 0:
|
|
398
356
|
functionName = 'activateModification';
|
|
399
|
-
|
|
357
|
+
_context3.next = 3;
|
|
400
358
|
return fsActivate(params, functionName, visitor, config);
|
|
401
359
|
|
|
402
360
|
case 3:
|
|
403
361
|
case "end":
|
|
404
|
-
return
|
|
362
|
+
return _context3.stop();
|
|
405
363
|
}
|
|
406
364
|
}
|
|
407
|
-
},
|
|
365
|
+
}, _callee3);
|
|
408
366
|
}));
|
|
409
367
|
|
|
410
|
-
return function activateModification(
|
|
411
|
-
return
|
|
368
|
+
return function activateModification(_x6) {
|
|
369
|
+
return _ref5.apply(this, arguments);
|
|
412
370
|
};
|
|
413
371
|
}();
|
|
414
372
|
|
|
415
373
|
var synchronizeModifications = /*#__PURE__*/function () {
|
|
416
|
-
var
|
|
417
|
-
|
|
418
|
-
return regeneratorRuntime.wrap(function _callee6$(_context6) {
|
|
374
|
+
var _ref6 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
|
|
375
|
+
return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
|
419
376
|
while (1) {
|
|
420
|
-
switch (
|
|
377
|
+
switch (_context4.prev = _context4.next) {
|
|
421
378
|
case 0:
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
379
|
+
if (visitor) {
|
|
380
|
+
_context4.next = 3;
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
logWarn(config, noVisitorMessage, 'synchronizeModifications');
|
|
385
|
+
return _context4.abrupt("return");
|
|
425
386
|
|
|
426
387
|
case 3:
|
|
388
|
+
_context4.next = 5;
|
|
389
|
+
return visitor.synchronizeModifications();
|
|
390
|
+
|
|
391
|
+
case 5:
|
|
427
392
|
case "end":
|
|
428
|
-
return
|
|
393
|
+
return _context4.stop();
|
|
429
394
|
}
|
|
430
395
|
}
|
|
431
|
-
},
|
|
396
|
+
}, _callee4);
|
|
432
397
|
}));
|
|
433
398
|
|
|
434
399
|
return function synchronizeModifications() {
|
|
435
|
-
return
|
|
400
|
+
return _ref6.apply(this, arguments);
|
|
436
401
|
};
|
|
437
402
|
}();
|
|
438
403
|
|
|
@@ -456,7 +421,38 @@ export var useFlagship = function useFlagship() {
|
|
|
456
421
|
});
|
|
457
422
|
};
|
|
458
423
|
|
|
424
|
+
function getFlag(key, defaultValue) {
|
|
425
|
+
if (!visitor) {
|
|
426
|
+
return new Flag(defaultValue);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return visitor.getFlag(key, defaultValue);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function fetchFlags() {
|
|
433
|
+
if (!visitor) {
|
|
434
|
+
logWarn(config, noVisitorMessage, 'fetchFlags');
|
|
435
|
+
return Promise.resolve();
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return visitor.fetchFlags();
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function setConsent(hasConsented) {
|
|
442
|
+
if (!visitor) {
|
|
443
|
+
logWarn(config, noVisitorMessage, 'setConsent');
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
visitor.setConsent(hasConsented);
|
|
448
|
+
}
|
|
449
|
+
|
|
459
450
|
return {
|
|
451
|
+
visitorId: visitor === null || visitor === void 0 ? void 0 : visitor.visitorId,
|
|
452
|
+
anonymousId: visitor === null || visitor === void 0 ? void 0 : visitor.anonymousId,
|
|
453
|
+
context: _objectSpread({}, visitor === null || visitor === void 0 ? void 0 : visitor.context),
|
|
454
|
+
hasConsented: visitor === null || visitor === void 0 ? void 0 : visitor.hasConsented,
|
|
455
|
+
setConsent: setConsent,
|
|
460
456
|
updateContext: fsUpdateContext,
|
|
461
457
|
clearContext: fsClearContext,
|
|
462
458
|
authenticate: fsAuthenticate,
|
|
@@ -466,16 +462,13 @@ export var useFlagship = function useFlagship() {
|
|
|
466
462
|
synchronizeModifications: synchronizeModifications,
|
|
467
463
|
getModifications: getModifications,
|
|
468
464
|
modifications: modifications || [],
|
|
465
|
+
FlagsData: (visitor === null || visitor === void 0 ? void 0 : visitor.getFlagsDataArray()) || [],
|
|
469
466
|
getModificationInfo: getModificationInfo,
|
|
470
467
|
hit: {
|
|
471
468
|
send: fsSendHit,
|
|
472
469
|
sendMultiple: fsSendHits
|
|
473
|
-
}
|
|
470
|
+
},
|
|
471
|
+
getFlag: getFlag,
|
|
472
|
+
fetchFlags: fetchFlags
|
|
474
473
|
};
|
|
475
|
-
};
|
|
476
|
-
export var noVisitorMessage = 'sdk not correctly initialized... Make sure fsVisitor is ready.';
|
|
477
|
-
export var noVisitorDefault = 'fsVisitor not initialized, returns default value';
|
|
478
|
-
|
|
479
|
-
var reportNoVisitor = function reportNoVisitor(config, tag) {
|
|
480
|
-
logError(config, noVisitorMessage, tag);
|
|
481
474
|
};
|
package/dist/esm/utils.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { LogLevel } from '@flagship.io/js-sdk';
|
|
2
|
+
import { useEffect, useRef } from 'react';
|
|
2
3
|
export function logError(config, message, tag) {
|
|
3
4
|
if (!config || !config.logManager || typeof config.logManager.error !== 'function' || !config.logLevel || config.logLevel < LogLevel.ERROR) {
|
|
4
5
|
return;
|
|
@@ -20,6 +21,16 @@ export function logWarn(config, message, tag) {
|
|
|
20
21
|
|
|
21
22
|
config.logManager.warning(message, tag);
|
|
22
23
|
}
|
|
24
|
+
export function log(level, message, tag) {
|
|
25
|
+
var now = new Date(); // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
|
|
27
|
+
var getTwoDigit = function getTwoDigit(value) {
|
|
28
|
+
return value.toString().length === 1 ? "0".concat(value) : value;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
var out = "[".concat(getTwoDigit(now.getFullYear()), "-").concat(getTwoDigit(now.getMonth()), "-").concat(getTwoDigit(now.getDay()), " ").concat(getTwoDigit(now.getHours()), ":").concat(getTwoDigit(now.getMinutes()), "] [Flagship SDK] [").concat(LogLevel[level], "] [").concat(tag, "] : ").concat(message);
|
|
32
|
+
console.log(out);
|
|
33
|
+
}
|
|
23
34
|
export var getModificationsFromCampaigns = function getModificationsFromCampaigns(campaigns) {
|
|
24
35
|
var modifications = new Map();
|
|
25
36
|
|
|
@@ -43,4 +54,24 @@ export var getModificationsFromCampaigns = function getModificationsFromCampaign
|
|
|
43
54
|
}
|
|
44
55
|
});
|
|
45
56
|
return modifications;
|
|
46
|
-
};
|
|
57
|
+
};
|
|
58
|
+
export function uuidV4() {
|
|
59
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (char) {
|
|
60
|
+
var rand = Math.random() * 16 | 0;
|
|
61
|
+
var value = char === 'x' ? rand : rand & 0x3 | 0x8;
|
|
62
|
+
return value.toString(16);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
export function useNonInitialEffect(effect, deps) {
|
|
66
|
+
var initialRender = useRef(true);
|
|
67
|
+
useEffect(function () {
|
|
68
|
+
if (initialRender.current) {
|
|
69
|
+
initialRender.current = false;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (typeof effect === 'function') {
|
|
74
|
+
return effect();
|
|
75
|
+
}
|
|
76
|
+
}, deps);
|
|
77
|
+
}
|