@webkrafters/react-observable-context 4.0.0-alpha.1 → 4.0.0-alpha.3

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,21 +1,21 @@
1
1
  "use strict";
2
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
3
  Object.defineProperty(exports, "__esModule", {
3
4
  value: true
4
5
  });
5
6
  exports["default"] = void 0;
6
- var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
7
- var _lodash2 = _interopRequireDefault(require("lodash.isequal"));
8
- var _lodash3 = _interopRequireDefault(require("lodash.isplainobject"));
9
- var _lodash4 = _interopRequireDefault(require("lodash.set"));
10
- var _constants = require("../../constants");
11
- var _utils = require("../../utils");
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
- function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
7
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
8
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
9
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
14
10
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15
11
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
16
12
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
13
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
14
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
15
+ function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
17
16
  function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
18
17
  function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
18
+ function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
19
19
  function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
20
20
  function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { _classCheckPrivateStaticAccess(receiver, classConstructor); _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
21
21
  function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { _classCheckPrivateStaticAccess(receiver, classConstructor); _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
@@ -25,122 +25,25 @@ function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) {
25
25
  function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
26
26
  function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
27
27
  function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
28
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
29
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
30
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
31
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
32
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
33
- function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
34
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
35
- function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
36
- /**
37
28
  * Returns function that checks whether its argument is either the ancestorPath or its child/descendant
38
29
  *
39
30
  * @type {(ancestorPath?: string) => {contains: (path: string) => boolean}}
40
31
  */
41
- var getPathTesterIn = function () {
42
- var PATH_DELIMITERS = {
43
- '.': null,
44
- '[': null
45
- };
46
- return function () {
47
- var ancestorPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
48
- var ancestorPathLength = (ancestorPath === null || ancestorPath === void 0 ? void 0 : ancestorPath.length) || 0;
49
- return {
50
- contains: function contains(path) {
51
- return ancestorPathLength && path.startsWith(ancestorPath) && (ancestorPathLength === path.length || path[ancestorPathLength] in PATH_DELIMITERS);
52
- }
53
- };
54
- };
55
- }();
56
- /**
57
32
  * Merges `changes` into a `slice` of the state object.
58
33
  *
59
34
  * Advisory: Will undo the readonly xteristics of affected hierarchies within the `slice`.
60
35
  */
61
- var mergeChanges = function () {
62
- var matchCompositeType = function matchCompositeType(slice, changes) {
63
- if (Array.isArray(slice) && Array.isArray(changes)) {
64
- return Array;
65
- }
66
- if ((0, _lodash3["default"])(slice) && (0, _lodash3["default"])(changes)) {
67
- return Object;
68
- }
69
- };
70
- var doMergeAt = function doMergeAt(key, slice, changes) {
71
- if ((0, _lodash2["default"])(slice[key], changes[key])) {
72
- return;
73
- }
74
- if (Object.isFrozen(changes[key])) {
75
- slice[key] = changes[key];
76
- return;
77
- }
78
- if (Object.isFrozen(slice[key])) {
79
- slice[key] = _objectSpread({}, slice[key]);
80
- }
81
- switch (matchCompositeType(slice[key], changes[key])) {
82
- case Object:
83
- runMerger(slice[key], changes[key]);
84
- break;
85
- case Array:
86
- slice[key].length = changes[key].length;
87
- runMerger(slice[key], changes[key]);
88
- break;
89
- default:
90
- slice[key] = changes[key];
91
- }
92
- };
93
- var runMerger = function runMerger(slice, changes) {
94
- if ((0, _lodash3["default"])(changes)) {
95
- for (var k in changes) {
96
- doMergeAt(k, slice, changes);
97
- }
98
- } else if (Array.isArray(changes)) {
99
- changes.forEach(function (_, c) {
100
- return doMergeAt(c, slice, changes);
101
- });
102
- }
103
- };
104
- /**
105
36
  * @param {T} slice
106
37
  * @param {T} changes
107
38
  * @template {PartialState<State>} T
108
39
  */
109
- var mergeChanges = function mergeChanges(slice, changes) {
110
- !(0, _lodash2["default"])(slice, changes) && runMerger(slice, changes);
111
- };
112
- return mergeChanges;
113
- }();
114
- /** @type {(paths: Array<string>) => Array<string>} */
115
- var arrangePaths = function arrangePaths(paths) {
116
- if (paths.includes(_constants.DEFAULT_STATE_PATH)) {
117
- return [_constants.DEFAULT_STATE_PATH];
118
- }
119
- /** @type {Array<string>} */
120
- var arranged = [];
121
- var group = getPathTesterIn();
122
- var _iterator = _createForOfIteratorHelper((0, _lodash["default"])(paths).sort()),
123
- _step;
124
- try {
125
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
126
- var path = _step.value;
127
- if (group.contains(path)) {
128
- continue;
129
- }
130
- group = getPathTesterIn(path);
131
- arranged.push(path);
132
- }
133
- } catch (err) {
134
- _iterator.e(err);
135
- } finally {
136
- _iterator.f();
137
- }
138
- return arranged;
139
- };
40
+ var MODERATE_NUM_PATHS_THRESHOLD = 8;
140
41
  /** @template {State} T */
141
42
  var _clients = new WeakMap();
142
43
  var _id = new WeakMap();
143
44
  var _paths = new WeakMap();
144
45
  var _source = new WeakMap();
145
46
  var _value = new WeakMap();
47
+ var _setValueAt = new WeakSet();
146
48
  var Accessor = function () {
147
49
  /** @type {Set<string>} */
148
50
  /** @type {number} */
149
51
  /** @type {Array<string>} */
150
52
  /** @type {T} */
151
- /** @type {Readonly<PartialState<T>>} */
53
+ /** @type {{[propertyPath: string]: Readonly<*>}} */
152
54
  /**
153
55
  * @param {T} source State object reference from which the accessedPropertyPaths are to be selected.
154
56
  * @param {Array<string>} accessedPropertyPaths
155
57
  */
156
58
  function Accessor(source, accessedPropertyPaths) {
157
59
  var _Accessor$NUM_INSTANC, _Accessor$NUM_INSTANC2;
158
60
  _classCallCheck(this, Accessor);
61
+ _classPrivateMethodInitSpec(this, _setValueAt);
159
62
  _classPrivateFieldInitSpec(this, _clients, {
160
63
  writable: true,
161
64
  value: void 0
@@ -163,11 +66,11 @@ var Accessor = function () {
163
66
  });
164
67
  _classPrivateFieldSet(this, _clients, new Set());
165
68
  _classPrivateFieldSet(this, _id, _classStaticPrivateFieldSpecSet(Accessor, Accessor, _NUM_INSTANCES, (_Accessor$NUM_INSTANC2 = _classStaticPrivateFieldSpecGet(Accessor, Accessor, _NUM_INSTANCES), ++_Accessor$NUM_INSTANC2)));
166
- _classPrivateFieldSet(this, _paths, arrangePaths(accessedPropertyPaths));
167
- /** @type {boolean} */
168
- this.refreshDue = true;
69
+ _classPrivateFieldSet(this, _paths, Array.from(new Set(accessedPropertyPaths)));
70
+ /** @type {Array<string>} */
71
+ this.outdatedPaths = _classPrivateFieldGet(this, _paths).slice();
169
72
  _classPrivateFieldSet(this, _source, source);
170
- _classPrivateFieldSet(this, _value, (0, _utils.makeReadonly)({}));
73
+ _classPrivateFieldSet(this, _value, {});
171
74
  }
172
75
  _createClass(Accessor, [{
173
76
  key: "numClients",
@@ -189,10 +92,11 @@ var Accessor = function () {
189
92
  get: function get() {
190
93
  return _classPrivateFieldGet(this, _value);
191
94
  }
192
- /** @param {string} clientId */
95
+ /**
193
96
  * @param {string} propertyPath
194
97
  * @param {Atom<V>} atom
195
98
  * @template V
196
99
  */
197
100
  }, {
198
101
  key: "addClient",
199
- value: function addClient(clientId) {
102
+ value: /** @param {string} clientId */
103
+ function addClient(clientId) {
200
104
  _classPrivateFieldGet(this, _clients).add(clientId);
201
105
  }
202
106
  /** @type {(clientId: string) => boolean} */
@@ -207,49 +111,76 @@ var Accessor = function () {
207
111
  value: function removeClient(clientId) {
208
112
  return _classPrivateFieldGet(this, _clients)["delete"](clientId);
209
113
  }
210
- /**
211
114
  * @param {{[propertyPath: string]: Atom}} atoms Curated slices of state currently requested
212
115
  * @returns {Readonly<PartialState<State>>}
213
116
  */
117
+ /**
214
118
  * @param {{[propertyPath: string]: Atom<*>}} atoms Curated slices of state currently requested
215
119
  * @returns {{[propertyPath: string]: Readonly<*>}}
216
120
  */
217
121
  }, {
218
122
  key: "refreshValue",
219
123
  value: function refreshValue(atoms) {
220
- if (!this.refreshDue) {
124
+ if (!this.outdatedPaths.length) {
221
125
  return _classPrivateFieldGet(this, _value);
222
126
  }
223
- this.refreshDue = false;
224
- var value = _objectSpread({}, _classPrivateFieldGet(this, _value));
225
- if (_classPrivateFieldGet(this, _paths)[0] === _constants.DEFAULT_STATE_PATH) {
226
- for (var k in _classPrivateFieldGet(this, _source)) {
227
- if (!(0, _lodash2["default"])(value[k], _classPrivateFieldGet(this, _source)[k])) {
228
- value[k] = (0, _lodash["default"])(_classPrivateFieldGet(this, _source)[k]);
127
+ var refreshLen;
128
+ var refreshPaths = {};
129
+ buildRefreshObj: {
130
+ var pathSet = new Set(this.outdatedPaths);
131
+ this.outdatedPaths.length = 0;
132
+ refreshLen = pathSet.size;
133
+ var _iterator = _createForOfIteratorHelper(pathSet),
134
+ _step;
135
+ try {
136
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
137
+ var p = _step.value;
138
+ refreshPaths[p] = true;
229
139
  }
140
+ } catch (err) {
141
+ _iterator.e(err);
142
+ } finally {
143
+ _iterator.f();
230
144
  }
231
- } else {
232
- /** @type {PartialState<State>} */
233
- var update = {};
145
+ }
146
+ if (refreshLen >= _classPrivateFieldGet(this, _paths).length) {
234
147
  var _iterator2 = _createForOfIteratorHelper(_classPrivateFieldGet(this, _paths)),
235
148
  _step2;
236
149
  try {
237
150
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
238
- var p = _step2.value;
239
- if (!(p in atoms)) {
240
- continue;
241
- }
242
- var atom = atoms[p];
243
- !atom.isConnected(_classPrivateFieldGet(this, _id)) && atom.connect(_classPrivateFieldGet(this, _id));
244
- (0, _lodash4["default"])(update, p, atom.value);
151
+ var _p = _step2.value;
152
+ _p in refreshPaths && _classPrivateMethodGet(this, _setValueAt, _setValueAt2).call(this, _p, atoms[_p]);
245
153
  }
246
154
  } catch (err) {
247
155
  _iterator2.e(err);
248
156
  } finally {
249
157
  _iterator2.f();
250
158
  }
251
- mergeChanges(value, update);
159
+ } else if (_classPrivateFieldGet(this, _paths).length > MODERATE_NUM_PATHS_THRESHOLD) {
160
+ var pathsObj = {};
161
+ var _iterator3 = _createForOfIteratorHelper(_classPrivateFieldGet(this, _paths).length),
162
+ _step3;
163
+ try {
164
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
165
+ var _p3 = _step3.value;
166
+ pathsObj[_p3] = true;
167
+ }
168
+ } catch (err) {
169
+ _iterator3.e(err);
170
+ } finally {
171
+ _iterator3.f();
172
+ }
173
+ for (var _p2 in refreshPaths) {
174
+ _p2 in pathsObj && _classPrivateMethodGet(this, _setValueAt, _setValueAt2).call(this, _p2, atoms[_p2]);
175
+ }
176
+ } else {
177
+ for (var _p4 in refreshPaths) {
178
+ _classPrivateFieldGet(this, _paths).includes(_p4) && _classPrivateMethodGet(this, _setValueAt, _setValueAt2).call(this, _p4, atoms[_p4]);
179
+ }
252
180
  }
253
- _classPrivateFieldSet(this, _value, (0, _utils.makeReadonly)(value));
254
181
  return _classPrivateFieldGet(this, _value);
255
182
  }
256
183
  }]);
257
184
  return Accessor;
258
185
  }();
186
+ function _setValueAt2(propertyPath, atom) {
187
+ !atom.isConnected(_classPrivateFieldGet(this, _id)) && atom.connect(_classPrivateFieldGet(this, _id));
188
+ _classPrivateFieldGet(this, _value)[propertyPath] = atom.value;
189
+ }
259
190
  var _NUM_INSTANCES = {
260
191
  writable: true,
261
192
  value: 0
@@ -1 +1 @@
1
- export default AccessorCache;
2
1
  /** @param {T} origin State object reference from which slices stored in this cache are to be curated */
3
2
  constructor(origin: T);
4
3
  /**
5
4
  * Gets state slice from the cache matching the `propertyPaths`.\
6
5
  * If not found, creates a new entry for the client from source, and returns it.
7
6
  *
8
7
  * @param {string} clientId
9
8
  * @param {...string} propertyPaths
10
9
  * @return {Readonly<PartialState<T>>}
11
10
  */
12
11
  get(clientId: string, ...propertyPaths: string[]): Readonly<PartialState<T>>;
13
12
  /**
14
13
  * Unlinks a consumer from the cache: performing synchronized state cleanup
15
14
  *
16
15
  * @param {string} clientId
17
16
  */
18
17
  unlinkClient(clientId: string): void;
19
18
  /** Observes the origin state bearing ObservableContext store for state changes to update accessors. */
20
19
  watchSource(): void;
21
20
  #private;
21
+ export default AccessorCache;
22
22
  /** @param {T} origin State object reference from which slices stored in this cache are to be curated */
23
23
  constructor(origin: T);
24
24
  /**
25
25
  * Gets state slice from the cache matching the `propertyPaths`.\
26
26
  * If not found, creates a new entry for the client from source, and returns it.
27
27
  *
28
28
  * @param {string} clientId
29
29
  * @param {...string} propertyPaths
30
30
  * @return {{[propertyPaths: string]: Readonly<*>}}
31
31
  */
32
32
  get(clientId: string, ...propertyPaths: string[]): {
33
33
  [propertyPaths: string]: Readonly<any>;
34
34
  };
35
35
  /**
36
36
  * Unlinks a consumer from the cache: performing synchronized state cleanup
37
37
  *
38
38
  * @param {string} clientId
39
39
  */
40
40
  unlinkClient(clientId: string): void;
41
41
  watchSource(changes: import("../../types").PartialState<T>): void;
42
42
  #private;
@@ -5,8 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports["default"] = void 0;
7
7
  var _lodash = _interopRequireDefault(require("lodash.get"));
8
- var _lodash2 = _interopRequireDefault(require("lodash.isempty"));
9
- var _lodash3 = _interopRequireDefault(require("lodash.isequal"));
8
+ var _lodash2 = _interopRequireDefault(require("lodash.has"));
9
+ var _lodash3 = _interopRequireDefault(require("lodash.isempty"));
10
+ var _lodash4 = _interopRequireDefault(require("lodash.isequal"));
10
11
  var _constants = require("../../constants");
11
12
  var _atom = _interopRequireDefault(require("../atom"));
12
13
  var _accessor = _interopRequireDefault(require("../accessor"));
@@ -32,14 +33,16 @@ var _accessors = new WeakMap();
32
33
  var _atoms = new WeakMap();
33
34
  var _origin = new WeakMap();
34
35
  var _createAccessor = new WeakSet();
36
+ var _getOriginAt = new WeakSet();
35
37
  /** @template {State} T */
36
38
  var AccessorCache = function () {
37
39
  /** @type {{[propertyPaths: string]: Accessor<T>}} */
38
- /** @type {{[propertyPath: string]: Atom}} */
40
+ /** @type {{[propertyPath: string]: Atom<*>}} */
39
41
  /** @type {T} */
40
42
  /** @param {T} origin State object reference from which slices stored in this cache are to be curated */
41
43
  function AccessorCache(origin) {
42
44
  _classCallCheck(this, AccessorCache);
45
+ _classPrivateMethodInitSpec(this, _getOriginAt);
43
46
  _classPrivateMethodInitSpec(this, _createAccessor);
44
47
  _classPrivateFieldInitSpec(this, _accessors, {
45
48
  writable: true,
@@ -61,13 +64,13 @@ var AccessorCache = function () {
61
64
  _createClass(AccessorCache, [{
62
65
  key: "get",
63
66
  value:
64
- /**
65
67
  * Gets state slice from the cache matching the `propertyPaths`.\
66
68
  * If not found, creates a new entry for the client from source, and returns it.
67
69
  *
68
70
  * @param {string} clientId
69
71
  * @param {...string} propertyPaths
70
72
  * @return {Readonly<PartialState<T>>}
71
73
  */
74
+ /**
72
75
  * Gets state slice from the cache matching the `propertyPaths`.\
73
76
  * If not found, creates a new entry for the client from source, and returns it.
74
77
  *
75
78
  * @param {string} clientId
76
79
  * @param {...string} propertyPaths
77
80
  * @return {{[propertyPaths: string]: Readonly<*>}}
78
81
  */
79
82
  function get(clientId) {
80
83
  for (var _len = arguments.length, propertyPaths = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
81
84
  propertyPaths[_key - 1] = arguments[_key];
82
85
  }
83
- if ((0, _lodash2["default"])(propertyPaths)) {
84
- propertyPaths = [_constants.DEFAULT_STATE_PATH];
86
+ if ((0, _lodash3["default"])(propertyPaths)) {
87
+ propertyPaths = [_constants.FULL_STATE_SELECTOR];
85
88
  }
86
89
  var cacheKey = JSON.stringify(propertyPaths);
87
90
  var accessor = cacheKey in _classPrivateFieldGet(this, _accessors) ? _classPrivateFieldGet(this, _accessors)[cacheKey] : _classPrivateMethodGet(this, _createAccessor, _createAccessor2).call(this, cacheKey, propertyPaths);
@@ -102,38 +105,30 @@ var AccessorCache = function () {
102
105
  delete accessors[k];
103
106
  }
104
107
  }
105
- /** Observes the origin state bearing ObservableContext store for state changes to update accessors. */
108
+ /**
106
109
  * Observes the origin state bearing ObservableContext store for state changes to update accessors.
107
110
  *
108
111
  * @type {Listener<T>}
109
112
  */
110
113
  }, {
111
114
  key: "watchSource",
112
- value: function watchSource() {
115
+ value: function watchSource(originChanges) {
113
116
  var accessors = _classPrivateFieldGet(this, _accessors);
114
117
  var atoms = _classPrivateFieldGet(this, _atoms);
115
- var state = _classPrivateFieldGet(this, _origin);
116
- var updatedPaths = {};
118
+ var updatedPaths = [];
117
119
  for (var path in atoms) {
118
- var newAtomVal = (0, _lodash["default"])(state, path);
119
- if ((0, _lodash3["default"])(newAtomVal, atoms[path].value)) {
120
+ if (path !== _constants.FULL_STATE_SELECTOR && !(0, _lodash2["default"])(originChanges, path)) {
121
+ continue;
122
+ }
123
+ var newAtomVal = _classPrivateMethodGet(this, _getOriginAt, _getOriginAt2).call(this, path);
124
+ if ((0, _lodash4["default"])(newAtomVal, atoms[path].value)) {
120
125
  continue;
121
126
  }
122
127
  atoms[path].setValue(newAtomVal);
123
- updatedPaths[path] = true;
128
+ updatedPaths.push(path);
124
129
  }
125
- if ((0, _lodash2["default"])(updatedPaths)) {
126
- for (var k in accessors) {
127
- if (accessors[k].paths[0] === _constants.DEFAULT_STATE_PATH) {
128
- accessors[k].refreshDue = true;
129
- }
130
- }
130
+ if (!updatedPaths.length) {
131
131
  return;
132
132
  }
133
- for (var _k in accessors) {
134
- if (accessors[_k].refreshDue) {
135
- continue;
136
- }
137
- var accessorPaths = accessors[_k].paths;
138
- accessors[_k].refreshDue = accessorPaths[0] === _constants.DEFAULT_STATE_PATH || accessorPaths.some(function (p) {
139
- return p in updatedPaths;
140
- });
133
+ for (var k in accessors) {
134
+ var _accessors$k$outdated;
135
+ (_accessors$k$outdated = accessors[k].outdatedPaths).push.apply(_accessors$k$outdated, updatedPaths);
141
136
  }
142
137
  }
143
138
  }]);
@@ -148,11 +143,9 @@ function _createAccessor2(cacheKey, propertyPaths) {
148
143
  try {
149
144
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
150
145
  var path = _step2.value;
151
- if (path in atoms) {
152
- continue;
146
+ if (!(path in atoms)) {
147
+ atoms[path] = new _atom["default"](_classPrivateMethodGet(this, _getOriginAt, _getOriginAt2).call(this, path));
153
148
  }
154
- atoms[path] = new _atom["default"]();
155
- atoms[path].setValue((0, _lodash["default"])(_classPrivateFieldGet(this, _origin), path));
156
149
  }
157
150
  } catch (err) {
158
151
  _iterator2.e(err);
@@ -161,5 +154,8 @@ function _createAccessor2(cacheKey, propertyPaths) {
161
154
  }
162
155
  return _classPrivateFieldGet(this, _accessors)[cacheKey];
163
156
  }
157
+ function _getOriginAt2(propertyPath) {
158
+ return propertyPath !== _constants.FULL_STATE_SELECTOR ? (0, _lodash["default"])(_classPrivateFieldGet(this, _origin), propertyPath) : _classPrivateFieldGet(this, _origin);
159
+ }
164
160
  var _default = AccessorCache;
165
161
  exports["default"] = _default;
@@ -1 +1 @@
1
- export default Atom;
2
1
  [x: string]: any;
3
2
  get value(): Readonly<Value>;
4
3
  /**
5
4
  * @param {number} accessorId
6
5
  * @returns {number} Number of connections remaining
7
6
  */
8
7
  connect(accessorId: number): number;
9
8
  /**
10
9
  * @param {number} accessorId
11
10
  * @returns {number} Number of connections remaining
12
11
  */
13
12
  disconnect(accessorId: number): number;
14
13
  /** @param {number} accessorId */
15
14
  isConnected(accessorId: number): boolean;
16
15
  /** @param {Value|Readonly<Value>} newValue */
17
16
  setValue(newValue: Value | Readonly<Value>): void;
18
17
  #private;
18
+ export default Atom;
19
19
  constructor(value?: any);
20
20
  get value(): Readonly<T>;
21
21
  /**
22
22
  * @param {number} accessorId
23
23
  * @returns {number} Number of connections remaining
24
24
  */
25
25
  connect(accessorId: number): number;
26
26
  /**
27
27
  * @param {number} accessorId
28
28
  * @returns {number} Number of connections remaining
29
29
  */
30
30
  disconnect(accessorId: number): number;
31
31
  /** @param {number} accessorId */
32
32
  isConnected(accessorId: number): boolean;
33
33
  /** @param {T|Readonly<T>} newValue */
34
34
  setValue(newValue: T | Readonly<T>): void;
35
35
  #private;
@@ -22,10 +22,12 @@ function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.
22
22
  var _connections = new WeakMap();
23
23
  var _value = new WeakMap();
24
24
  /** An atom represents an entry for each individual property path of the state still in use by client components */
25
+ /** @template T */
25
26
  var Atom = function () {
26
27
  /** @type {Set<number>} */
27
- /** @type {Readonly<Value>} */
28
+ /** @type {Readonly<T>} */
28
29
  function Atom() {
30
+ var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
29
31
  _classCallCheck(this, Atom);
30
32
  _classPrivateFieldInitSpec(this, _connections, {
31
33
  writable: true,
@@ -36,7 +38,7 @@ var Atom = function () {
36
38
  value: void 0
37
39
  });
38
40
  _classPrivateFieldSet(this, _connections, new Set());
39
- _classPrivateFieldSet(this, _value, (0, _utils.makeReadonly)({}));
41
+ this.setValue(value);
40
42
  }
41
43
  _createClass(Atom, [{
42
44
  key: "value",
@@ -63,7 +65,7 @@ var Atom = function () {
63
65
  value: function isConnected(accessorId) {
64
66
  return _classPrivateFieldGet(this, _connections).has(accessorId);
65
67
  }
66
- /** @param {Value|Readonly<Value>} newValue */
68
+ /** @param {T|Readonly<T>} newValue */
67
69
  }, {
68
70
  key: "setValue",
69
71
  value: function setValue(newValue) {
package/dist/types.d.ts CHANGED
@@ -1 +1 @@
1
- declare const _default: any;
2
1
  getItem: (key: string) => T;
3
2
  removeItem: (key: string) => void;
4
3
  setItem: (key: string, data: T) => void;
5
4
  getState: NonReactUsageReport;
6
5
  resetState: NonReactUsageReport;
7
6
  setState: NonReactUsageReport;
8
7
  subscribe: NonReactUsageReport;
9
8
  resetState?: (state: {
10
9
  current: T;
11
10
  original: T;
12
11
  }) => boolean;
13
12
  setState?: (newChanges: PartialState<T>) => boolean;
14
13
  [x: string]: any;
15
14
  resetState: VoidFunction;
16
15
  setState: (changes: PartialState<T>) => void;
17
16
  getState: (clientId: string, ...propertyPaths?: string[]) => Readonly<PartialState<T>>;
18
17
  subscribe: (listener: Listener<T>) => Unsubscribe;
19
18
  unlinkCache: (clientId: string) => void;
20
19
  data: PartialState<T>;
21
20
  resetState: VoidFunction;
22
21
  setState: (changes: PartialState<T>) => void;
22
+ declare const _default: any;
23
23
  [selectorKey: string]: Readonly<any>;
24
24
  getItem: (key: string) => T;
25
25
  removeItem: (key: string) => void;
26
26
  setItem: (key: string, data: T) => void;
27
27
  getState: NonReactUsageReport;
28
28
  resetState: NonReactUsageReport;
29
29
  setState: NonReactUsageReport;
30
30
  subscribe: NonReactUsageReport;
31
31
  resetState?: (state: {
32
32
  current: T;
33
33
  original: T;
34
34
  }) => boolean;
35
35
  setState?: (newChanges: PartialState<T>) => boolean;
36
36
  [x: string]: any;
37
37
  resetState: VoidFunction;
38
38
  setState: (changes: PartialState<T>) => void;
39
39
  getState: (clientId: string, ...propertyPaths?: string[]) => {
40
40
  [propertyPaths: string]: Readonly<any>;
41
41
  };
42
42
  subscribe: (listener: Listener<T>) => Unsubscribe;
43
43
  unlinkCache: (clientId: string) => void;
44
44
  data: Data;
45
45
  resetState: VoidFunction;
46
46
  setState: (changes: PartialState<T>) => void;