@statsig/client-core 0.0.1-beta.5 → 0.0.1-beta.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statsig/client-core",
3
- "version": "0.0.1-beta.5",
3
+ "version": "0.0.1-beta.7",
4
4
  "dependencies": {},
5
5
  "type": "commonjs",
6
6
  "main": "./src/index.js",
@@ -4,7 +4,8 @@ import { StatsigEvent } from './StatsigEvent';
4
4
  import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes';
5
5
  import { StatsigUser } from './StatsigUser';
6
6
  export interface StatsigClientCommonInterface extends StatsigClientEventEmitterInterface {
7
- initialize(): Promise<void>;
7
+ initializeSync(): void;
8
+ initializeAsync(): Promise<void>;
8
9
  shutdown(): Promise<void>;
9
10
  }
10
11
  export interface OnDeviceEvaluationsInterface extends StatsigClientCommonInterface {
@@ -17,7 +18,8 @@ export interface OnDeviceEvaluationsInterface extends StatsigClientCommonInterfa
17
18
  }
18
19
  export interface PrecomputedEvaluationsInterface extends StatsigClientCommonInterface {
19
20
  getCurrentUser(): StatsigUser;
20
- updateUser(user: StatsigUser): Promise<void>;
21
+ updateUserSync(user: StatsigUser): void;
22
+ updateUserAsync(user: StatsigUser): Promise<void>;
21
23
  checkGate(name: string, options: EvaluationOptions): boolean;
22
24
  getFeatureGate(name: string, options: EvaluationOptions): FeatureGate;
23
25
  getDynamicConfig(name: string, options: EvaluationOptions): DynamicConfig;
@@ -13,14 +13,16 @@ export declare class EventLogger {
13
13
  private _lastExposureMap;
14
14
  private _maxQueueSize;
15
15
  private _failedLogs;
16
- private _hasFiredQuickFlush;
16
+ private _hasRunQuickFlush;
17
+ private _creationTime;
17
18
  constructor(_sdkKey: string, _emitter: StatsigClientEmitEventFunc, _network: NetworkCore, _options: StatsigOptionsCommon | null);
18
19
  enqueue(event: StatsigEventInternal): void;
19
20
  reset(): void;
20
21
  onVisibilityChanged(visibility: Visibility): void;
21
22
  shutdown(): Promise<void>;
22
23
  /**
23
- * We 'Quick Flush' following the very first enqueued event
24
+ * We 'Quick Flush' following the very first event enqueued
25
+ * within the quick flush window
24
26
  */
25
27
  private _quickFlushIfNeeded;
26
28
  private _shouldLogEvent;
@@ -60,6 +60,7 @@ var MAX_DEDUPER_KEYS = 1000;
60
60
  var DEDUPER_WINDOW_DURATION_MS = 60000;
61
61
  var MAX_FAILED_LOGS = 500;
62
62
  var DEFAULT_API = 'https://api.statsig.com/v1';
63
+ var QUICK_FLUSH_WINDOW_MS = 200;
63
64
  var EventLogger = /** @class */ (function () {
64
65
  function EventLogger(_sdkKey, _emitter, _network, _options) {
65
66
  var _this = this;
@@ -70,7 +71,8 @@ var EventLogger = /** @class */ (function () {
70
71
  this._options = _options;
71
72
  this._queue = [];
72
73
  this._lastExposureMap = {};
73
- this._hasFiredQuickFlush = false;
74
+ this._hasRunQuickFlush = false;
75
+ this._creationTime = Date.now();
74
76
  this._maxQueueSize = (_a = _options === null || _options === void 0 ? void 0 : _options.loggingBufferMaxSize) !== null && _a !== void 0 ? _a : DEFAULT_QUEUE_SIZE;
75
77
  var flushInterval = (_b = _options === null || _options === void 0 ? void 0 : _options.loggingIntervalMs) !== null && _b !== void 0 ? _b : DEFAULT_FLUSH_INTERVAL_MS;
76
78
  this._flushTimer = setInterval(function () { return _this._flushAndForget(); }, flushInterval);
@@ -119,15 +121,19 @@ var EventLogger = /** @class */ (function () {
119
121
  });
120
122
  };
121
123
  /**
122
- * We 'Quick Flush' following the very first enqueued event
124
+ * We 'Quick Flush' following the very first event enqueued
125
+ * within the quick flush window
123
126
  */
124
127
  EventLogger.prototype._quickFlushIfNeeded = function () {
125
128
  var _this = this;
126
- if (this._hasFiredQuickFlush) {
129
+ if (this._hasRunQuickFlush) {
127
130
  return;
128
131
  }
129
- this._hasFiredQuickFlush = true;
130
- setTimeout(function () { return _this._flushAndForget(); }, 200);
132
+ this._hasRunQuickFlush = true;
133
+ if (Date.now() - this._creationTime > QUICK_FLUSH_WINDOW_MS) {
134
+ return;
135
+ }
136
+ setTimeout(function () { return _this._flushAndForget(); }, QUICK_FLUSH_WINDOW_MS);
131
137
  };
132
138
  EventLogger.prototype._shouldLogEvent = function (event) {
133
139
  var _a, _b, _c, _d;
@@ -233,8 +239,8 @@ var EventLogger = /** @class */ (function () {
233
239
  })];
234
240
  case 1:
235
241
  result = _a.sent();
236
- if (result) {
237
- return [2 /*return*/, JSON.parse(result)];
242
+ if (result === null || result === void 0 ? void 0 : result.body) {
243
+ return [2 /*return*/, JSON.parse(result.body)];
238
244
  }
239
245
  return [2 /*return*/, { success: false }];
240
246
  }
@@ -11,13 +11,17 @@ type RequestArgs = {
11
11
  type RequestArgsWithData = RequestArgs & {
12
12
  data: Record<string, unknown>;
13
13
  };
14
+ type NetworkResponse = {
15
+ body: string | null;
16
+ code: number;
17
+ };
14
18
  export declare class NetworkCore {
15
19
  private _options;
16
20
  private _emitter?;
17
21
  private readonly _timeout;
18
22
  constructor(_options: StatsigOptionsCommon | null, _emitter?: StatsigClientEmitEventFunc | undefined);
19
- post(args: RequestArgsWithData): Promise<string | null>;
20
- get(args: RequestArgs): Promise<string | null>;
23
+ post(args: RequestArgsWithData): Promise<NetworkResponse | null>;
24
+ get(args: RequestArgs): Promise<NetworkResponse | null>;
21
25
  beacon(args: RequestArgsWithData): boolean;
22
26
  private _sendRequest;
23
27
  private _getPopulatedURL;
@@ -149,7 +149,10 @@ var NetworkCore = /** @class */ (function () {
149
149
  status: response.status,
150
150
  contentLength: response.headers.get('content-length'),
151
151
  });
152
- return [2 /*return*/, text];
152
+ return [2 /*return*/, {
153
+ body: text,
154
+ code: response.status,
155
+ }];
153
156
  case 4:
154
157
  error_1 = _b.sent();
155
158
  errorMessage = _getErrorMessage(controller, error_1);
package/src/StableID.js CHANGED
@@ -64,7 +64,7 @@ exports.StableID = {
64
64
  },
65
65
  };
66
66
  function _getStableIDStorageKey(sdkKey) {
67
- return "statsig.stable_id:".concat((0, Hashing_1.DJB2)(sdkKey));
67
+ return "statsig.stable_id.".concat((0, Hashing_1.DJB2)(sdkKey));
68
68
  }
69
69
  function _persistToStorage(stableID, sdkKey) {
70
70
  var storageKey = _getStableIDStorageKey(sdkKey);
@@ -3,35 +3,28 @@ import { ErrorBoundary } from './ErrorBoundary';
3
3
  import { EventLogger } from './EventLogger';
4
4
  import { NetworkCore } from './NetworkCore';
5
5
  import { StatsigClientEvent, StatsigClientEventCallback, StatsigClientEventData, StatsigClientEventEmitterInterface, StatsigLoadingStatus } from './StatsigClientEventEmitter';
6
- import { DataSource, StatsigDataProvider } from './StatsigDataProvider';
6
+ import { StatsigDataAdapter, StatsigDataAdapterResult } from './StatsigDataAdapter';
7
7
  import { StatsigEventInternal } from './StatsigEvent';
8
8
  import { StatsigOptionsCommon } from './StatsigOptionsCommon';
9
9
  import { StatsigUser } from './StatsigUser';
10
- type DataProviderResult = {
11
- data: string | null;
12
- source: DataSource;
13
- };
14
10
  export type EvaluationOptions = {
15
11
  disableExposureLog?: boolean;
16
12
  };
17
13
  export declare const DEFAULT_EVAL_OPTIONS: EvaluationOptions;
18
14
  export type StatsigClientEmitEventFunc = (data: StatsigClientEventData) => void;
19
- export declare class StatsigClientBase implements StatsigClientEventEmitterInterface {
15
+ export declare abstract class StatsigClientBase implements StatsigClientEventEmitterInterface {
16
+ protected readonly _sdkKey: string;
17
+ protected readonly _adapter: StatsigDataAdapter;
20
18
  loadingStatus: StatsigLoadingStatus;
21
- protected _errorBoundary: ErrorBoundary;
22
- protected _logger: EventLogger;
23
- protected _sdkKey: string;
24
- protected _dataProviders: StatsigDataProvider[];
19
+ protected readonly _errorBoundary: ErrorBoundary;
20
+ protected readonly _logger: EventLogger;
25
21
  private _listeners;
26
- constructor(sdkKey: string, network: NetworkCore, options: StatsigOptionsCommon | null, dataProviders: StatsigDataProvider[]);
22
+ constructor(_sdkKey: string, _adapter: StatsigDataAdapter, network: NetworkCore, options: StatsigOptionsCommon | null);
27
23
  on(event: StatsigClientEvent | '*', listener: StatsigClientEventCallback): void;
28
24
  off(event: StatsigClientEvent | '*', listener: StatsigClientEventCallback): void;
25
+ getDataAdapter(): StatsigDataAdapter;
29
26
  protected emit(data: StatsigClientEventData): void;
30
27
  protected _setStatus(newStatus: StatsigLoadingStatus): void;
31
- protected _getDataFromProviders(user?: StatsigUser): DataProviderResult;
32
- protected _getDataFromProvidersAsync(user?: StatsigUser): Promise<DataProviderResult>;
33
- protected _getDataPostInitFromProviders(currentData: string | null, user?: StatsigUser): Promise<DataProviderResult>;
34
- protected _saveToDataProviders(currentData: string | null, user?: StatsigUser): void;
35
28
  protected _enqueueExposure(options: EvaluationOptions, exposure: StatsigEventInternal): void;
29
+ protected _runPostUpdate(current: StatsigDataAdapterResult | null, user?: StatsigUser): void;
36
30
  }
37
- export {};
@@ -1,47 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __asyncValues = (this && this.__asyncValues) || function (o) {
39
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
40
- var m = o[Symbol.asyncIterator], i;
41
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
42
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
43
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
44
- };
45
2
  Object.defineProperty(exports, "__esModule", { value: true });
46
3
  exports.StatsigClientBase = exports.DEFAULT_EVAL_OPTIONS = void 0;
47
4
  require("./$_StatsigGlobal");
@@ -53,22 +10,23 @@ exports.DEFAULT_EVAL_OPTIONS = {
53
10
  disableExposureLog: false,
54
11
  };
55
12
  var StatsigClientBase = /** @class */ (function () {
56
- function StatsigClientBase(sdkKey, network, options, dataProviders) {
13
+ function StatsigClientBase(_sdkKey, _adapter, network, options) {
57
14
  var _a, _b;
15
+ this._sdkKey = _sdkKey;
16
+ this._adapter = _adapter;
58
17
  this.loadingStatus = 'Uninitialized';
59
18
  this._listeners = {};
60
- this._logger = new EventLogger_1.EventLogger(sdkKey, this.emit.bind(this), network, options);
61
- this._sdkKey = sdkKey;
62
- this._errorBoundary = new ErrorBoundary_1.ErrorBoundary(sdkKey);
19
+ Log_1.Log.level = (_a = options === null || options === void 0 ? void 0 : options.logLevel) !== null && _a !== void 0 ? _a : Log_1.LogLevel.Warn;
20
+ this._logger = new EventLogger_1.EventLogger(_sdkKey, this.emit.bind(this), network, options);
21
+ this._errorBoundary = new ErrorBoundary_1.ErrorBoundary(_sdkKey);
63
22
  if (options === null || options === void 0 ? void 0 : options.overrideStableID) {
64
- StableID_1.StableID.setOverride(options.overrideStableID, sdkKey);
23
+ StableID_1.StableID.setOverride(options.overrideStableID, _sdkKey);
65
24
  }
66
25
  __STATSIG__ = __STATSIG__ !== null && __STATSIG__ !== void 0 ? __STATSIG__ : {};
67
- var instances = (_a = __STATSIG__.instances) !== null && _a !== void 0 ? _a : new Set();
26
+ var instances = (_b = __STATSIG__.instances) !== null && _b !== void 0 ? _b : new Set();
68
27
  instances.add(this);
69
28
  __STATSIG__.instances = instances;
70
- Log_1.Log.level = (_b = options === null || options === void 0 ? void 0 : options.logLevel) !== null && _b !== void 0 ? _b : Log_1.LogLevel.Warn;
71
- this._dataProviders = dataProviders;
29
+ this._adapter.attach(_sdkKey, options);
72
30
  }
73
31
  StatsigClientBase.prototype.on = function (event, listener) {
74
32
  if (!this._listeners[event]) {
@@ -84,6 +42,9 @@ var StatsigClientBase = /** @class */ (function () {
84
42
  }
85
43
  }
86
44
  };
45
+ StatsigClientBase.prototype.getDataAdapter = function () {
46
+ return this._adapter;
47
+ };
87
48
  StatsigClientBase.prototype.emit = function (data) {
88
49
  var _a;
89
50
  if (this._listeners[data.event]) {
@@ -95,178 +56,17 @@ var StatsigClientBase = /** @class */ (function () {
95
56
  this.loadingStatus = newStatus;
96
57
  this.emit({ event: 'status_change', loadingStatus: newStatus });
97
58
  };
98
- StatsigClientBase.prototype._getDataFromProviders = function (user) {
99
- var _a;
100
- for (var _i = 0, _b = this._dataProviders; _i < _b.length; _i++) {
101
- var provider = _b[_i];
102
- var data = (_a = provider.getData) === null || _a === void 0 ? void 0 : _a.call(provider, this._sdkKey, user);
103
- if (data) {
104
- return { data: data, source: provider.source };
105
- }
106
- }
107
- return { data: null, source: 'NoValues' };
108
- };
109
- StatsigClientBase.prototype._getDataFromProvidersAsync = function (user) {
110
- var _a, e_1, _b, _c;
111
- var _d;
112
- return __awaiter(this, void 0, void 0, function () {
113
- var _e, _f, _g, provider, data, e_1_1;
114
- return __generator(this, function (_h) {
115
- switch (_h.label) {
116
- case 0:
117
- _h.trys.push([0, 6, 7, 12]);
118
- _e = true, _f = __asyncValues(this._dataProviders);
119
- _h.label = 1;
120
- case 1: return [4 /*yield*/, _f.next()];
121
- case 2:
122
- if (!(_g = _h.sent(), _a = _g.done, !_a)) return [3 /*break*/, 5];
123
- _c = _g.value;
124
- _e = false;
125
- provider = _c;
126
- return [4 /*yield*/, ((_d = provider.getDataAsync) === null || _d === void 0 ? void 0 : _d.call(provider, this._sdkKey, user))];
127
- case 3:
128
- data = _h.sent();
129
- if (data) {
130
- return [2 /*return*/, { data: data, source: provider.source }];
131
- }
132
- _h.label = 4;
133
- case 4:
134
- _e = true;
135
- return [3 /*break*/, 1];
136
- case 5: return [3 /*break*/, 12];
137
- case 6:
138
- e_1_1 = _h.sent();
139
- e_1 = { error: e_1_1 };
140
- return [3 /*break*/, 12];
141
- case 7:
142
- _h.trys.push([7, , 10, 11]);
143
- if (!(!_e && !_a && (_b = _f.return))) return [3 /*break*/, 9];
144
- return [4 /*yield*/, _b.call(_f)];
145
- case 8:
146
- _h.sent();
147
- _h.label = 9;
148
- case 9: return [3 /*break*/, 11];
149
- case 10:
150
- if (e_1) throw e_1.error;
151
- return [7 /*endfinally*/];
152
- case 11: return [7 /*endfinally*/];
153
- case 12: return [2 /*return*/, { data: null, source: 'NoValues' }];
154
- }
155
- });
156
- });
157
- };
158
- StatsigClientBase.prototype._getDataPostInitFromProviders = function (currentData, user) {
159
- var _a, e_2, _b, _c;
160
- var _d;
161
- return __awaiter(this, void 0, void 0, function () {
162
- var _e, _f, _g, provider, data, e_2_1;
163
- return __generator(this, function (_h) {
164
- switch (_h.label) {
165
- case 0:
166
- _h.trys.push([0, 6, 7, 12]);
167
- _e = true, _f = __asyncValues(this._dataProviders);
168
- _h.label = 1;
169
- case 1: return [4 /*yield*/, _f.next()];
170
- case 2:
171
- if (!(_g = _h.sent(), _a = _g.done, !_a)) return [3 /*break*/, 5];
172
- _c = _g.value;
173
- _e = false;
174
- provider = _c;
175
- return [4 /*yield*/, ((_d = provider.getDataPostInit) === null || _d === void 0 ? void 0 : _d.call(provider, this._sdkKey, currentData, user))];
176
- case 3:
177
- data = _h.sent();
178
- if (data) {
179
- return [2 /*return*/, { data: data, source: provider.source }];
180
- }
181
- _h.label = 4;
182
- case 4:
183
- _e = true;
184
- return [3 /*break*/, 1];
185
- case 5: return [3 /*break*/, 12];
186
- case 6:
187
- e_2_1 = _h.sent();
188
- e_2 = { error: e_2_1 };
189
- return [3 /*break*/, 12];
190
- case 7:
191
- _h.trys.push([7, , 10, 11]);
192
- if (!(!_e && !_a && (_b = _f.return))) return [3 /*break*/, 9];
193
- return [4 /*yield*/, _b.call(_f)];
194
- case 8:
195
- _h.sent();
196
- _h.label = 9;
197
- case 9: return [3 /*break*/, 11];
198
- case 10:
199
- if (e_2) throw e_2.error;
200
- return [7 /*endfinally*/];
201
- case 11: return [7 /*endfinally*/];
202
- case 12: return [2 /*return*/, { data: null, source: 'NoValues' }];
203
- }
204
- });
205
- });
206
- };
207
- StatsigClientBase.prototype._saveToDataProviders = function (currentData, user) {
208
- var _this = this;
209
- (function () { return __awaiter(_this, void 0, void 0, function () {
210
- var latest, data, _a, _b, _c, provider, e_3_1;
211
- var _d, e_3, _e, _f;
212
- var _g, _h;
213
- return __generator(this, function (_j) {
214
- switch (_j.label) {
215
- case 0: return [4 /*yield*/, this._getDataPostInitFromProviders(currentData, user)];
216
- case 1:
217
- latest = _j.sent();
218
- data = (_g = latest.data) !== null && _g !== void 0 ? _g : currentData;
219
- if (!data) {
220
- return [2 /*return*/];
221
- }
222
- _j.label = 2;
223
- case 2:
224
- _j.trys.push([2, 8, 9, 14]);
225
- _a = true, _b = __asyncValues(this._dataProviders);
226
- _j.label = 3;
227
- case 3: return [4 /*yield*/, _b.next()];
228
- case 4:
229
- if (!(_c = _j.sent(), _d = _c.done, !_d)) return [3 /*break*/, 7];
230
- _f = _c.value;
231
- _a = false;
232
- provider = _f;
233
- return [4 /*yield*/, ((_h = provider.setDataPostInit) === null || _h === void 0 ? void 0 : _h.call(provider, this._sdkKey, data, user))];
234
- case 5:
235
- _j.sent();
236
- _j.label = 6;
237
- case 6:
238
- _a = true;
239
- return [3 /*break*/, 3];
240
- case 7: return [3 /*break*/, 14];
241
- case 8:
242
- e_3_1 = _j.sent();
243
- e_3 = { error: e_3_1 };
244
- return [3 /*break*/, 14];
245
- case 9:
246
- _j.trys.push([9, , 12, 13]);
247
- if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 11];
248
- return [4 /*yield*/, _e.call(_b)];
249
- case 10:
250
- _j.sent();
251
- _j.label = 11;
252
- case 11: return [3 /*break*/, 13];
253
- case 12:
254
- if (e_3) throw e_3.error;
255
- return [7 /*endfinally*/];
256
- case 13: return [7 /*endfinally*/];
257
- case 14: return [2 /*return*/];
258
- }
259
- });
260
- }); })().catch(function (error) {
261
- _this.emit({ event: 'error', error: error });
262
- });
263
- };
264
59
  StatsigClientBase.prototype._enqueueExposure = function (options, exposure) {
265
60
  if (options.disableExposureLog === true) {
266
61
  return;
267
62
  }
268
63
  this._logger.enqueue(exposure);
269
64
  };
65
+ StatsigClientBase.prototype._runPostUpdate = function (current, user) {
66
+ this._adapter.getDataAsync(current, user).catch(function (err) {
67
+ Log_1.Log.error('An error occurred after update.', err);
68
+ });
69
+ };
270
70
  return StatsigClientBase;
271
71
  }());
272
72
  exports.StatsigClientBase = StatsigClientBase;
@@ -0,0 +1,40 @@
1
+ import { StatsigOptionsCommon } from './StatsigOptionsCommon';
2
+ import { StatsigUser } from './StatsigUser';
3
+ export type DataSource = 'Uninitialized' | 'Loading' | 'NoValues' | 'Cache' | 'Network' | 'NetworkNotModified' | 'Bootstrap';
4
+ export type StatsigDataAdapterResult = {
5
+ readonly source: DataSource;
6
+ readonly data: string;
7
+ };
8
+ /**
9
+ * Describes a type that is used during intialize/update operations of a Statsig client.
10
+ *
11
+ * See below to find the default adapters, but know that it is possible to create your
12
+ * own StatsigDataAdapter and provide it via {@link StatsigOptions.dataAdapter}.
13
+ *
14
+ * Defaults:
15
+ *
16
+ * - {@link PrecomputedEvaluationsClient} uses {@link EvaluationsDataAdapter}
17
+ *
18
+ * - {@link OnDeviceEvaluationsClient} uses {@link SpecsDataAdapter}
19
+ */
20
+ export type StatsigDataAdapter = {
21
+ /**
22
+ * Called when the StatsigDataAdapter is attached to the Statsig client instance during construction.
23
+ * @param {string} sdkKey The SDK key being used by the Statsig client.
24
+ * @param {StatsigOptionsCommon | null} options The StatsigOptions being used by the Statsig client.
25
+ */
26
+ readonly attach: (sdkKey: string, options: StatsigOptionsCommon | null) => void;
27
+ /**
28
+ * Synchronously get data for the given user (if any). Called during initializeSync and/or updateUserSync.
29
+ * It is also called during async update operations before StatsigDataAdapter.getDataAsync is called.
30
+ * @param {StatsigUser | undefined} user The StatsigUser to get data for.
31
+ * @returns {StatsigDataAdapterResult | null} The data that was found for the given StatsigUser.
32
+ */
33
+ readonly getDataSync: (user?: StatsigUser) => StatsigDataAdapterResult | null;
34
+ /**
35
+ * Asynchronously get data for the given user (if any). Called during initializeAsync and/or updateUserAsync.
36
+ * @param {StatsigUser | undefined} user The StatsigUser to get data for.
37
+ * @returns {StatsigDataAdapterResult | null} The data that was found for the given StatsigUser.
38
+ */
39
+ readonly getDataAsync: (current: StatsigDataAdapterResult | null, user?: StatsigUser) => Promise<StatsigDataAdapterResult | null>;
40
+ };
@@ -12,7 +12,7 @@ var __assign = (this && this.__assign) || function () {
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.StatsigMetadataProvider = void 0;
15
- var SDK_VERSION = '0.0.1-beta.5';
15
+ var SDK_VERSION = '0.0.1-beta.7';
16
16
  var metadata = {
17
17
  sdkVersion: SDK_VERSION,
18
18
  sdkType: 'js-mono', // js-mono is overwritten by Precomp and OnDevice clients
@@ -1,5 +1,5 @@
1
1
  import { LogLevel } from './Log';
2
- import { StatsigDataProvider } from './StatsigDataProvider';
2
+ import { StatsigDataAdapter } from './StatsigDataAdapter';
3
3
  /** Common options for configuring the Statsig SDK. */
4
4
  export type StatsigOptionsCommon = {
5
5
  /**
@@ -22,10 +22,10 @@ export type StatsigOptionsCommon = {
22
22
  */
23
23
  logLevel?: LogLevel;
24
24
  /**
25
- * StatsigDataProvider implementors used to customize the initialization flow.
26
- * Default: LocalStorageCache then Network
25
+ * StatsigDataAdapter implementor used to customize the initialization/update flow.
26
+ * Default: EvaluationsDataAdapter (Precomputed) or SpecsDataAdapter (OnDevice)
27
27
  */
28
- dataProviders?: StatsigDataProvider[];
28
+ dataAdapter?: StatsigDataAdapter;
29
29
  /**
30
30
  * The maximum amount of time (in milliseconds) that any network request can take
31
31
  * before timing out. Default: 10,000 (10 seconds)
@@ -16,8 +16,7 @@ function normalizeUser(original, environment) {
16
16
  }
17
17
  exports.normalizeUser = normalizeUser;
18
18
  function getUserStorageKey(sdkKey, user) {
19
- var key = (0, Hashing_1.DJB2)(JSON.stringify(_getSortedObject({ sdkKey: sdkKey, user: user })));
20
- return "statsig.user_cache.".concat(key);
19
+ return (0, Hashing_1.DJB2)(JSON.stringify(_getSortedObject({ sdkKey: sdkKey, user: user })));
21
20
  }
22
21
  exports.getUserStorageKey = getUserStorageKey;
23
22
  function _getSortedObject(object) {
package/src/index.d.ts CHANGED
@@ -12,7 +12,7 @@ export * from './NetworkCore';
12
12
  export * from './StableID';
13
13
  export * from './StatsigClientBase';
14
14
  export * from './StatsigClientEventEmitter';
15
- export * from './StatsigDataProvider';
15
+ export * from './StatsigDataAdapter';
16
16
  export * from './StatsigEvent';
17
17
  export * from './StatsigMetadata';
18
18
  export * from './StatsigOptionsCommon';
package/src/index.js CHANGED
@@ -41,7 +41,7 @@ __exportStar(require("./NetworkCore"), exports);
41
41
  __exportStar(require("./StableID"), exports);
42
42
  __exportStar(require("./StatsigClientBase"), exports);
43
43
  __exportStar(require("./StatsigClientEventEmitter"), exports);
44
- __exportStar(require("./StatsigDataProvider"), exports);
44
+ __exportStar(require("./StatsigDataAdapter"), exports);
45
45
  __exportStar(require("./StatsigEvent"), exports);
46
46
  __exportStar(require("./StatsigMetadata"), exports);
47
47
  __exportStar(require("./StatsigOptionsCommon"), exports);
@@ -1,9 +0,0 @@
1
- import { StatsigUser } from './StatsigUser';
2
- export type DataSource = 'Uninitialized' | 'Loading' | 'NoValues' | 'Cache' | 'Network' | 'Bootstrap' | 'Prefetch';
3
- export type StatsigDataProvider = {
4
- readonly getData?: (sdkKey: string, user?: StatsigUser) => string | null;
5
- readonly getDataAsync?: (sdkKey: string, user?: StatsigUser) => Promise<string | null>;
6
- readonly getDataPostInit?: (sdkKey: string, currentData: string | null, user?: StatsigUser) => Promise<string | null>;
7
- readonly setDataPostInit?: (sdkKey: string, data: string, user?: StatsigUser) => Promise<void>;
8
- readonly source: DataSource;
9
- };