dataflux 1.2.0 → 1.2.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.
@@ -0,0 +1,17 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a bug report
4
+ title: ''
5
+ labels: bug
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **Provide an example**
14
+ Provide an example in terms of prefixes and BGP messages. Possibly provide a snippet of config.yml and prefixes.yml.
15
+
16
+ **Expected behavior**
17
+ A clear and concise description of what you expected to happen.
@@ -0,0 +1,17 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: enhancement
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe what you would like to achieve**
11
+ A clear and concise description of what you want to happen.
12
+
13
+ **Describe why the current solution (if any) is not satisfactory**
14
+ A clear and concise description.
15
+
16
+ **Provide an example**
17
+ Provide an example in terms of prefixes and BGP messages. Possibly provide a snippet of config.yml and prefixes.yml.
@@ -0,0 +1,8 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: npm
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ open-pull-requests-limit: 10
8
+ target-branch: dev
@@ -0,0 +1,20 @@
1
+ # Number of days of inactivity before an issue becomes stale
2
+ daysUntilStale: 90
3
+ # Number of days of inactivity before a stale issue is closed
4
+ daysUntilClose: 1
5
+ # Issues with these labels will never be considered stale
6
+ exemptLabels:
7
+ - pinned
8
+ - security
9
+ - no-stale
10
+
11
+ # Label to use when marking an issue as stale
12
+ staleLabel: wontfix
13
+
14
+ markComment: false
15
+
16
+ # Comment to post when marking an issue as stale. Set to `false` to disable
17
+ closeComment: >
18
+ This issue has been automatically closed as stale.
19
+ This mechanism helps to prioritize feature requests which received more support from the community.
20
+ If you want to open again this issue you have to provide a Pull Request.
@@ -0,0 +1,83 @@
1
+ name: Main
2
+
3
+ on:
4
+ push:
5
+ branches: [ "*" ]
6
+ pull_request:
7
+ branches: [ "*" ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+
15
+ - name: Set up Javascript/Node
16
+ uses: actions/setup-node@v2
17
+ with:
18
+ node-version: '14'
19
+
20
+ - name: Check out code
21
+ uses: actions/checkout@v2
22
+ with:
23
+ fetch-depth: '0'
24
+
25
+ - name: Cache multiple paths
26
+ uses: actions/cache@v2
27
+ with:
28
+ path: ~/.npm
29
+ key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
30
+ restore-keys: |
31
+ ${{ runner.os }}-node-
32
+
33
+ - name: Build
34
+ run: |
35
+ npm install
36
+ npm run compile
37
+
38
+ - name: Upload Artifact
39
+ uses: actions/upload-artifact@v2
40
+ if: ${{ always() }}
41
+ with:
42
+ name: logs
43
+ path: ~/.npm/_logs/*
44
+ retention-days: 14
45
+
46
+ test:
47
+ name: Test
48
+ runs-on: ubuntu-latest
49
+ steps:
50
+
51
+ - name: Set up Javascript/Node
52
+ uses: actions/setup-node@v2
53
+ with:
54
+ node-version: '14'
55
+
56
+ - name: Check out code
57
+ uses: actions/checkout@v2
58
+ with:
59
+ fetch-depth: '0'
60
+
61
+ - name: Cache multiple paths
62
+ uses: actions/cache@v2
63
+ with:
64
+ path: ~/.npm
65
+ key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
66
+ restore-keys: |
67
+ ${{ runner.os }}-node-
68
+
69
+ - name: Install
70
+ run: |
71
+ npm install
72
+
73
+ - name: Tests
74
+ run: |
75
+ npm run test
76
+
77
+ - name: Upload Artifact
78
+ uses: actions/upload-artifact@v2
79
+ if: ${{ always() }}
80
+ with:
81
+ name: logs
82
+ path: ~/.npm/_logs/*
83
+ retention-days: 14
package/README.md CHANGED
@@ -205,7 +205,7 @@ The store can be configured with the following options:
205
205
 
206
206
  | Option | Description | Default |
207
207
  |-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
208
- | autoSave | It can be `true`, `false`, or an amount of milliseconds (integer). If `false`, you will have to perform `store.save()` manually. If `true`, the store will automatically perform `save()` when objects change. If an amount of milliseconds is provided, the objects are saved periodically AND when a change is detected. See [Editing objects](#editing-objects) for more information. | 3000 |
208
+ | autoSave | It can be `true`, `false`, or an amount of milliseconds (integer). If `false`, you will have to perform `store.save()` manually. If `true`, the store will automatically perform `save()` when objects change. If an amount of milliseconds is provided, the objects are saved periodically AND when a change is detected. See [Editing objects](#editing-objects) for more information. | true |
209
209
  | saveDelay | An amount of milliseconds used to defer synching operations with the server. It triggers `store.save()` milliseconds after the last change on the store's objects is detedect. This allows to bundle together multiple changes operated by an interacting user. See [Editing objects](#editing-objects) for more information. | 1000 |
210
210
  | lazyLoad | A boolean. If set to `false`, the store is pre-populated with all the models' objects. If set to `true`, models' objects are loaded only on first usage (e.g., 'find', 'subscribe', 'getRelation'). LazyLoad operates per model, only the objects of the used models are loaded. | false |
211
211
 
package/dist/Model.js CHANGED
@@ -33,12 +33,16 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
33
33
 
34
34
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
35
35
 
36
+ function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
37
+
36
38
  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; }
37
39
 
38
40
  function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
39
41
 
40
42
  function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
41
43
 
44
+ function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
45
+
42
46
  function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
43
47
 
44
48
  function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
@@ -49,6 +53,18 @@ function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!priva
49
53
 
50
54
  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; } }
51
55
 
56
+ var applyData = function applyData(obj, data) {
57
+ for (var att in data) {
58
+ if (att !== "id" || obj.id === undefined || att === "id" && obj.id === data.id) {
59
+ obj[att] = data[att];
60
+ } else {
61
+ return Promise.reject("The loading function cannot change the id of the object.");
62
+ }
63
+ }
64
+
65
+ return Promise.resolve(obj);
66
+ };
67
+
52
68
  var _type = /*#__PURE__*/new WeakMap();
53
69
 
54
70
  var _store = /*#__PURE__*/new WeakMap();
@@ -71,6 +87,8 @@ var _axios = /*#__PURE__*/new WeakMap();
71
87
 
72
88
  var _loadFunction = /*#__PURE__*/new WeakMap();
73
89
 
90
+ var _error = /*#__PURE__*/new WeakSet();
91
+
74
92
  var _addRelationByField = /*#__PURE__*/new WeakMap();
75
93
 
76
94
  var _addRelationByFilter = /*#__PURE__*/new WeakMap();
@@ -92,6 +110,8 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
92
110
 
93
111
  _classCallCheck(this, Model);
94
112
 
113
+ _classPrivateMethodInitSpec(this, _error);
114
+
95
115
  _classPrivateFieldInitSpec(this, _type, {
96
116
  writable: true,
97
117
  value: void 0
@@ -160,35 +180,31 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
160
180
  });
161
181
 
162
182
  _defineProperty(this, "load", function (obj) {
163
- return new Promise(function (resolve, reject) {
164
- var applyData = function applyData(data) {
165
- for (var att in data) {
166
- if (att !== "id" || obj.id === undefined || att === "id" && obj.id === data.id) {
167
- obj[att] = data[att];
168
- } else {
169
- return Promise.reject("The loading function cannot change the id of the object.");
170
- }
171
- }
172
- };
173
-
174
- if (_classPrivateFieldGet(_this, _loadFunction)) {
175
- var res = _classPrivateFieldGet(_this, _loadFunction).call(_this, obj.toJson());
183
+ if (_classPrivateFieldGet(_this, _loadFunction)) {
184
+ return _this.getStore().whenSaved(_this.getType())["catch"](function () {
185
+ throw new Error("You cannot perform load() on an unsaved object.");
186
+ }).then(function () {
187
+ var res = _classPrivateFieldGet(_this, _loadFunction).call(_this, obj.toJSON());
176
188
 
177
189
  if (typeof res === "string") {
178
- _classPrivateFieldGet(_this, _axios).call(_this, {
190
+ return _classPrivateFieldGet(_this, _axios).call(_this, {
179
191
  method: "get",
180
192
  url: res,
181
193
  responseType: "json"
182
194
  }).then(function (data) {
183
- return applyData(data.data);
184
- }).then(resolve);
195
+ return data.data;
196
+ });
185
197
  } else {
186
- res.then(applyData).then(resolve);
198
+ return res;
187
199
  }
188
- } else {
189
- reject("You must define a loading function in the model to enable load().");
190
- }
191
- });
200
+ }).then(function (data) {
201
+ return applyData(obj, data);
202
+ })["catch"](function (error) {
203
+ return _classPrivateMethodGet(_this, _error, _error2).call(_this, error);
204
+ });
205
+ } else {
206
+ return _classPrivateMethodGet(_this, _error, _error2).call(_this, "You must define a loading function in the model to enable load().");
207
+ }
192
208
  });
193
209
 
194
210
  _defineProperty(this, "addRelation", function (model, param2, param3) {
@@ -216,17 +232,15 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
216
232
  var filterRelation = _classPrivateFieldGet(_this, _includes)[includedType];
217
233
 
218
234
  if (filterRelation) {
219
- return _this.getStore().find(includedType, function (item) {
220
- return filterRelation(parentObject, item);
221
- }).then(function (data) {
222
- if (filterFunction) {
223
- return data.filter(filterFunction);
224
- }
225
-
226
- return data;
235
+ return parentObject.load()["catch"](function () {}).then(function () {
236
+ return _this.getStore().find(includedType, function (item) {
237
+ return filterRelation(parentObject, item);
238
+ }).then(function (data) {
239
+ return filterFunction ? data.filter(filterFunction) : data;
240
+ });
227
241
  });
228
242
  } else {
229
- return Promise.reject("The relation doesn't exist");
243
+ return _classPrivateMethodGet(_this, _error, _error2).call(_this, "The relation doesn't exist");
230
244
  }
231
245
  });
232
246
 
@@ -235,7 +249,15 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
235
249
  });
236
250
 
237
251
  _defineProperty(this, "retrieveAll", function () {
238
- return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(_this, _retrieveHook), null, _classPrivateFieldGet(_this, _axios)).then(_classPrivateFieldGet(_this, _toArray));
252
+ return (0, _modelHooksUtils.executeHook)("retrieve", _classPrivateFieldGet(_this, _retrieveHook), null, _classPrivateFieldGet(_this, _axios)).then(function (data) {
253
+ if (Array.isArray(data)) {
254
+ return data;
255
+ } else {
256
+ _classPrivateFieldSet(_this, _singleItemQuery, true);
257
+
258
+ return [data];
259
+ }
260
+ });
239
261
  });
240
262
 
241
263
  _defineProperty(this, "insertObjects", function (objects) {
@@ -275,9 +297,13 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
275
297
  writable: true,
276
298
  value: function value(objects, action) {
277
299
  if (_classPrivateFieldGet(_this, _singleItemQuery)) {
278
- return (0, _batchPromises["default"])(_classPrivateFieldGet(_this, _batchSize), objects, action);
300
+ return (0, _batchPromises["default"])(_classPrivateFieldGet(_this, _batchSize), objects.map(function (i) {
301
+ return i.toJSON();
302
+ }), action);
279
303
  } else {
280
- return action(objects);
304
+ return action(objects.map(function (i) {
305
+ return i.toJSON();
306
+ }));
281
307
  }
282
308
  }
283
309
  });
@@ -288,8 +314,6 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
288
314
  if (Array.isArray(data)) {
289
315
  return data;
290
316
  } else {
291
- _classPrivateFieldSet(_this, _singleItemQuery, true);
292
-
293
317
  return [data];
294
318
  }
295
319
  }
@@ -356,4 +380,10 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
356
380
 
357
381
  });
358
382
 
359
- exports["default"] = Model;
383
+ exports["default"] = Model;
384
+
385
+ function _error2(error) {
386
+ error = error.message || error;
387
+ this.getStore().pubSub.publish("error", error);
388
+ return Promise.reject(error);
389
+ }
@@ -35,7 +35,7 @@ function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) {
35
35
 
36
36
  var _loaded = /*#__PURE__*/new WeakMap();
37
37
 
38
- var Obj = /*#__PURE__*/_createClass(function Obj(values, model) {
38
+ var Obj = /*#__PURE__*/_createClass(function Obj(values, _model) {
39
39
  var _this = this;
40
40
 
41
41
  _classCallCheck(this, Obj);
@@ -49,16 +49,20 @@ var Obj = /*#__PURE__*/_createClass(function Obj(values, model) {
49
49
  if (_classPrivateFieldGet(_this, _loaded)) {
50
50
  return Promise.resolve(_this);
51
51
  } else {
52
- return _this.getModel().load(_this).then(function () {
52
+ var model = _this.getModel();
53
+
54
+ return model.load(_this).then(function () {
53
55
  _classPrivateFieldSet(_this, _loaded, true);
54
56
 
57
+ return model.getStore().update([_this], true); // Propagate update
58
+ }).then(function () {
55
59
  return _this;
56
- });
60
+ }); // return always this
57
61
  }
58
62
  });
59
63
 
60
64
  _defineProperty(this, "getFingerprint", function () {
61
- return (0, _fingerprint["default"])(_this.toJson());
65
+ return (0, _fingerprint["default"])(_this.toJSON());
62
66
  });
63
67
 
64
68
  _defineProperty(this, "get", function (attribute) {
@@ -75,8 +79,7 @@ var Obj = /*#__PURE__*/_createClass(function Obj(values, model) {
75
79
  }
76
80
 
77
81
  _this[attribute] = value;
78
-
79
- _this.getModel().getStore().update([_this]);
82
+ return _this.getModel().getStore().update([_this]);
80
83
  });
81
84
 
82
85
  _defineProperty(this, "save", function () {
@@ -87,7 +90,7 @@ var Obj = /*#__PURE__*/_createClass(function Obj(values, model) {
87
90
  return _this.getModel().getStore()["delete"]([_this]);
88
91
  });
89
92
 
90
- _defineProperty(this, "toJson", function () {
93
+ _defineProperty(this, "toJSON", function () {
91
94
  var attrs = Object.keys(_this);
92
95
  var out = {};
93
96
 
@@ -103,11 +106,11 @@ var Obj = /*#__PURE__*/_createClass(function Obj(values, model) {
103
106
  });
104
107
 
105
108
  _defineProperty(this, "toString", function () {
106
- return JSON.stringify(_this.toJson());
109
+ return JSON.stringify(_this.toJSON());
107
110
  });
108
111
 
109
112
  this.getModel = function () {
110
- return model;
113
+ return _model;
111
114
  };
112
115
 
113
116
  Object.keys(values).forEach(function (key) {
@@ -208,8 +208,8 @@ var ObserverStore = /*#__PURE__*/function (_PersistentStore) {
208
208
 
209
209
  _createClass(ObserverStore, [{
210
210
  key: "update",
211
- value: function update(objects) {
212
- return _get(_getPrototypeOf(ObserverStore.prototype), "update", this).call(this, objects).then(_classPrivateFieldGet(this, _propagateChange));
211
+ value: function update(objects, skipSave) {
212
+ return _get(_getPrototypeOf(ObserverStore.prototype), "update", this).call(this, objects, skipSave).then(_classPrivateFieldGet(this, _propagateChange));
213
213
  }
214
214
  }, {
215
215
  key: "insert",
@@ -49,11 +49,35 @@ var PersistentStore = /*#__PURE__*/function (_Store) {
49
49
 
50
50
  _this = _super.call(this, options);
51
51
 
52
+ _defineProperty(_assertThisInitialized(_this), "whenSaved", function (type) {
53
+ return _this.getDiff(type).then(function (_ref) {
54
+ var inserted = _ref.inserted,
55
+ updated = _ref.updated,
56
+ deleted = _ref.deleted;
57
+
58
+ if (inserted.length === 0 && updated.length === 0 && deleted.length === 0) {
59
+ return true;
60
+ } else if (_this.options.autoSave) {
61
+ return _this._saveDiff(type, {
62
+ inserted: inserted,
63
+ updated: updated,
64
+ deleted: deleted
65
+ });
66
+ } else {
67
+ return Promise.reject("Save must be invoked manually");
68
+ }
69
+ });
70
+ });
71
+
52
72
  _defineProperty(_assertThisInitialized(_this), "save", function () {
53
73
  _this._busy = true;
54
74
 
55
75
  _this.pubSub.publish("save", "start");
56
76
 
77
+ if (_this._delayedSaveTimer) {
78
+ clearTimeout(_this._delayedSaveTimer);
79
+ }
80
+
57
81
  return Promise.all(Object.keys(_this.models).map(_this._saveByType)).then(function (data) {
58
82
  _this._busy = false;
59
83
 
@@ -71,33 +95,43 @@ var PersistentStore = /*#__PURE__*/function (_Store) {
71
95
  });
72
96
  });
73
97
 
98
+ _defineProperty(_assertThisInitialized(_this), "_saveDiff", function (type, _ref2) {
99
+ var inserted = _ref2.inserted,
100
+ updated = _ref2.updated,
101
+ deleted = _ref2.deleted;
102
+ var model = _this.models[type].model; // Operations order:
103
+ // 1) insert
104
+ // 2) update
105
+ // 3) delete
106
+
107
+ return model.insertObjects(inserted.map(function (i) {
108
+ return i.object;
109
+ })).then(function () {
110
+ return _this.applyDiff({
111
+ inserted: inserted
112
+ }, type);
113
+ }).then(function () {
114
+ return model.updateObjects(updated.map(function (i) {
115
+ return i.object;
116
+ }));
117
+ }).then(function () {
118
+ return _this.applyDiff({
119
+ updated: updated
120
+ }, type);
121
+ }).then(function () {
122
+ return model.deleteObjects(deleted.map(function (i) {
123
+ return i.object;
124
+ }));
125
+ }).then(function () {
126
+ return _this.applyDiff({
127
+ deleted: deleted
128
+ }, type);
129
+ });
130
+ });
131
+
74
132
  _defineProperty(_assertThisInitialized(_this), "_saveByType", function (type) {
75
- return _this.getDiff(type).then(function (_ref) {
76
- var inserted = _ref.inserted,
77
- updated = _ref.updated,
78
- deleted = _ref.deleted;
79
- var model = _this.models[type].model; // Operations order:
80
- // 1) insert
81
- // 2) update
82
- // 3) delete
83
-
84
- return model.insertObjects(inserted).then(function () {
85
- return _this.applyDiff({
86
- inserted: inserted
87
- }, type);
88
- }).then(function () {
89
- return model.updateObjects(updated);
90
- }).then(function () {
91
- return _this.applyDiff({
92
- updated: updated
93
- }, type);
94
- }).then(function () {
95
- return model.deleteObjects(deleted);
96
- }).then(function () {
97
- return _this.applyDiff({
98
- deleted: deleted
99
- }, type);
100
- });
133
+ return _this.getDiff(type).then(function (diff) {
134
+ return _this._saveDiff(type, diff);
101
135
  });
102
136
  });
103
137
 
@@ -131,8 +165,7 @@ var PersistentStore = /*#__PURE__*/function (_Store) {
131
165
  var _this2 = this;
132
166
 
133
167
  this._busy = true;
134
-
135
- _get(_getPrototypeOf(PersistentStore.prototype), "addModel", this).call(this, model).then(function () {
168
+ return _get(_getPrototypeOf(PersistentStore.prototype), "addModel", this).call(this, model).then(function () {
136
169
  _this2._busy = false;
137
170
  });
138
171
  }
@@ -160,11 +193,13 @@ var PersistentStore = /*#__PURE__*/function (_Store) {
160
193
  }
161
194
  }, {
162
195
  key: "update",
163
- value: function update(objects) {
196
+ value: function update(objects, skipSave) {
164
197
  var _this5 = this;
165
198
 
166
199
  return _get(_getPrototypeOf(PersistentStore.prototype), "update", this).call(this, objects).then(function (data) {
167
- _this5.delayedSave();
200
+ if (!skipSave) {
201
+ _this5.delayedSave();
202
+ }
168
203
 
169
204
  return data;
170
205
  });
@@ -108,7 +108,7 @@ var ReactStore = /*#__PURE__*/function (_ObserverStore) {
108
108
  _createClass(ReactStore, [{
109
109
  key: "findAll",
110
110
  value: function findAll(type, stateAttribute, context, filterFunction) {
111
- _classPrivateMethodGet(this, _fixState, _fixState2).call(this, stateAttribute, context);
111
+ _classPrivateMethodGet(this, _fixState, _fixState2).call(this, stateAttribute, context, false);
112
112
 
113
113
  var subKey = this.subscribe(type, function (data) {
114
114
  context.setState(_objectSpread(_objectSpread({}, context.state), {}, _defineProperty({}, stateAttribute, data || [])));
@@ -118,7 +118,7 @@ var ReactStore = /*#__PURE__*/function (_ObserverStore) {
118
118
  }, {
119
119
  key: "findOne",
120
120
  value: function findOne(type, stateAttribute, context, filterFunction) {
121
- _classPrivateMethodGet(this, _fixState, _fixState2).call(this, stateAttribute, context);
121
+ _classPrivateMethodGet(this, _fixState, _fixState2).call(this, stateAttribute, context, true);
122
122
 
123
123
  var subKey = this.subscribe(type, function (data) {
124
124
  context.setState(_objectSpread(_objectSpread({}, context.state), {}, _defineProperty({}, stateAttribute, data && data.length ? data[0] : null)));
@@ -132,8 +132,8 @@ var ReactStore = /*#__PURE__*/function (_ObserverStore) {
132
132
 
133
133
  exports["default"] = ReactStore;
134
134
 
135
- function _fixState2(stateAttribute, context) {
135
+ function _fixState2(stateAttribute, context, one) {
136
136
  if (!context[stateAttribute]) {
137
- context[stateAttribute] = []; // side effect on state
137
+ context[stateAttribute] = one ? null : []; // side effect on state
138
138
  }
139
139
  }
package/dist/Store.js CHANGED
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports["default"] = void 0;
7
7
 
8
- var _StoreObject = _interopRequireDefault(require("./StoreObject"));
8
+ var _Obj = _interopRequireDefault(require("./Obj"));
9
9
 
10
10
  var _PubSub = _interopRequireDefault(require("./PubSub"));
11
11
 
@@ -17,21 +17,29 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
17
17
 
18
18
  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; }
19
19
 
20
- 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); }
21
-
22
20
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
21
 
24
22
  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, descriptor.key, descriptor); } }
25
23
 
26
24
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
27
25
 
26
+ function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
27
+
28
28
  function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
29
29
 
30
30
  function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
31
31
 
32
+ function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
33
+
34
+ function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
35
+
36
+ function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
37
+
32
38
  function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
33
39
 
34
- var _deleteByObject = /*#__PURE__*/new WeakSet();
40
+ var _error = /*#__PURE__*/new WeakSet();
41
+
42
+ var _deleteByObject = /*#__PURE__*/new WeakMap();
35
43
 
36
44
  var _deleteByFilter = /*#__PURE__*/new WeakSet();
37
45
 
@@ -43,6 +51,8 @@ var _loadObjects = /*#__PURE__*/new WeakSet();
43
51
 
44
52
  var Store = /*#__PURE__*/function () {
45
53
  function Store() {
54
+ var _this = this;
55
+
46
56
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
47
57
 
48
58
  _classCallCheck(this, Store);
@@ -55,10 +65,23 @@ var Store = /*#__PURE__*/function () {
55
65
 
56
66
  _classPrivateMethodInitSpec(this, _deleteByFilter);
57
67
 
58
- _classPrivateMethodInitSpec(this, _deleteByObject);
68
+ _classPrivateMethodInitSpec(this, _error);
69
+
70
+ _classPrivateFieldInitSpec(this, _deleteByObject, {
71
+ writable: true,
72
+ value: function value(object) {
73
+ var id = object.getId();
74
+
75
+ var filterFunction = function filterFunction(item) {
76
+ return id === item.getId();
77
+ };
78
+
79
+ return _classPrivateMethodGet(_this, _deleteByFilter, _deleteByFilter2).call(_this, object.getModel().getType(), filterFunction);
80
+ }
81
+ });
59
82
 
60
83
  this.options = {
61
- autoSave: options.autoSave !== undefined ? options.autoSave : 5000,
84
+ autoSave: options.autoSave !== undefined ? options.autoSave : true,
62
85
  saveDelay: options.saveDelay || 1000,
63
86
  lazyLoad: options.lazyLoad || false
64
87
  };
@@ -83,65 +106,75 @@ var Store = /*#__PURE__*/function () {
83
106
  }, {
84
107
  key: "addModel",
85
108
  value: function addModel(model) {
86
- var _this = this;
109
+ var _this2 = this;
87
110
 
88
111
  return new Promise(function (resolve, reject) {
89
- _this.validateModel(model);
112
+ _this2.validateModel(model);
90
113
 
91
114
  var type = model.getType();
92
115
 
93
- if (!_this.models[type]) {
94
- _this.models[type] = {
116
+ if (!_this2.models[type]) {
117
+ _this2.models[type] = {
95
118
  model: model,
96
119
  storedObjects: {}
97
120
  };
98
- model.setStore(_this);
121
+ model.setStore(_this2);
99
122
 
100
- if (!_this.options.lazyLoad) {
101
- resolve(_classPrivateMethodGet(_this, _loadObjects, _loadObjects2).call(_this, type));
123
+ if (!_this2.options.lazyLoad) {
124
+ resolve(_classPrivateMethodGet(_this2, _loadObjects, _loadObjects2).call(_this2, type));
102
125
  } else {
103
126
  resolve();
104
127
  }
105
128
  } else {
106
- reject("The model already exists");
129
+ var error = "The model already exists";
130
+
131
+ _this2.pubSub.publish("error", error);
132
+
133
+ reject(error);
107
134
  }
108
135
  });
109
136
  }
110
137
  }, {
111
138
  key: "update",
112
139
  value: function update(objects) {
113
- return Promise.resolve(); // Nothing to do at this level
140
+ return Promise.resolve(objects); // Nothing to do at this level
114
141
  }
115
142
  }, {
116
143
  key: "delete",
117
144
  value: function _delete(typeOrObjects, filterFunction) {
118
145
  if (typeof typeOrObjects === "string" && typeof filterFunction === "function") {
119
- return _classPrivateMethodGet(this, _deleteByFilter, _deleteByFilter2).call(this, typeOrObjects, filterFunction);
120
- } else if (_typeof(typeOrObjects) === "object" && typeOrObjects.length) {
121
- return Promise.all(typeOrObjects.map(_classPrivateMethodGet(this, _deleteByObject, _deleteByObject2)));
146
+ var type = typeOrObjects;
147
+ return _classPrivateMethodGet(this, _deleteByFilter, _deleteByFilter2).call(this, type, filterFunction);
148
+ } else if (Array.isArray(typeOrObjects) && typeOrObjects.length && !filterFunction) {
149
+ var objects = typeOrObjects;
150
+ return Promise.all(objects.map(_classPrivateFieldGet(this, _deleteByObject))).then(function (data) {
151
+ return data.flat();
152
+ });
122
153
  } else {
123
- return Promise.reject("Invalid delete request. You have to provide a list of objects or a type and a filter function");
154
+ var error = "Invalid delete request. You have to provide a list of objects or a type and a filter function";
155
+ this.pubSub.publish("error", error);
156
+ return Promise.reject(error);
124
157
  }
125
158
  }
126
159
  }, {
127
160
  key: "insert",
128
161
  value: function insert(type, objects) {
129
- var _this2 = this;
162
+ var _this3 = this;
130
163
 
131
164
  return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
132
165
  return objects.map(function (object) {
133
- return _classPrivateMethodGet(_this2, _insertObject, _insertObject2).call(_this2, type, object, true);
166
+ return _classPrivateMethodGet(_this3, _insertObject, _insertObject2).call(_this3, type, object, true);
134
167
  });
135
168
  });
136
169
  }
137
170
  }, {
138
171
  key: "get",
139
172
  value: function get(type, id) {
140
- var _this3 = this;
173
+ var _this4 = this;
141
174
 
142
175
  return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
143
176
  try {
144
- return _this3.models[type].storedObjects[id].object;
177
+ return _this4.models[type].storedObjects[id].object;
145
178
  } catch (error) {
146
179
  return Promise.reject("Object not found");
147
180
  }
@@ -150,17 +183,17 @@ var Store = /*#__PURE__*/function () {
150
183
  }, {
151
184
  key: "find",
152
185
  value: function find(type, filterFunction) {
153
- var _this4 = this;
186
+ var _this5 = this;
154
187
 
155
188
  return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
156
- var all = Object.values(_this4.models[type].storedObjects).filter(function (i) {
189
+ var all = Object.values(_this5.models[type].storedObjects).filter(function (i) {
157
190
  return i.status !== "deleted";
158
191
  }).map(function (i) {
159
192
  return i.object;
160
193
  });
161
194
  return filterFunction ? all.filter(filterFunction) : all;
162
195
  })["catch"](function (error) {
163
- _this4.pubSub.publish("error", error);
196
+ _this5.pubSub.publish("error", error);
164
197
 
165
198
  return Promise.reject(error);
166
199
  });
@@ -168,7 +201,7 @@ var Store = /*#__PURE__*/function () {
168
201
  }, {
169
202
  key: "applyDiff",
170
203
  value: function applyDiff(_ref, type) {
171
- var _this5 = this;
204
+ var _this6 = this;
172
205
 
173
206
  var _ref$inserted = _ref.inserted,
174
207
  inserted = _ref$inserted === void 0 ? [] : _ref$inserted,
@@ -185,7 +218,7 @@ var Store = /*#__PURE__*/function () {
185
218
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
186
219
  var object = _step.value;
187
220
 
188
- var item = _this5.models[type || object.object.getModel().getType()].storedObjects[object.id];
221
+ var item = _this6.models[type || object.object.getModel().getType()].storedObjects[object.id];
189
222
 
190
223
  item.fingerprint = object.object.getFingerprint();
191
224
  item.status = "old";
@@ -202,7 +235,7 @@ var Store = /*#__PURE__*/function () {
202
235
  try {
203
236
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
204
237
  var _object = _step2.value;
205
- delete _this5.models[type || _object.object.getModel().getType()].storedObjects[_object.id];
238
+ delete _this6.models[type || _object.object.getModel().getType()].storedObjects[_object.id];
206
239
  }
207
240
  } catch (err) {
208
241
  _iterator2.e(err);
@@ -216,13 +249,19 @@ var Store = /*#__PURE__*/function () {
216
249
  }
217
250
  });
218
251
  }
252
+ }, {
253
+ key: "hasChanged",
254
+ value: function hasChanged(type, object) {
255
+ var obj = this.models[type].storedObjects[object.getId()];
256
+ return obj.fingerprint !== obj.object.getFingerprint();
257
+ }
219
258
  }, {
220
259
  key: "getDiff",
221
260
  value: function getDiff(type) {
222
- var _this6 = this;
261
+ var _this7 = this;
223
262
 
224
263
  return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
225
- var objects = Object.values(_this6.models[type].storedObjects);
264
+ var objects = Object.values(_this7.models[type].storedObjects);
226
265
  var inserted = [];
227
266
  var updated = [];
228
267
  var deleted = [];
@@ -234,7 +273,7 @@ var Store = /*#__PURE__*/function () {
234
273
  inserted.push(object);
235
274
  } else if (object.status === "deleted") {
236
275
  deleted.push(object);
237
- } else if (object.fingerprint !== object.object.getFingerprint()) {
276
+ } else if (_this7.hasChanged(type, object.object)) {
238
277
  updated.push(object);
239
278
  }
240
279
  }
@@ -253,17 +292,17 @@ var Store = /*#__PURE__*/function () {
253
292
 
254
293
  exports["default"] = Store;
255
294
 
256
- function _deleteByObject2(object) {
257
- return _classPrivateMethodGet(this, _deleteByFilter, _deleteByFilter2).call(this, object.getType(), function (item) {
258
- return object.getId() === item.getId();
259
- });
295
+ function _error2(error) {
296
+ error = error.message || error;
297
+ this.pubSub.publish("error", error);
298
+ return Promise.reject(error);
260
299
  }
261
300
 
262
301
  function _deleteByFilter2(type, filterFunction) {
263
- var _this7 = this;
302
+ var _this8 = this;
264
303
 
265
304
  return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
266
- var deleted = Object.values(_this7.models[type].storedObjects).filter(function (i) {
305
+ var deleted = Object.values(_this8.models[type].storedObjects).filter(function (i) {
267
306
  return filterFunction(i.object);
268
307
  });
269
308
 
@@ -281,12 +320,14 @@ function _deleteByFilter2(type, filterFunction) {
281
320
  _iterator3.f();
282
321
  }
283
322
 
284
- return true;
323
+ return deleted.map(function (i) {
324
+ return i.object;
325
+ });
285
326
  });
286
327
  }
287
328
 
288
329
  function _getPromise2(type) {
289
- var _this8 = this;
330
+ var _this9 = this;
290
331
 
291
332
  if (!this.models[type]) {
292
333
  return Promise.reject("The model doesn't exist");
@@ -294,7 +335,7 @@ function _getPromise2(type) {
294
335
  return Promise.reject("The model is not loaded");
295
336
  } else if (!this.models[type].promise && this.options.lazyLoad) {
296
337
  return _classPrivateMethodGet(this, _loadObjects, _loadObjects2).call(this, type).then(function () {
297
- return _this8.models[type].promise;
338
+ return _this9.models[type].promise;
298
339
  });
299
340
  } else {
300
341
  return this.models[type].promise;
@@ -304,7 +345,7 @@ function _getPromise2(type) {
304
345
  function _insertObject2(type, item) {
305
346
  var markAsNew = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
306
347
  var model = this.models[type].model;
307
- var wrapper = new _StoreObject["default"](item, model);
348
+ var wrapper = new _Obj["default"](item, model);
308
349
  var id = wrapper.getId();
309
350
 
310
351
  if (this.models[type].storedObjects[id]) {
@@ -321,7 +362,7 @@ function _insertObject2(type, item) {
321
362
  }
322
363
 
323
364
  function _loadObjects2(type) {
324
- var _this9 = this;
365
+ var _this10 = this;
325
366
 
326
367
  var item = this.models[type];
327
368
  this.pubSub.publish("loading", {
@@ -336,7 +377,7 @@ function _loadObjects2(type) {
336
377
  for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
337
378
  var _item = _step4.value;
338
379
 
339
- _classPrivateMethodGet(_this9, _insertObject, _insertObject2).call(_this9, type, _item, false);
380
+ _classPrivateMethodGet(_this10, _insertObject, _insertObject2).call(_this10, type, _item, false);
340
381
  }
341
382
  } catch (err) {
342
383
  _iterator4.e(err);
@@ -344,7 +385,7 @@ function _loadObjects2(type) {
344
385
  _iterator4.f();
345
386
  }
346
387
 
347
- _this9.pubSub.publish("loading", {
388
+ _this10.pubSub.publish("loading", {
348
389
  status: "end",
349
390
  model: type
350
391
  });
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "dataflux",
3
- "version": "1.2.0",
3
+ "version": "1.2.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",
7
7
  "scripts": {
8
8
  "babel": "node_modules/.bin/babel",
9
- "test": "./node_modules/.bin/mocha tests --require @babel/register",
9
+ "test": "./node_modules/.bin/mocha tests/*.test.js --require @babel/register --exit",
10
10
  "compile": "rm -rf dist/ && ./node_modules/.bin/babel src -d dist",
11
11
  "release": "dotenv release-it"
12
12
  },
@@ -86,7 +86,10 @@
86
86
  "@babel/plugin-transform-runtime": "^7.16.10",
87
87
  "@babel/preset-env": "^7.16.11",
88
88
  "@babel/preset-react": "^7.16.7",
89
+ "chai": "^4.3.4",
90
+ "chai-subset": "^1.6.0",
89
91
  "dotenv-cli": "^4.1.1",
92
+ "mocha": "^9.1.4",
90
93
  "release-it": "^14.12.3"
91
94
  },
92
95
  "dependencies": {