mixpanel-browser 2.60.0 → 2.61.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.
@@ -1,12 +1,8 @@
1
1
  import { Promise } from '../promise-polyfill';
2
- import { _ } from '../utils'; // eslint-disable-line camelcase
2
+ import { _, JSONParse, JSONStringify } from '../utils'; // eslint-disable-line camelcase
3
3
 
4
4
  /**
5
- * @typedef {import('./wrapper').StorageWrapper}
6
- */
7
-
8
- /**
9
- * @type {StorageWrapper}
5
+ * @type {import('./wrapper').StorageWrapper}
10
6
  */
11
7
  var LocalStorageWrapper = function (storageOverride) {
12
8
  this.storage = storageOverride || localStorage;
@@ -19,7 +15,7 @@ LocalStorageWrapper.prototype.init = function () {
19
15
  LocalStorageWrapper.prototype.setItem = function (key, value) {
20
16
  return new Promise(_.bind(function (resolve, reject) {
21
17
  try {
22
- this.storage.setItem(key, value);
18
+ this.storage.setItem(key, JSONStringify(value));
23
19
  } catch (e) {
24
20
  reject(e);
25
21
  }
@@ -31,7 +27,7 @@ LocalStorageWrapper.prototype.getItem = function (key) {
31
27
  return new Promise(_.bind(function (resolve, reject) {
32
28
  var item;
33
29
  try {
34
- item = this.storage.getItem(key);
30
+ item = JSONParse(this.storage.getItem(key));
35
31
  } catch (e) {
36
32
  reject(e);
37
33
  }
@@ -6,9 +6,9 @@
6
6
  /**
7
7
  * @typedef {Object} StorageWrapper
8
8
  * @property {function():Promise<void>} init - Initializes the wrapper, async storage like IDB needs to create a table and upgrade if needed.
9
- * @property {function(string, string):Promise<void>} setItem - Sets an item in storage.
10
- * @property {function(string):Promise<string>} getItem - Retrieves an item from storage.
11
- * @property {function(string, string):Promise<void>} removeItem - Removes an item from storage.
9
+ * @property {function(string, any):Promise<void>} setItem - Sets an item in storage.
10
+ * @property {function(string):Promise<any>} getItem - Retrieves an item from storage.
11
+ * @property {function(string):Promise<void>} removeItem - Removes an item from storage.
12
12
  */
13
13
 
14
14
  export { };
package/src/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /* eslint camelcase: "off", eqeqeq: "off" */
2
2
  import Config from './config';
3
- import { NpoPromise } from './promise-polyfill';
3
+ import { NpoPromise, Promise } from './promise-polyfill';
4
4
  import { window } from './window';
5
5
 
6
6
  // Maximum allowed session recording length
@@ -1080,15 +1080,9 @@ _.cookie = {
1080
1080
  }
1081
1081
  };
1082
1082
 
1083
- var _localStorageSupported = null;
1084
- var localStorageSupported = function(storage, forceCheck) {
1085
- if (_localStorageSupported !== null && !forceCheck) {
1086
- return _localStorageSupported;
1087
- }
1088
-
1083
+ var _testStorageSupported = function (storage) {
1089
1084
  var supported = true;
1090
1085
  try {
1091
- storage = storage || window.localStorage;
1092
1086
  var key = '__mplss_' + cheap_guid(8),
1093
1087
  val = 'xyz';
1094
1088
  storage.setItem(key, val);
@@ -1099,59 +1093,74 @@ var localStorageSupported = function(storage, forceCheck) {
1099
1093
  } catch (err) {
1100
1094
  supported = false;
1101
1095
  }
1102
-
1103
- _localStorageSupported = supported;
1104
1096
  return supported;
1105
1097
  };
1106
1098
 
1107
- // _.localStorage
1108
- _.localStorage = {
1109
- is_supported: function(force_check) {
1110
- var supported = localStorageSupported(null, force_check);
1111
- if (!supported) {
1112
- console.error('localStorage unsupported; falling back to cookie store');
1113
- }
1114
- return supported;
1115
- },
1116
-
1117
- error: function(msg) {
1118
- console.error('localStorage error: ' + msg);
1119
- },
1099
+ var _localStorageSupported = null;
1100
+ var localStorageSupported = function(storage, forceCheck) {
1101
+ if (_localStorageSupported !== null && !forceCheck) {
1102
+ return _localStorageSupported;
1103
+ }
1104
+ return _localStorageSupported = _testStorageSupported(storage || window.localStorage);
1105
+ };
1120
1106
 
1121
- get: function(name) {
1122
- try {
1123
- return window.localStorage.getItem(name);
1124
- } catch (err) {
1125
- _.localStorage.error(err);
1126
- }
1127
- return null;
1128
- },
1107
+ var _sessionStorageSupported = null;
1108
+ var sessionStorageSupported = function(storage, forceCheck) {
1109
+ if (_sessionStorageSupported !== null && !forceCheck) {
1110
+ return _sessionStorageSupported;
1111
+ }
1112
+ return _sessionStorageSupported = _testStorageSupported(storage || window.sessionStorage);
1113
+ };
1129
1114
 
1130
- parse: function(name) {
1131
- try {
1132
- return _.JSONDecode(_.localStorage.get(name)) || {};
1133
- } catch (err) {
1134
- // noop
1135
- }
1136
- return null;
1137
- },
1115
+ function _storageWrapper(storage, name, is_supported_fn) {
1116
+ var log_error = function(msg) {
1117
+ console.error(name + ' error: ' + msg);
1118
+ };
1138
1119
 
1139
- set: function(name, value) {
1140
- try {
1141
- window.localStorage.setItem(name, value);
1142
- } catch (err) {
1143
- _.localStorage.error(err);
1120
+ return {
1121
+ is_supported: function(forceCheck) {
1122
+ var supported = is_supported_fn(storage, forceCheck);
1123
+ if (!supported) {
1124
+ console.error(name + ' unsupported');
1125
+ }
1126
+ return supported;
1127
+ },
1128
+ error: log_error,
1129
+ get: function(key) {
1130
+ try {
1131
+ return storage.getItem(key);
1132
+ } catch (err) {
1133
+ log_error(err);
1134
+ }
1135
+ return null;
1136
+ },
1137
+ parse: function(key) {
1138
+ try {
1139
+ return _.JSONDecode(storage.getItem(key)) || {};
1140
+ } catch (err) {
1141
+ // noop
1142
+ }
1143
+ return null;
1144
+ },
1145
+ set: function(key, value) {
1146
+ try {
1147
+ storage.setItem(key, value);
1148
+ } catch (err) {
1149
+ log_error(err);
1150
+ }
1151
+ },
1152
+ remove: function(key) {
1153
+ try {
1154
+ storage.removeItem(key);
1155
+ } catch (err) {
1156
+ log_error(err);
1157
+ }
1144
1158
  }
1145
- },
1159
+ };
1160
+ }
1146
1161
 
1147
- remove: function(name) {
1148
- try {
1149
- window.localStorage.removeItem(name);
1150
- } catch (err) {
1151
- _.localStorage.error(err);
1152
- }
1153
- }
1154
- };
1162
+ _.localStorage = _storageWrapper(window.localStorage, 'localStorage', localStorageSupported);
1163
+ _.sessionStorage = _storageWrapper(window.sessionStorage, 'sessionStorage', sessionStorageSupported);
1155
1164
 
1156
1165
  _.register_event = (function() {
1157
1166
  // written by Dean Edwards, 2005
@@ -1678,6 +1687,31 @@ _.info = {
1678
1687
  }
1679
1688
  };
1680
1689
 
1690
+ /**
1691
+ * Returns a throttled function that will only run at most every `waitMs` and returns a promise that resolves with the next invocation.
1692
+ * Throttled calls will build up a batch of args and invoke the callback with all args since the last invocation.
1693
+ */
1694
+ var batchedThrottle = function (fn, waitMs) {
1695
+ var timeoutPromise = null;
1696
+ var throttledItems = [];
1697
+ return function (item) {
1698
+ var self = this;
1699
+ throttledItems.push(item);
1700
+
1701
+ if (!timeoutPromise) {
1702
+ timeoutPromise = new Promise(function (resolve) {
1703
+ setTimeout(function () {
1704
+ var returnValue = fn.apply(self, [throttledItems]);
1705
+ timeoutPromise = null;
1706
+ throttledItems = [];
1707
+ resolve(returnValue);
1708
+ }, waitMs);
1709
+ });
1710
+ }
1711
+ return timeoutPromise;
1712
+ };
1713
+ };
1714
+
1681
1715
  var cheap_guid = function(maxlen) {
1682
1716
  var guid = Math.random().toString(36).substring(2, 10) + Math.random().toString(36).substring(2, 10);
1683
1717
  return maxlen ? guid.substring(0, maxlen) : guid;
@@ -1720,6 +1754,8 @@ var isOnline = function() {
1720
1754
  return _.isUndefined(onLine) || onLine;
1721
1755
  };
1722
1756
 
1757
+ var NOOP_FUNC = function () {};
1758
+
1723
1759
  var JSONStringify = null, JSONParse = null;
1724
1760
  if (typeof JSON !== 'undefined') {
1725
1761
  JSONStringify = JSON.stringify;
@@ -1728,22 +1764,23 @@ if (typeof JSON !== 'undefined') {
1728
1764
  JSONStringify = JSONStringify || _.JSONEncode;
1729
1765
  JSONParse = JSONParse || _.JSONDecode;
1730
1766
 
1731
- // EXPORTS (for closure compiler)
1732
- _['toArray'] = _.toArray;
1733
- _['isObject'] = _.isObject;
1734
- _['JSONEncode'] = _.JSONEncode;
1735
- _['JSONDecode'] = _.JSONDecode;
1736
- _['isBlockedUA'] = _.isBlockedUA;
1737
- _['isEmptyObject'] = _.isEmptyObject;
1767
+ // UNMINIFIED EXPORTS (for closure compiler)
1738
1768
  _['info'] = _.info;
1739
- _['info']['device'] = _.info.device;
1740
1769
  _['info']['browser'] = _.info.browser;
1741
1770
  _['info']['browserVersion'] = _.info.browserVersion;
1771
+ _['info']['device'] = _.info.device;
1742
1772
  _['info']['properties'] = _.info.properties;
1773
+ _['isBlockedUA'] = _.isBlockedUA;
1774
+ _['isEmptyObject'] = _.isEmptyObject;
1775
+ _['isObject'] = _.isObject;
1776
+ _['JSONDecode'] = _.JSONDecode;
1777
+ _['JSONEncode'] = _.JSONEncode;
1778
+ _['toArray'] = _.toArray;
1743
1779
  _['NPO'] = NpoPromise;
1744
1780
 
1745
1781
  export {
1746
1782
  _,
1783
+ batchedThrottle,
1747
1784
  cheap_guid,
1748
1785
  console_with_prefix,
1749
1786
  console,
@@ -1756,6 +1793,7 @@ export {
1756
1793
  MAX_RECORDING_MS,
1757
1794
  MAX_VALUE_FOR_MIN_RECORDING_MS,
1758
1795
  navigator,
1796
+ NOOP_FUNC,
1759
1797
  safewrap,
1760
1798
  safewrapClass,
1761
1799
  slice,