dataflux 1.4.0 → 1.4.4
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 +17 -13
- package/dist/Obj.js +10 -2
- package/dist/ObserverStore.js +38 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -160,6 +160,8 @@ If now a book is inserted/deleted/edited:
|
|
|
160
160
|
* if the book has `price < 20`, `drawBooksCallback` will be called again with the new dataset;
|
|
161
161
|
* if the book has `price > 20`, `drawBooksCallback` will NOT be called again (because the new book doesn't impact our selection).
|
|
162
162
|
|
|
163
|
+
> Warning: if you edit the objects inside your callback (e.g., you do `.set()`), you will trigger the subscription's callback again in an infinite loop! If you want to set an attribute of an object inside your callback, before drawing it, use `setConstant()`.
|
|
164
|
+
|
|
163
165
|
You can terminate the subscription with `store.unsubscribe()`:
|
|
164
166
|
|
|
165
167
|
```js
|
|
@@ -176,7 +178,7 @@ const subscriptions = [
|
|
|
176
178
|
["author"], // No filter function, all objects returned
|
|
177
179
|
];
|
|
178
180
|
|
|
179
|
-
const callback = (
|
|
181
|
+
const callback = ({book, author}) => {
|
|
180
182
|
// Objects are ready
|
|
181
183
|
};
|
|
182
184
|
|
|
@@ -481,18 +483,20 @@ The store emits the following events:
|
|
|
481
483
|
Each object created is enriched with the following methods.
|
|
482
484
|
|
|
483
485
|
|
|
484
|
-
| Method | Description
|
|
485
|
-
|
|
486
|
-
| getId() | It returns a unique ID used by the store to identify the object. The ID is unique inside a single model. Be aware, `object.id` and `objet.getId()` may return different values, since store's IDs can be different from the one of the REST API.
|
|
487
|
-
| set(attribute, value, hidden) | A method to set an attribute to the object. It provides some advantages compared to doing `object.attribute = value`, these are discussed in [below](#editing-objects).
|
|
488
|
-
|
|
|
489
|
-
|
|
|
490
|
-
|
|
|
491
|
-
|
|
|
492
|
-
|
|
|
493
|
-
|
|
494
|
-
|
|
|
495
|
-
|
|
|
486
|
+
| Method | Description |
|
|
487
|
+
|------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
488
|
+
| getId() | It returns a unique ID used by the store to identify the object. The ID is unique inside a single model. Be aware, `object.id` and `objet.getId()` may return different values, since store's IDs can be different from the one of the REST API. |
|
|
489
|
+
| set(attribute, value, hidden) | A method to set an attribute to the object. It provides some advantages compared to doing `object.attribute = value`, these are discussed in [below](#editing-objects). The third parameter is optional, and when set to true will set the attribute as hidden (see [hiddenFields](#models-creation)). |
|
|
490
|
+
| setConstant(attribute, value) | A method to set an unmodifiable hidden attribute on the object. Setting the attribute as a constant will not propagate an update. |
|
|
491
|
+
| get(attribute, defaultValue) | Method to retrieve the value of an attribute. It does not provide any advantage compared to accessing directly the attribute (e.g., `author.name`); except for hidden fields and constants, which can be retrieved only with the `.get` method. Additionally, you can provide a default value as a second parameter in case the object doesn't have that attribute. |
|
|
492
|
+
| getRelation(model, filterFunction) | To get all the objects respecting a specific relation with this object (see [model relations](#model-relations)). |
|
|
493
|
+
| save() | Method to save the object. You can do `store.save()` instead. |
|
|
494
|
+
| destroy() | Method to delete the object. You can do `store.delete()` instead. |
|
|
495
|
+
|
|
496
|
+
| toJSON() | It returns a pure JSON representation of the object. |
|
|
497
|
+
| toString() | It returns a string representation of the object. |
|
|
498
|
+
| getFingerprint() | It returns a hash of the object. The hash changes at every change of the object or of any nested object. Useful to detect object changes. |
|
|
499
|
+
| getModel() | It returns the model of this object. Mostly useful to do `object.getModel().getType()` and obtain a string defining the type of the object. |
|
|
496
500
|
|
|
497
501
|
## Editing objects
|
|
498
502
|
The option `autoSave` can be `true`, `false`, or a number (milliseconds).
|
package/dist/Obj.js
CHANGED
|
@@ -76,8 +76,10 @@ var Obj = /*#__PURE__*/_createClass(function Obj(values, _model) {
|
|
|
76
76
|
return (0, _fingerprint["default"])(_this.toJSON());
|
|
77
77
|
});
|
|
78
78
|
|
|
79
|
-
_defineProperty(this, "get", function (attribute) {
|
|
80
|
-
|
|
79
|
+
_defineProperty(this, "get", function (attribute, defaultValue) {
|
|
80
|
+
var _ref, _classPrivateFieldGet2;
|
|
81
|
+
|
|
82
|
+
return (_ref = (_classPrivateFieldGet2 = _classPrivateFieldGet(_this, _setHidden)[attribute]) !== null && _classPrivateFieldGet2 !== void 0 ? _classPrivateFieldGet2 : _this[attribute]) !== null && _ref !== void 0 ? _ref : defaultValue;
|
|
81
83
|
});
|
|
82
84
|
|
|
83
85
|
_defineProperty(this, "getRelation", function (type, filterFunction) {
|
|
@@ -98,6 +100,12 @@ var Obj = /*#__PURE__*/_createClass(function Obj(values, _model) {
|
|
|
98
100
|
return _this.getModel().getStore().update([_this]);
|
|
99
101
|
});
|
|
100
102
|
|
|
103
|
+
_defineProperty(this, "setConstant", function (attribute, value) {
|
|
104
|
+
var _classPrivateFieldGet3;
|
|
105
|
+
|
|
106
|
+
_classPrivateFieldGet(_this, _setHidden)[attribute] = (_classPrivateFieldGet3 = _classPrivateFieldGet(_this, _setHidden)[attribute]) !== null && _classPrivateFieldGet3 !== void 0 ? _classPrivateFieldGet3 : value;
|
|
107
|
+
});
|
|
108
|
+
|
|
101
109
|
_defineProperty(this, "save", function () {
|
|
102
110
|
return _this.getModel().getStore().save([_this]);
|
|
103
111
|
});
|
package/dist/ObserverStore.js
CHANGED
|
@@ -15,19 +15,19 @@ var _PersistentStore2 = _interopRequireDefault(require("./PersistentStore"));
|
|
|
15
15
|
|
|
16
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
17
17
|
|
|
18
|
-
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(
|
|
18
|
+
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(_e2) { throw _e2; }, 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(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
19
19
|
|
|
20
|
-
function
|
|
20
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
21
21
|
|
|
22
|
-
function
|
|
22
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
23
23
|
|
|
24
24
|
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); }
|
|
25
25
|
|
|
26
|
-
function
|
|
26
|
+
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; }
|
|
27
27
|
|
|
28
|
-
function
|
|
28
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
29
29
|
|
|
30
|
-
function
|
|
30
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
31
31
|
|
|
32
32
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
33
33
|
|
|
@@ -92,14 +92,31 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
|
|
|
92
92
|
_classPrivateMethodInitSpec(_assertThisInitialized(_this), _propagateInsertChange);
|
|
93
93
|
|
|
94
94
|
_defineProperty(_assertThisInitialized(_this), "multipleSubscribe", function (subscriptions, callback) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
95
|
+
var dataPayload = {};
|
|
96
|
+
|
|
97
|
+
var areAllDone = function areAllDone() {
|
|
98
|
+
return subscriptions.map(function (_ref) {
|
|
99
|
+
var _ref2 = _slicedToArray(_ref, 1),
|
|
100
|
+
name = _ref2[0];
|
|
101
|
+
|
|
102
|
+
return name;
|
|
103
|
+
}).every(function (name) {
|
|
104
|
+
return dataPayload[name] !== undefined;
|
|
102
105
|
});
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
return Promise.all(subscriptions.map(function (sub) {
|
|
109
|
+
var _sub = _slicedToArray(sub, 2),
|
|
110
|
+
name = _sub[0],
|
|
111
|
+
_sub$ = _sub[1],
|
|
112
|
+
filterFunction = _sub$ === void 0 ? null : _sub$;
|
|
113
|
+
|
|
114
|
+
var wrappedCallback = function wrappedCallback(data) {
|
|
115
|
+
dataPayload[name] = data;
|
|
116
|
+
return areAllDone() && callback(dataPayload);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return _this.subscribe(name, wrappedCallback, filterFunction);
|
|
103
120
|
})).then(function (subKeys) {
|
|
104
121
|
var subKey = (0, _uuid.v4)();
|
|
105
122
|
_this._multipleSubscribed[subKey] = subKeys;
|
|
@@ -209,9 +226,9 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
|
|
|
209
226
|
|
|
210
227
|
var uniqueSubs = _classPrivateFieldGet(_assertThisInitialized(_this), _getUniqueSubs).call(_assertThisInitialized(_this), objects, type);
|
|
211
228
|
|
|
212
|
-
(0, _batchPromises["default"])(10, uniqueSubs, function (
|
|
213
|
-
var callback =
|
|
214
|
-
filterFunction =
|
|
229
|
+
(0, _batchPromises["default"])(10, uniqueSubs, function (_ref3) {
|
|
230
|
+
var callback = _ref3.callback,
|
|
231
|
+
filterFunction = _ref3.filterFunction;
|
|
215
232
|
return _this.find(type, filterFunction).then(callback);
|
|
216
233
|
});
|
|
217
234
|
}
|
|
@@ -284,8 +301,8 @@ function _propagateInsertChange2(type, newObjects) {
|
|
|
284
301
|
var uniqueSubs = {};
|
|
285
302
|
var objects = Object.values(this._subscribed[type]);
|
|
286
303
|
|
|
287
|
-
for (var
|
|
288
|
-
var object = _objects[
|
|
304
|
+
for (var _i2 = 0, _objects = objects; _i2 < _objects.length; _i2++) {
|
|
305
|
+
var object = _objects[_i2];
|
|
289
306
|
|
|
290
307
|
var _iterator5 = _createForOfIteratorHelper(object),
|
|
291
308
|
_step5;
|
|
@@ -306,9 +323,9 @@ function _propagateInsertChange2(type, newObjects) {
|
|
|
306
323
|
}
|
|
307
324
|
|
|
308
325
|
var possibleSubs = Object.values(uniqueSubs);
|
|
309
|
-
(0, _batchPromises["default"])(10, possibleSubs, function (
|
|
310
|
-
var callback =
|
|
311
|
-
filterFunction =
|
|
326
|
+
(0, _batchPromises["default"])(10, possibleSubs, function (_ref4) {
|
|
327
|
+
var callback = _ref4.callback,
|
|
328
|
+
filterFunction = _ref4.filterFunction;
|
|
312
329
|
var objectsToSubscribe = filterFunction ? newObjects.filter(filterFunction) : newObjects;
|
|
313
330
|
|
|
314
331
|
if (objectsToSubscribe.length) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dataflux",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "DataFlux, automatically interfaces with your REST APIs to create a 2-way-synced local data store. Transparently manages data propagation in the React state.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": "dist/index.js",
|