@ninetailed/experience.js 1.5.0 → 1.5.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/index.d.ts CHANGED
@@ -2,4 +2,4 @@ export * from './lib/analytics';
2
2
  export * from './lib/Ninetailed';
3
3
  export * from './lib/selectVariant';
4
4
  export * from './lib/types';
5
- export { Profile } from '@ninetailed/experience.js-shared';
5
+ export type { Profile } from '@ninetailed/experience.js-shared';
package/index.esm.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import get$2 from 'lodash/get';
2
- import throttle from 'lodash/throttle';
3
2
  import unionBy from 'lodash/unionBy';
4
3
  import { buildEmptyCache, buildTrackEvent, buildIdentifyEvent, buildPageEvent } from '@ninetailed/experience.js-shared';
5
4
  import require$$1 from 'tty';
@@ -3439,7 +3438,37 @@ const NINETAILED_TRACKER_EVENTS = {
3439
3438
  const PLUGIN_NAME = 'ninetailed';
3440
3439
 
3441
3440
  const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
3441
+ /**
3442
+ * Similar to _.throttle but waits for the promise to resolve. There is no
3443
+ * wait time because you can simply await `Promise.timeout` inside `fn`
3444
+ * to wait some time before the next call.
3445
+ */
3446
+
3447
+
3448
+ function asyncThrottle(fn) {
3449
+ let runningPromise;
3450
+ let queuedPromise;
3451
+ let nextArgs;
3452
+ return args => __awaiter(this, void 0, void 0, function* () {
3453
+ if (runningPromise) {
3454
+ nextArgs = args;
3442
3455
 
3456
+ if (queuedPromise) {
3457
+ return queuedPromise;
3458
+ } else {
3459
+ queuedPromise = runningPromise.then(() => {
3460
+ queuedPromise = undefined;
3461
+ runningPromise = fn(nextArgs);
3462
+ return runningPromise;
3463
+ });
3464
+ return queuedPromise;
3465
+ }
3466
+ } else {
3467
+ runningPromise = fn(args);
3468
+ return runningPromise;
3469
+ }
3470
+ });
3471
+ }
3443
3472
  const ninetailedPlugin = ({
3444
3473
  clientId,
3445
3474
  environment,
@@ -3448,7 +3477,6 @@ const ninetailedPlugin = ({
3448
3477
  profile,
3449
3478
  locale
3450
3479
  }) => {
3451
- let isFirstEvent = true;
3452
3480
  let isInitialized = false;
3453
3481
  const apiClient = new NinetailedApiClient({
3454
3482
  clientId,
@@ -3523,23 +3551,21 @@ const ninetailedPlugin = ({
3523
3551
  },
3524
3552
  cache
3525
3553
  });
3526
- }
3527
- });
3554
+ } // This is necessary to make sure that the cache is updated before the next flush is performed
3555
+
3556
+
3557
+ yield delay(20);
3558
+ }); // const throttledFlush = throttle(flush, 200, {
3559
+ // leading: false,
3560
+ // trailing: true,
3561
+ // });
3528
3562
 
3529
- const throttledFlush = throttle(flush, 200, {
3530
- leading: false,
3531
- trailing: true
3532
- });
3563
+
3564
+ const throttledFlush = asyncThrottle(flush);
3533
3565
 
3534
3566
  const createEvent = (event, instance) => __awaiter(void 0, void 0, void 0, function* () {
3535
3567
  queue = unionBy([event], queue, 'messageId');
3536
-
3537
- if (isFirstEvent) {
3538
- flush(instance);
3539
- isFirstEvent = false;
3540
- } else {
3541
- throttledFlush(instance);
3542
- }
3568
+ throttledFlush(instance);
3543
3569
  });
3544
3570
 
3545
3571
  return {
package/index.umd.js CHANGED
@@ -1,13 +1,12 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash/get'), require('lodash/throttle'), require('lodash/unionBy'), require('@ninetailed/experience.js-shared'), require('tty'), require('util'), require('os'), require('analytics'), require('lodash/flatten'), require('lodash/find'), require('lodash/includes')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'lodash/get', 'lodash/throttle', 'lodash/unionBy', '@ninetailed/experience.js-shared', 'tty', 'util', 'os', 'analytics', 'lodash/flatten', 'lodash/find', 'lodash/includes'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Javascript = {}, global.get$2, global.throttle, global.unionBy, global.experience_jsShared, global.require$$1, global.require$$1$1, global.require$$0, global.Analytics, global.flatten, global.find, global.includes));
5
- })(this, (function (exports, get$2, throttle, unionBy, experience_jsShared, require$$1, require$$1$1, require$$0, Analytics, flatten, find, includes) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash/get'), require('lodash/unionBy'), require('@ninetailed/experience.js-shared'), require('tty'), require('util'), require('os'), require('analytics'), require('lodash/flatten'), require('lodash/find'), require('lodash/includes')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'lodash/get', 'lodash/unionBy', '@ninetailed/experience.js-shared', 'tty', 'util', 'os', 'analytics', 'lodash/flatten', 'lodash/find', 'lodash/includes'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Javascript = {}, global.get$2, global.unionBy, global.experience_jsShared, global.require$$1, global.require$$1$1, global.require$$0, global.Analytics, global.flatten, global.find, global.includes));
5
+ })(this, (function (exports, get$2, unionBy, experience_jsShared, require$$1, require$$1$1, require$$0, Analytics, flatten, find, includes) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
9
9
  var get__default = /*#__PURE__*/_interopDefaultLegacy(get$2);
10
- var throttle__default = /*#__PURE__*/_interopDefaultLegacy(throttle);
11
10
  var unionBy__default = /*#__PURE__*/_interopDefaultLegacy(unionBy);
12
11
  var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1);
13
12
  var require$$1__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
@@ -3545,7 +3544,49 @@
3545
3544
  return setTimeout(resolve, ms);
3546
3545
  });
3547
3546
  };
3547
+ /**
3548
+ * Similar to _.throttle but waits for the promise to resolve. There is no
3549
+ * wait time because you can simply await `Promise.timeout` inside `fn`
3550
+ * to wait some time before the next call.
3551
+ */
3552
+
3553
+
3554
+ function asyncThrottle(fn) {
3555
+ var _this = this;
3556
+
3557
+ var runningPromise;
3558
+ var queuedPromise;
3559
+ var nextArgs;
3560
+ return function (args) {
3561
+ return __awaiter(_this, void 0, void 0, function () {
3562
+ return __generator(this, function (_a) {
3563
+ if (runningPromise) {
3564
+ nextArgs = args;
3548
3565
 
3566
+ if (queuedPromise) {
3567
+ return [2
3568
+ /*return*/
3569
+ , queuedPromise];
3570
+ } else {
3571
+ queuedPromise = runningPromise.then(function () {
3572
+ queuedPromise = undefined;
3573
+ runningPromise = fn(nextArgs);
3574
+ return runningPromise;
3575
+ });
3576
+ return [2
3577
+ /*return*/
3578
+ , queuedPromise];
3579
+ }
3580
+ } else {
3581
+ runningPromise = fn(args);
3582
+ return [2
3583
+ /*return*/
3584
+ , runningPromise];
3585
+ }
3586
+ });
3587
+ });
3588
+ };
3589
+ }
3549
3590
  var ninetailedPlugin = function ninetailedPlugin(_a) {
3550
3591
  var clientId = _a.clientId,
3551
3592
  environment = _a.environment,
@@ -3554,7 +3595,6 @@
3554
3595
  url = _a.url,
3555
3596
  profile = _a.profile,
3556
3597
  locale = _a.locale;
3557
- var isFirstEvent = true;
3558
3598
  var isInitialized = false;
3559
3599
  var apiClient = new NinetailedApiClient({
3560
3600
  clientId: clientId,
@@ -3650,31 +3690,34 @@
3650
3690
  , 4];
3651
3691
 
3652
3692
  case 4:
3693
+ // This is necessary to make sure that the cache is updated before the next flush is performed
3694
+ return [4
3695
+ /*yield*/
3696
+ , delay(20)];
3697
+
3698
+ case 5:
3699
+ // This is necessary to make sure that the cache is updated before the next flush is performed
3700
+ _b.sent();
3701
+
3653
3702
  return [2
3654
3703
  /*return*/
3655
3704
  ];
3656
3705
  }
3657
3706
  });
3658
3707
  });
3659
- };
3708
+ }; // const throttledFlush = throttle(flush, 200, {
3709
+ // leading: false,
3710
+ // trailing: true,
3711
+ // });
3660
3712
 
3661
- var throttledFlush = throttle__default["default"](flush, 200, {
3662
- leading: false,
3663
- trailing: true
3664
- });
3713
+
3714
+ var throttledFlush = asyncThrottle(flush);
3665
3715
 
3666
3716
  var createEvent = function createEvent(event, instance) {
3667
3717
  return __awaiter(void 0, void 0, void 0, function () {
3668
3718
  return __generator(this, function (_a) {
3669
3719
  queue = unionBy__default["default"]([event], queue, 'messageId');
3670
-
3671
- if (isFirstEvent) {
3672
- flush(instance);
3673
- isFirstEvent = false;
3674
- } else {
3675
- throttledFlush(instance);
3676
- }
3677
-
3720
+ throttledFlush(instance);
3678
3721
  return [2
3679
3722
  /*return*/
3680
3723
  ];
@@ -21,5 +21,11 @@ export declare const NINETAILED_TRACKER_EVENTS: {
21
21
  profile: string;
22
22
  };
23
23
  export declare const PLUGIN_NAME = "ninetailed";
24
+ /**
25
+ * Similar to _.throttle but waits for the promise to resolve. There is no
26
+ * wait time because you can simply await `Promise.timeout` inside `fn`
27
+ * to wait some time before the next call.
28
+ */
29
+ export default function asyncThrottle<I, O = void>(fn: (args: I) => Promise<O>): (args: I) => Promise<O>;
24
30
  export declare const ninetailedPlugin: ({ clientId, environment, preview, url, profile, locale, }: AnalyticsPluginNinetailedConfig) => AnalyticsPlugin;
25
31
  export {};
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "main": "./index.umd.js",
5
5
  "module": "./index.esm.js",
6
6
  "typings": "./index.d.ts",
7
7
  "dependencies": {
8
- "@ninetailed/experience.js-shared": "1.5.0",
8
+ "analytics": "^0.8.0",
9
+ "@ninetailed/experience.js-shared": "1.5.1",
9
10
  "uuid": "^8.3.2",
10
11
  "ts-toolbelt": "^9.6.0",
11
12
  "locale-enum": "^1.1.1",
12
13
  "i18n-iso-countries": "^7.3.0",
13
- "analytics": "^0.8.0",
14
14
  "lodash": "^4.17.21"
15
15
  },
16
16
  "peerDependencies": {}