dataflux 1.7.4 → 1.7.5

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/README.md CHANGED
@@ -234,6 +234,25 @@ The method `findAll` returns always an array. The method `findOne` returns a sin
234
234
 
235
235
  When the component will unmount, the `findAll` subscription will be automatically terminated without the need to unsubscribe. Be aware, `store.findAll()` injects the unsubscribe call inside `componentWillUnmount()`. If your component already implements `componentWillUnmount()`, then you will have to use `store.subscribe()` and `store.unsubscribe()` instead of `store.findAll()`, to avoid side effects when the component is unmounted.
236
236
 
237
+ In case you prefer React hooks:
238
+
239
+ ```js
240
+ function MyComponent() {
241
+ const [books, setBooks] = useState([]);
242
+
243
+ useEffect(() => {
244
+ const subKey = store.subscribe("books", setBooks, ({price}) => price < 20);
245
+
246
+ return () => {
247
+ store.unsubscribe(subKey); // Remember to unsubscribe
248
+ };
249
+ }, []);
250
+
251
+ return books.map(book => <Book onTitleChange={(title) => book.set("title", title)}/>);
252
+ }
253
+ ```
254
+
255
+
237
256
  ## Configuration
238
257
 
239
258
  The store can be configured with the following options:
@@ -55,20 +55,28 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
55
55
 
56
56
  function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
57
57
 
58
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
59
+
58
60
  function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
59
61
 
60
62
  function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
61
63
 
62
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
63
-
64
64
  function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
65
65
 
66
+ function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
67
+
68
+ 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; } }
69
+
66
70
  function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
67
71
 
68
72
  function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
69
73
 
70
74
  function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
71
75
 
76
+ var _queryPromises = /*#__PURE__*/new WeakMap();
77
+
78
+ var _unsubPromises = /*#__PURE__*/new WeakMap();
79
+
72
80
  var _getUniqueSubs = /*#__PURE__*/new WeakMap();
73
81
 
74
82
  var _propagateChange = /*#__PURE__*/new WeakMap();
@@ -91,6 +99,16 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
91
99
 
92
100
  _classPrivateMethodInitSpec(_assertThisInitialized(_this), _propagateInsertChange);
93
101
 
102
+ _classPrivateFieldInitSpec(_assertThisInitialized(_this), _queryPromises, {
103
+ writable: true,
104
+ value: []
105
+ });
106
+
107
+ _classPrivateFieldInitSpec(_assertThisInitialized(_this), _unsubPromises, {
108
+ writable: true,
109
+ value: []
110
+ });
111
+
94
112
  _defineProperty(_assertThisInitialized(_this), "multipleSubscribe", function (subscriptions, callback) {
95
113
  var dataPayload = {};
96
114
 
@@ -125,55 +143,60 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
125
143
  });
126
144
 
127
145
  _defineProperty(_assertThisInitialized(_this), "subscribe", function (type, callback, filterFunction) {
128
- var subKey = (0, _uuid.v4)();
146
+ var _this$_subscribed, _this$_subscribed$typ;
129
147
 
130
- if (!_this._subscribed[type]) {
131
- _this._subscribed[type] = {};
132
- }
148
+ var subKey = (0, _uuid.v4)();
149
+ (_this$_subscribed$typ = (_this$_subscribed = _this._subscribed)[type]) !== null && _this$_subscribed$typ !== void 0 ? _this$_subscribed$typ : _this$_subscribed[type] = {};
133
150
 
134
- _this.find(type, filterFunction).then(function (data) {
151
+ var prom = _this.find(type, filterFunction).then(function (data) {
135
152
  _classPrivateFieldGet(_assertThisInitialized(_this), _subscribeToObjects).call(_assertThisInitialized(_this), type, data, {
136
153
  callback: callback,
137
154
  filterFunction: filterFunction,
138
155
  subKey: subKey
139
156
  });
140
157
 
141
- return callback(data);
158
+ callback(data);
142
159
  });
143
160
 
161
+ _classPrivateFieldGet(_assertThisInitialized(_this), _queryPromises).push(prom);
162
+
144
163
  return subKey;
145
164
  });
146
165
 
147
166
  _defineProperty(_assertThisInitialized(_this), "unsubscribe", function (key) {
148
- if (_this._multipleSubscribed[key]) {
149
- var _iterator = _createForOfIteratorHelper(_this._multipleSubscribed[key]),
150
- _step;
151
-
152
- try {
153
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
154
- var sub = _step.value;
167
+ if (_classPrivateFieldGet(_assertThisInitialized(_this), _queryPromises).length) {
168
+ _classPrivateFieldSet(_assertThisInitialized(_this), _unsubPromises, Promise.all(_classPrivateFieldGet(_assertThisInitialized(_this), _queryPromises)).then(function () {
169
+ if (_this._multipleSubscribed[key]) {
170
+ var _iterator = _createForOfIteratorHelper(_this._multipleSubscribed[key]),
171
+ _step;
155
172
 
156
- _this.unsubscribe(sub);
157
- }
158
- } catch (err) {
159
- _iterator.e(err);
160
- } finally {
161
- _iterator.f();
162
- }
173
+ try {
174
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
175
+ var sub = _step.value;
163
176
 
164
- delete _this._multipleSubscribed[key];
165
- } else {
166
- for (var type in _this._subscribed) {
167
- for (var id in _this._subscribed[type]) {
168
- _this._subscribed[type][id] = _this._subscribed[type][id].filter(function (i) {
169
- return i.subKey !== key;
170
- });
177
+ _this.unsubscribe(sub);
178
+ }
179
+ } catch (err) {
180
+ _iterator.e(err);
181
+ } finally {
182
+ _iterator.f();
183
+ }
171
184
 
172
- if (_this._subscribed[type][id].length === 0) {
173
- delete _this._subscribed[type][id];
185
+ delete _this._multipleSubscribed[key];
186
+ } else {
187
+ for (var type in _this._subscribed) {
188
+ for (var id in _this._subscribed[type]) {
189
+ _this._subscribed[type][id] = _this._subscribed[type][id].filter(function (i) {
190
+ return i.subKey !== key;
191
+ });
192
+
193
+ if (_this._subscribed[type][id].length === 0) {
194
+ delete _this._subscribed[type][id];
195
+ }
196
+ }
174
197
  }
175
198
  }
176
- }
199
+ }));
177
200
  }
178
201
  });
179
202
 
@@ -197,8 +220,10 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
197
220
 
198
221
  try {
199
222
  for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
223
+ var _sub$subKey, _out$_sub$subKey;
224
+
200
225
  var sub = _step3.value;
201
- out[sub.subKey] = out[sub.subKey] || sub;
226
+ (_out$_sub$subKey = out[_sub$subKey = sub.subKey]) !== null && _out$_sub$subKey !== void 0 ? _out$_sub$subKey : out[_sub$subKey] = sub;
202
227
  }
203
228
  } catch (err) {
204
229
  _iterator3.e(err);
@@ -220,20 +245,21 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
220
245
  writable: true,
221
246
  value: function value() {
222
247
  var objects = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
248
+ return (_classPrivateFieldGet(_assertThisInitialized(_this), _unsubPromises).length ? Promise.all(_classPrivateFieldGet(_assertThisInitialized(_this), _unsubPromises)) : Promise.resolve()).then(function () {
249
+ if (objects.length) {
250
+ var type = objects[0].getModel().getType();
223
251
 
224
- if (objects.length) {
225
- var type = objects[0].getModel().getType();
226
-
227
- var uniqueSubs = _classPrivateFieldGet(_assertThisInitialized(_this), _getUniqueSubs).call(_assertThisInitialized(_this), objects, type);
252
+ var uniqueSubs = _classPrivateFieldGet(_assertThisInitialized(_this), _getUniqueSubs).call(_assertThisInitialized(_this), objects, type);
228
253
 
229
- (0, _batchPromises["default"])(10, uniqueSubs, function (_ref3) {
230
- var callback = _ref3.callback,
231
- filterFunction = _ref3.filterFunction;
232
- return _this.find(type, filterFunction).then(callback);
233
- });
234
- }
254
+ (0, _batchPromises["default"])(10, uniqueSubs, function (_ref3) {
255
+ var callback = _ref3.callback,
256
+ filterFunction = _ref3.filterFunction;
257
+ return _this.find(type, filterFunction).then(callback);
258
+ });
259
+ }
235
260
 
236
- return objects;
261
+ return objects;
262
+ });
237
263
  }
238
264
  });
239
265
 
@@ -245,12 +271,11 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
245
271
 
246
272
  try {
247
273
  for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
274
+ var _this$_subscribed$typ2, _this$_subscribed$typ3;
275
+
248
276
  var object = _step4.value;
249
277
  var id = object.getId();
250
-
251
- if (!_this._subscribed[type][id]) {
252
- _this._subscribed[type][id] = [];
253
- }
278
+ (_this$_subscribed$typ3 = (_this$_subscribed$typ2 = _this._subscribed[type])[id]) !== null && _this$_subscribed$typ3 !== void 0 ? _this$_subscribed$typ3 : _this$_subscribed$typ2[id] = [];
254
279
 
255
280
  _this._subscribed[type][id].push(item);
256
281
  }
@@ -297,71 +322,73 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
297
322
  function _propagateInsertChange2(type, newObjects) {
298
323
  var _this3 = this;
299
324
 
300
- if (this._subscribed[type]) {
301
- var uniqueSubs = {};
302
- var objects = Object.values(this._subscribed[type]);
325
+ return (_classPrivateFieldGet(this, _unsubPromises).length ? Promise.all(_classPrivateFieldGet(this, _unsubPromises)) : Promise.resolve()).then(function () {
326
+ if (_this3._subscribed[type]) {
327
+ var uniqueSubs = {};
328
+ var objects = Object.values(_this3._subscribed[type]);
303
329
 
304
- for (var _i2 = 0, _objects = objects; _i2 < _objects.length; _i2++) {
305
- var object = _objects[_i2];
330
+ for (var _i2 = 0, _objects = objects; _i2 < _objects.length; _i2++) {
331
+ var object = _objects[_i2];
306
332
 
307
- var _iterator5 = _createForOfIteratorHelper(object),
308
- _step5;
333
+ var _iterator5 = _createForOfIteratorHelper(object),
334
+ _step5;
309
335
 
310
- try {
311
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
312
- var sub = _step5.value;
336
+ try {
337
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
338
+ var sub = _step5.value;
313
339
 
314
- if (!uniqueSubs[sub.subKey]) {
315
- uniqueSubs[sub.subKey] = sub;
340
+ if (!uniqueSubs[sub.subKey]) {
341
+ uniqueSubs[sub.subKey] = sub;
342
+ }
316
343
  }
344
+ } catch (err) {
345
+ _iterator5.e(err);
346
+ } finally {
347
+ _iterator5.f();
317
348
  }
318
- } catch (err) {
319
- _iterator5.e(err);
320
- } finally {
321
- _iterator5.f();
322
349
  }
323
- }
324
350
 
325
- var possibleSubs = Object.values(uniqueSubs);
326
- (0, _batchPromises["default"])(10, possibleSubs, function (_ref4) {
327
- var callback = _ref4.callback,
328
- filterFunction = _ref4.filterFunction;
329
- var objectsToSubscribe = filterFunction ? newObjects.filter(filterFunction) : newObjects;
351
+ var possibleSubs = Object.values(uniqueSubs);
352
+ (0, _batchPromises["default"])(10, possibleSubs, function (_ref4) {
353
+ var callback = _ref4.callback,
354
+ filterFunction = _ref4.filterFunction;
355
+ var objectsToSubscribe = filterFunction ? newObjects.filter(filterFunction) : newObjects;
330
356
 
331
- if (objectsToSubscribe.length) {
332
- // Check if the new objects matter
333
- return _this3.find(type, filterFunction).then(function (data) {
334
- var subKey;
357
+ if (objectsToSubscribe.length) {
358
+ // Check if the new objects matter
359
+ return _this3.find(type, filterFunction).then(function (data) {
360
+ var subKey;
335
361
 
336
- var _iterator6 = _createForOfIteratorHelper(data),
337
- _step6;
362
+ var _iterator6 = _createForOfIteratorHelper(data),
363
+ _step6;
338
364
 
339
- try {
340
- for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
341
- var d = _step6.value;
365
+ try {
366
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
367
+ var d = _step6.value;
342
368
 
343
- var item = _this3._subscribed[d.getModel().getType()][d.getId()];
369
+ var item = _this3._subscribed[d.getModel().getType()][d.getId()];
344
370
 
345
- subKey = item ? item.subKey : null;
346
- if (subKey) break;
371
+ subKey = item ? item.subKey : null;
372
+ if (subKey) break;
373
+ }
374
+ } catch (err) {
375
+ _iterator6.e(err);
376
+ } finally {
377
+ _iterator6.f();
347
378
  }
348
- } catch (err) {
349
- _iterator6.e(err);
350
- } finally {
351
- _iterator6.f();
352
- }
353
379
 
354
- _classPrivateFieldGet(_this3, _subscribeToObjects).call(_this3, type, objectsToSubscribe, {
355
- callback: callback,
356
- filterFunction: filterFunction,
357
- subKey: subKey
358
- });
380
+ _classPrivateFieldGet(_this3, _subscribeToObjects).call(_this3, type, objectsToSubscribe, {
381
+ callback: callback,
382
+ filterFunction: filterFunction,
383
+ subKey: subKey
384
+ });
359
385
 
360
- return data;
361
- }).then(callback);
362
- }
363
- });
364
- }
386
+ return data;
387
+ }).then(callback);
388
+ }
389
+ });
390
+ }
391
+ });
365
392
  }
366
393
 
367
394
  var _default = ObserverStore;