@imtbl/sdk 1.5.2 → 1.5.3-alpha

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/dist/config.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import axios from 'axios';
2
+ import { memorise } from 'lru-memorise';
3
+ import { getGlobalisedValue as getGlobalisedValue$1 } from 'global-const';
2
4
 
3
5
  const isNode = () => typeof window === 'undefined';
4
6
  const isBrowser = () => !isNode();
@@ -13,12 +15,6 @@ var Detail;
13
15
  Detail["DOMAIN"] = "domain";
14
16
  Detail["SDK_VERSION"] = "sdkVersion";
15
17
  })(Detail || (Detail = {}));
16
- var Store;
17
- (function (Store) {
18
- Store["EVENTS"] = "events";
19
- Store["RUNTIME"] = "runtime";
20
- })(Store || (Store = {}));
21
- const POLLING_FREQUENCY = 5000;
22
18
 
23
19
  const IMTBL_API = 'https://api.x.immutable.com';
24
20
  async function post(path, data) {
@@ -78,6 +74,11 @@ const setItem = (key, payload) => {
78
74
  return false;
79
75
  };
80
76
 
77
+ var Store;
78
+ (function (Store) {
79
+ Store["EVENTS"] = "events";
80
+ Store["RUNTIME"] = "runtime";
81
+ })(Store || (Store = {}));
81
82
  // In memory storage for events and other data
82
83
  let EVENT_STORE;
83
84
  let RUNTIME_DETAILS;
@@ -127,9 +128,7 @@ const flattenProperties = (properties) => {
127
128
  };
128
129
 
129
130
  // WARNING: DO NOT CHANGE THE STRING BELOW. IT GETS REPLACED AT BUILD TIME.
130
- const SDK_VERSION = '1.5.2';
131
- let initialised = false;
132
- const isInitialised = () => initialised;
131
+ const SDK_VERSION = '1.5.3-alpha';
133
132
  const getReferrer = () => {
134
133
  if (isNode()) {
135
134
  return '';
@@ -166,21 +165,28 @@ const getRuntimeDetails = () => {
166
165
  screen: `${window.screen.width}x${window.screen.height}`,
167
166
  };
168
167
  };
168
+ let initialised = false;
169
+ const isInitialised = () => initialised;
169
170
  const initialise = async () => {
170
- const runtimeDetails = flattenProperties(getRuntimeDetails());
171
- const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
172
- const body = {
173
- version: 1,
174
- data: {
175
- runtimeDetails,
176
- runtimeId: existingRuntimeId,
177
- },
178
- };
179
- const response = await post('/v1/sdk/initialise', body);
180
- // Get runtimeId and store it
181
- const { runtimeId } = response;
182
- storeDetail(Detail.RUNTIME_ID, runtimeId);
183
171
  initialised = true;
172
+ try {
173
+ const runtimeDetails = flattenProperties(getRuntimeDetails());
174
+ const existingRuntimeId = getDetail(Detail.RUNTIME_ID);
175
+ const body = {
176
+ version: 1,
177
+ data: {
178
+ runtimeDetails,
179
+ runtimeId: existingRuntimeId,
180
+ },
181
+ };
182
+ const response = await post('/v1/sdk/initialise', body);
183
+ // Get runtimeId and store it
184
+ const { runtimeId } = response;
185
+ storeDetail(Detail.RUNTIME_ID, runtimeId);
186
+ }
187
+ catch (error) {
188
+ initialised = false;
189
+ }
184
190
  };
185
191
 
186
192
  function errorBoundary(fn) {
@@ -212,6 +218,21 @@ function isTestEnvironment() {
212
218
  return process.env.JEST_WORKER_ID !== undefined;
213
219
  }
214
220
 
221
+ const GLOBALISE_KEY = 'imtbl__metrics';
222
+ const MEMORISE_TIMEFRAME = 1000;
223
+ const MEMORISE_MAX = 1000;
224
+ const getGlobalisedValue = (key, value) => getGlobalisedValue$1(GLOBALISE_KEY, key, value);
225
+ const getGlobalisedCachedFunction = (key, fn) => {
226
+ // Some applications (esp backend, or frontends using the split bundles) can sometimes
227
+ // initialise the same request multiple times. This will prevent multiple of the
228
+ // same event,value from being reported in a 1 second period.
229
+ const memorisedFn = memorise(fn, {
230
+ lruOptions: { ttl: MEMORISE_TIMEFRAME, max: MEMORISE_MAX },
231
+ });
232
+ return getGlobalisedValue$1(GLOBALISE_KEY, key, memorisedFn);
233
+ };
234
+
235
+ const POLLING_FREQUENCY = 5000;
215
236
  // Store the event in the event store
216
237
  const trackFn = (moduleName, eventName, properties) => {
217
238
  const event = {
@@ -221,7 +242,7 @@ const trackFn = (moduleName, eventName, properties) => {
221
242
  };
222
243
  addEvent(event);
223
244
  };
224
- const track = errorBoundary(trackFn);
245
+ const track = errorBoundary(getGlobalisedCachedFunction('track', trackFn));
225
246
  // Sending events to the server
226
247
  const flushFn = async () => {
227
248
  // Don't flush if not initialised
@@ -257,19 +278,31 @@ const flushPoll = async () => {
257
278
  await flush();
258
279
  setTimeout(flushPoll, POLLING_FREQUENCY);
259
280
  };
281
+ let flushingStarted = false;
282
+ const startFlushing = () => {
283
+ if (flushingStarted) {
284
+ return;
285
+ }
286
+ flushingStarted = true;
287
+ flushPoll();
288
+ };
260
289
  // This will get initialised when module is imported.
261
290
  if (!isTestEnvironment()) {
262
- errorBoundary(flushPoll)();
291
+ errorBoundary(getGlobalisedValue('startFlushing', startFlushing))();
263
292
  }
264
293
 
265
294
  const setEnvironmentFn = (env) => {
266
295
  storeDetail(Detail.ENVIRONMENT, env);
267
296
  };
268
- const setEnvironment = errorBoundary(setEnvironmentFn);
297
+ const setEnvironment = errorBoundary(getGlobalisedValue('setEnvironment', setEnvironmentFn));
298
+ const setPassportClientIdFn = (passportClientId) => {
299
+ storeDetail(Detail.PASSPORT_CLIENT_ID, passportClientId);
300
+ };
301
+ errorBoundary(getGlobalisedValue('setPassportClientId', setPassportClientIdFn));
269
302
  const setPublishableApiKeyFn = (publishableApiKey) => {
270
303
  storeDetail(Detail.PUBLISHABLE_API_KEY, publishableApiKey);
271
304
  };
272
- const setPublishableApiKey = errorBoundary(setPublishableApiKeyFn);
305
+ const setPublishableApiKey = errorBoundary(getGlobalisedValue('setPublishableApiKey', setPublishableApiKeyFn));
273
306
 
274
307
  var Environment;
275
308
  (function (Environment) {