dataflux 1.0.4 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +270 -127
- package/dist/Model.js +241 -57
- package/dist/ObserverStore.js +178 -131
- package/dist/PersistentStore.js +104 -75
- package/dist/PubSub.js +59 -0
- package/dist/ReactStore.js +22 -2
- package/dist/Store.js +295 -151
- package/dist/StoreObject.js +33 -0
- package/dist/fingerprint.js +8 -12
- package/dist/modelHooksUtils.js +61 -19
- package/package.json +6 -5
package/dist/PubSub.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
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; } } }; }
|
|
9
|
+
|
|
10
|
+
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); }
|
|
11
|
+
|
|
12
|
+
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; }
|
|
13
|
+
|
|
14
|
+
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); } }
|
|
15
|
+
|
|
16
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
17
|
+
|
|
18
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
19
|
+
|
|
20
|
+
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; }
|
|
21
|
+
|
|
22
|
+
var PubSub = /*#__PURE__*/_createClass(function PubSub() {
|
|
23
|
+
var _this = this;
|
|
24
|
+
|
|
25
|
+
_classCallCheck(this, PubSub);
|
|
26
|
+
|
|
27
|
+
_defineProperty(this, "subscribe", function (channel, callback) {
|
|
28
|
+
_this.callbacks[channel] = _this.callbacks[channel] || [];
|
|
29
|
+
|
|
30
|
+
_this.callbacks[channel].push(callback);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
_defineProperty(this, "publish", function (channel, content) {
|
|
34
|
+
var _iterator = _createForOfIteratorHelper(_this.callbacks[channel] || []),
|
|
35
|
+
_step;
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
var _loop = function _loop() {
|
|
39
|
+
var clb = _step.value;
|
|
40
|
+
new Promise(function (resolve, reject) {
|
|
41
|
+
clb(content, channel);
|
|
42
|
+
resolve(true);
|
|
43
|
+
})["catch"](console.log);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
47
|
+
_loop();
|
|
48
|
+
}
|
|
49
|
+
} catch (err) {
|
|
50
|
+
_iterator.e(err);
|
|
51
|
+
} finally {
|
|
52
|
+
_iterator.f();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
this.callbacks = {};
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
exports["default"] = PubSub;
|
package/dist/ReactStore.js
CHANGED
|
@@ -39,6 +39,12 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
|
|
|
39
39
|
|
|
40
40
|
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; }
|
|
41
41
|
|
|
42
|
+
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
|
|
43
|
+
|
|
44
|
+
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
45
|
+
|
|
46
|
+
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
|
|
47
|
+
|
|
42
48
|
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; } } }; }
|
|
43
49
|
|
|
44
50
|
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); }
|
|
@@ -73,6 +79,8 @@ var addSubscriptionToContext = function addSubscriptionToContext(context, subKey
|
|
|
73
79
|
};
|
|
74
80
|
};
|
|
75
81
|
|
|
82
|
+
var _fixState = /*#__PURE__*/new WeakSet();
|
|
83
|
+
|
|
76
84
|
var ReactStore = /*#__PURE__*/function (_ObserverStore) {
|
|
77
85
|
_inherits(ReactStore, _ObserverStore);
|
|
78
86
|
|
|
@@ -85,6 +93,8 @@ var ReactStore = /*#__PURE__*/function (_ObserverStore) {
|
|
|
85
93
|
|
|
86
94
|
_this2 = _super.call(this, options);
|
|
87
95
|
|
|
96
|
+
_classPrivateMethodInitSpec(_assertThisInitialized(_this2), _fixState);
|
|
97
|
+
|
|
88
98
|
_defineProperty(_assertThisInitialized(_this2), "handleChange", function (object, name) {
|
|
89
99
|
return function (event, rawValue) {
|
|
90
100
|
var value = event ? event.target.type === "checkbox" ? event.target.checked : event.target.value : "";
|
|
@@ -98,14 +108,18 @@ var ReactStore = /*#__PURE__*/function (_ObserverStore) {
|
|
|
98
108
|
_createClass(ReactStore, [{
|
|
99
109
|
key: "findAll",
|
|
100
110
|
value: function findAll(type, stateAttribute, context, filterFunction) {
|
|
111
|
+
_classPrivateMethodGet(this, _fixState, _fixState2).call(this, stateAttribute, context);
|
|
112
|
+
|
|
101
113
|
var subKey = this.subscribe(type, function (data) {
|
|
102
|
-
context.setState(_objectSpread(_objectSpread({}, context.state), {}, _defineProperty({}, stateAttribute, data)));
|
|
114
|
+
context.setState(_objectSpread(_objectSpread({}, context.state), {}, _defineProperty({}, stateAttribute, data || [])));
|
|
103
115
|
}, filterFunction);
|
|
104
116
|
addSubscriptionToContext(context, subKey);
|
|
105
117
|
}
|
|
106
118
|
}, {
|
|
107
119
|
key: "findOne",
|
|
108
120
|
value: function findOne(type, stateAttribute, context, filterFunction) {
|
|
121
|
+
_classPrivateMethodGet(this, _fixState, _fixState2).call(this, stateAttribute, context);
|
|
122
|
+
|
|
109
123
|
var subKey = this.subscribe(type, function (data) {
|
|
110
124
|
context.setState(_objectSpread(_objectSpread({}, context.state), {}, _defineProperty({}, stateAttribute, data && data.length ? data[0] : null)));
|
|
111
125
|
}, filterFunction);
|
|
@@ -116,4 +130,10 @@ var ReactStore = /*#__PURE__*/function (_ObserverStore) {
|
|
|
116
130
|
return ReactStore;
|
|
117
131
|
}(_ObserverStore2["default"]);
|
|
118
132
|
|
|
119
|
-
exports["default"] = ReactStore;
|
|
133
|
+
exports["default"] = ReactStore;
|
|
134
|
+
|
|
135
|
+
function _fixState2(stateAttribute, context) {
|
|
136
|
+
if (!context[stateAttribute]) {
|
|
137
|
+
context[stateAttribute] = []; // side effect on state
|
|
138
|
+
}
|
|
139
|
+
}
|
package/dist/Store.js
CHANGED
|
@@ -7,202 +7,346 @@ exports["default"] = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _StoreObject = _interopRequireDefault(require("./StoreObject"));
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _PubSub = _interopRequireDefault(require("./PubSub"));
|
|
11
11
|
|
|
12
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
13
|
|
|
14
|
-
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); }
|
|
15
|
-
|
|
16
14
|
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; } } }; }
|
|
17
15
|
|
|
18
16
|
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); }
|
|
19
17
|
|
|
20
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; }
|
|
21
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
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
23
|
+
|
|
22
24
|
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); } }
|
|
23
25
|
|
|
24
26
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
25
27
|
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
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; }
|
|
28
|
+
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
var _this = this;
|
|
30
|
+
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
|
|
34
33
|
|
|
35
|
-
|
|
34
|
+
var _deleteByObject = /*#__PURE__*/new WeakSet();
|
|
36
35
|
|
|
37
|
-
|
|
38
|
-
var type = model.getType();
|
|
36
|
+
var _deleteByFilter = /*#__PURE__*/new WeakSet();
|
|
39
37
|
|
|
40
|
-
|
|
41
|
-
throw new Error("Not valid model object: type missing");
|
|
42
|
-
}
|
|
43
|
-
});
|
|
38
|
+
var _getPromise = /*#__PURE__*/new WeakSet();
|
|
44
39
|
|
|
45
|
-
|
|
46
|
-
_this.validateModel(model);
|
|
40
|
+
var _insertObject = /*#__PURE__*/new WeakSet();
|
|
47
41
|
|
|
48
|
-
|
|
42
|
+
var _loadObjects = /*#__PURE__*/new WeakSet();
|
|
49
43
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
storedObjects: {}
|
|
54
|
-
};
|
|
55
|
-
model.store = _this;
|
|
44
|
+
var Store = /*#__PURE__*/function () {
|
|
45
|
+
function Store() {
|
|
46
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
_this._loadObjects(type);
|
|
59
|
-
}
|
|
60
|
-
} else {
|
|
61
|
-
throw new Error("The model already exists");
|
|
62
|
-
}
|
|
63
|
-
});
|
|
48
|
+
_classCallCheck(this, Store);
|
|
64
49
|
|
|
65
|
-
|
|
66
|
-
if (!_this.models[type]) {
|
|
67
|
-
return Promise.reject("The model doesn't exist");
|
|
68
|
-
} else if (!_this.models[type].promise && !_this.options.lazyLoad) {
|
|
69
|
-
return Promise.reject("The model is not loaded");
|
|
70
|
-
} else if (!_this.models[type].promise && _this.options.lazyLoad) {
|
|
71
|
-
return _this._loadObjects(type).then(function () {
|
|
72
|
-
return _this.models[type].promise;
|
|
73
|
-
});
|
|
74
|
-
} else {
|
|
75
|
-
return _this.models[type].promise;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
50
|
+
_classPrivateMethodInitSpec(this, _loadObjects);
|
|
78
51
|
|
|
79
|
-
|
|
80
|
-
var markAsNew = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
81
|
-
var model = _this.models[type].model;
|
|
82
|
-
var wrapper = new _StoreObject["default"](item, model);
|
|
83
|
-
var id = wrapper.getId();
|
|
52
|
+
_classPrivateMethodInitSpec(this, _insertObject);
|
|
84
53
|
|
|
85
|
-
|
|
86
|
-
throw new Error("The IDs provided for the model ".concat(type, " are not unique"));
|
|
87
|
-
}
|
|
54
|
+
_classPrivateMethodInitSpec(this, _getPromise);
|
|
88
55
|
|
|
89
|
-
|
|
90
|
-
id: id,
|
|
91
|
-
fingerprint: wrapper.getFingerprint(),
|
|
92
|
-
object: wrapper,
|
|
93
|
-
status: markAsNew ? "new" : "old"
|
|
94
|
-
};
|
|
95
|
-
return wrapper;
|
|
96
|
-
});
|
|
56
|
+
_classPrivateMethodInitSpec(this, _deleteByFilter);
|
|
97
57
|
|
|
98
|
-
|
|
99
|
-
var item = _this.models[type];
|
|
100
|
-
return item.promise = item.model.retrieveAll().then(function (items) {
|
|
101
|
-
var _iterator = _createForOfIteratorHelper(items),
|
|
102
|
-
_step;
|
|
58
|
+
_classPrivateMethodInitSpec(this, _deleteByObject);
|
|
103
59
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
60
|
+
this.options = {
|
|
61
|
+
autoSave: options.autoSave !== undefined ? options.autoSave : 5000,
|
|
62
|
+
saveDelay: options.saveDelay || 1000,
|
|
63
|
+
lazyLoad: options.lazyLoad || false
|
|
64
|
+
};
|
|
65
|
+
this.models = {};
|
|
66
|
+
this.pubSub = new _PubSub["default"]();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
_createClass(Store, [{
|
|
70
|
+
key: "on",
|
|
71
|
+
value: function on(channel, callback) {
|
|
72
|
+
this.pubSub.subscribe(channel, callback);
|
|
73
|
+
}
|
|
74
|
+
}, {
|
|
75
|
+
key: "validateModel",
|
|
76
|
+
value: function validateModel(model) {
|
|
77
|
+
var type = model.getType();
|
|
107
78
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
} catch (err) {
|
|
111
|
-
_iterator.e(err);
|
|
112
|
-
} finally {
|
|
113
|
-
_iterator.f();
|
|
79
|
+
if (typeof type !== "string" || type === "") {
|
|
80
|
+
throw new Error("Not valid model object: type missing");
|
|
114
81
|
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
82
|
+
}
|
|
83
|
+
}, {
|
|
84
|
+
key: "addModel",
|
|
85
|
+
value: function addModel(model) {
|
|
86
|
+
var _this = this;
|
|
87
|
+
|
|
88
|
+
return new Promise(function (resolve, reject) {
|
|
89
|
+
_this.validateModel(model);
|
|
90
|
+
|
|
91
|
+
var type = model.getType();
|
|
92
|
+
|
|
93
|
+
if (!_this.models[type]) {
|
|
94
|
+
_this.models[type] = {
|
|
95
|
+
model: model,
|
|
96
|
+
storedObjects: {}
|
|
97
|
+
};
|
|
98
|
+
model.setStore(_this);
|
|
99
|
+
|
|
100
|
+
if (!_this.options.lazyLoad) {
|
|
101
|
+
resolve(_classPrivateMethodGet(_this, _loadObjects, _loadObjects2).call(_this, type));
|
|
102
|
+
} else {
|
|
103
|
+
resolve();
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
reject("The model already exists");
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}, {
|
|
111
|
+
key: "update",
|
|
112
|
+
value: function update(objects) {
|
|
113
|
+
return Promise.resolve(); // Nothing to do at this level
|
|
114
|
+
}
|
|
115
|
+
}, {
|
|
116
|
+
key: "delete",
|
|
117
|
+
value: function _delete(typeOrObjects, filterFunction) {
|
|
118
|
+
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)));
|
|
122
|
+
} else {
|
|
123
|
+
return Promise.reject("Invalid delete request. You have to provide a list of objects or a type and a filter function");
|
|
124
124
|
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
125
|
+
}
|
|
126
|
+
}, {
|
|
127
|
+
key: "insert",
|
|
128
|
+
value: function insert(type, objects) {
|
|
129
|
+
var _this2 = this;
|
|
130
|
+
|
|
131
|
+
return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
|
|
132
|
+
return objects.map(function (object) {
|
|
133
|
+
return _classPrivateMethodGet(_this2, _insertObject, _insertObject2).call(_this2, type, object, true);
|
|
134
|
+
});
|
|
134
135
|
});
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return (
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
136
|
+
}
|
|
137
|
+
}, {
|
|
138
|
+
key: "get",
|
|
139
|
+
value: function get(type, id) {
|
|
140
|
+
var _this3 = this;
|
|
141
|
+
|
|
142
|
+
return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
|
|
143
|
+
try {
|
|
144
|
+
return _this3.models[type].storedObjects[id].object;
|
|
145
|
+
} catch (error) {
|
|
146
|
+
return Promise.reject("Object not found");
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}, {
|
|
151
|
+
key: "find",
|
|
152
|
+
value: function find(type, filterFunction) {
|
|
153
|
+
var _this4 = this;
|
|
154
|
+
|
|
155
|
+
return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
|
|
156
|
+
var all = Object.values(_this4.models[type].storedObjects).filter(function (i) {
|
|
157
|
+
return i.status !== "deleted";
|
|
158
|
+
}).map(function (i) {
|
|
159
|
+
return i.object;
|
|
160
|
+
});
|
|
161
|
+
return filterFunction ? all.filter(filterFunction) : all;
|
|
162
|
+
})["catch"](function (error) {
|
|
163
|
+
_this4.pubSub.publish("error", error);
|
|
164
|
+
|
|
165
|
+
return Promise.reject(error);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}, {
|
|
169
|
+
key: "applyDiff",
|
|
170
|
+
value: function applyDiff(_ref, type) {
|
|
171
|
+
var _this5 = this;
|
|
172
|
+
|
|
173
|
+
var _ref$inserted = _ref.inserted,
|
|
174
|
+
inserted = _ref$inserted === void 0 ? [] : _ref$inserted,
|
|
175
|
+
_ref$updated = _ref.updated,
|
|
176
|
+
updated = _ref$updated === void 0 ? [] : _ref$updated,
|
|
177
|
+
_ref$deleted = _ref.deleted,
|
|
178
|
+
deleted = _ref$deleted === void 0 ? [] : _ref$deleted;
|
|
179
|
+
return new Promise(function (resolve, reject) {
|
|
180
|
+
try {
|
|
181
|
+
var _iterator = _createForOfIteratorHelper(inserted.concat(updated)),
|
|
182
|
+
_step;
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
186
|
+
var object = _step.value;
|
|
187
|
+
|
|
188
|
+
var item = _this5.models[type || object.object.getModel().getType()].storedObjects[object.id];
|
|
189
|
+
|
|
190
|
+
item.fingerprint = object.object.getFingerprint();
|
|
191
|
+
item.status = "old";
|
|
192
|
+
}
|
|
193
|
+
} catch (err) {
|
|
194
|
+
_iterator.e(err);
|
|
195
|
+
} finally {
|
|
196
|
+
_iterator.f();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
var _iterator2 = _createForOfIteratorHelper(deleted),
|
|
200
|
+
_step2;
|
|
201
|
+
|
|
202
|
+
try {
|
|
203
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
204
|
+
var _object = _step2.value;
|
|
205
|
+
delete _this5.models[type || _object.object.getModel().getType()].storedObjects[_object.id];
|
|
206
|
+
}
|
|
207
|
+
} catch (err) {
|
|
208
|
+
_iterator2.e(err);
|
|
209
|
+
} finally {
|
|
210
|
+
_iterator2.f();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
resolve();
|
|
214
|
+
} catch (error) {
|
|
215
|
+
return reject(error);
|
|
216
|
+
}
|
|
149
217
|
});
|
|
218
|
+
}
|
|
219
|
+
}, {
|
|
220
|
+
key: "getDiff",
|
|
221
|
+
value: function getDiff(type) {
|
|
222
|
+
var _this6 = this;
|
|
223
|
+
|
|
224
|
+
return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
|
|
225
|
+
var objects = Object.values(_this6.models[type].storedObjects);
|
|
226
|
+
var inserted = [];
|
|
227
|
+
var updated = [];
|
|
228
|
+
var deleted = [];
|
|
229
|
+
|
|
230
|
+
for (var _i = 0, _objects = objects; _i < _objects.length; _i++) {
|
|
231
|
+
var object = _objects[_i];
|
|
232
|
+
|
|
233
|
+
if (object.status === "new") {
|
|
234
|
+
inserted.push(object);
|
|
235
|
+
} else if (object.status === "deleted") {
|
|
236
|
+
deleted.push(object);
|
|
237
|
+
} else if (object.fingerprint !== object.object.getFingerprint()) {
|
|
238
|
+
updated.push(object);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
150
241
|
|
|
151
|
-
|
|
152
|
-
|
|
242
|
+
return {
|
|
243
|
+
inserted: inserted,
|
|
244
|
+
updated: updated,
|
|
245
|
+
deleted: deleted
|
|
246
|
+
};
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}]);
|
|
153
250
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
var object = _step2.value;
|
|
157
|
-
object.status = "deleted";
|
|
158
|
-
}
|
|
159
|
-
} catch (err) {
|
|
160
|
-
_iterator2.e(err);
|
|
161
|
-
} finally {
|
|
162
|
-
_iterator2.f();
|
|
163
|
-
}
|
|
251
|
+
return Store;
|
|
252
|
+
}();
|
|
164
253
|
|
|
165
|
-
|
|
166
|
-
return i.object;
|
|
167
|
-
}));
|
|
254
|
+
exports["default"] = Store;
|
|
168
255
|
|
|
169
|
-
|
|
170
|
-
|
|
256
|
+
function _deleteByObject2(object) {
|
|
257
|
+
return _classPrivateMethodGet(this, _deleteByFilter, _deleteByFilter2).call(this, object.getType(), function (item) {
|
|
258
|
+
return object.getId() === item.getId();
|
|
171
259
|
});
|
|
260
|
+
}
|
|
172
261
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return _this._deleteByFilter(typeOrObjects, filterFunction);
|
|
176
|
-
} else if (_typeof(typeOrObjects) === "object" && typeOrObjects.length) {
|
|
177
|
-
return Promise.all(typeOrObjects.map(_this._deleteByObject));
|
|
178
|
-
} else {
|
|
179
|
-
throw new Error("Invalid delete request. You have to provide a list of objects or a type and a filter function");
|
|
180
|
-
}
|
|
181
|
-
});
|
|
262
|
+
function _deleteByFilter2(type, filterFunction) {
|
|
263
|
+
var _this7 = this;
|
|
182
264
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
265
|
+
return _classPrivateMethodGet(this, _getPromise, _getPromise2).call(this, type).then(function () {
|
|
266
|
+
var deleted = Object.values(_this7.models[type].storedObjects).filter(function (i) {
|
|
267
|
+
return filterFunction(i.object);
|
|
268
|
+
});
|
|
186
269
|
|
|
187
|
-
|
|
270
|
+
var _iterator3 = _createForOfIteratorHelper(deleted),
|
|
271
|
+
_step3;
|
|
188
272
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
273
|
+
try {
|
|
274
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
275
|
+
var object = _step3.value;
|
|
276
|
+
object.status = "deleted";
|
|
277
|
+
}
|
|
278
|
+
} catch (err) {
|
|
279
|
+
_iterator3.e(err);
|
|
280
|
+
} finally {
|
|
281
|
+
_iterator3.f();
|
|
282
|
+
}
|
|
192
283
|
|
|
193
|
-
|
|
194
|
-
_this.trigger("update", objects);
|
|
284
|
+
return true;
|
|
195
285
|
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function _getPromise2(type) {
|
|
289
|
+
var _this8 = this;
|
|
290
|
+
|
|
291
|
+
if (!this.models[type]) {
|
|
292
|
+
return Promise.reject("The model doesn't exist");
|
|
293
|
+
} else if (!this.models[type].promise && !this.options.lazyLoad) {
|
|
294
|
+
return Promise.reject("The model is not loaded");
|
|
295
|
+
} else if (!this.models[type].promise && this.options.lazyLoad) {
|
|
296
|
+
return _classPrivateMethodGet(this, _loadObjects, _loadObjects2).call(this, type).then(function () {
|
|
297
|
+
return _this8.models[type].promise;
|
|
298
|
+
});
|
|
299
|
+
} else {
|
|
300
|
+
return this.models[type].promise;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function _insertObject2(type, item) {
|
|
305
|
+
var markAsNew = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
306
|
+
var model = this.models[type].model;
|
|
307
|
+
var wrapper = new _StoreObject["default"](item, model);
|
|
308
|
+
var id = wrapper.getId();
|
|
309
|
+
|
|
310
|
+
if (this.models[type].storedObjects[id]) {
|
|
311
|
+
throw new Error("The IDs provided for the model ".concat(type, " are not unique"));
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
this.models[type].storedObjects[id] = {
|
|
315
|
+
id: id,
|
|
316
|
+
fingerprint: wrapper.getFingerprint(),
|
|
317
|
+
object: wrapper,
|
|
318
|
+
status: markAsNew ? "new" : "old"
|
|
319
|
+
};
|
|
320
|
+
return wrapper;
|
|
321
|
+
}
|
|
196
322
|
|
|
197
|
-
|
|
323
|
+
function _loadObjects2(type) {
|
|
324
|
+
var _this9 = this;
|
|
325
|
+
|
|
326
|
+
var item = this.models[type];
|
|
327
|
+
this.pubSub.publish("loading", {
|
|
328
|
+
status: "start",
|
|
329
|
+
model: type
|
|
198
330
|
});
|
|
331
|
+
return item.promise = item.model.retrieveAll().then(function (items) {
|
|
332
|
+
var _iterator4 = _createForOfIteratorHelper(items),
|
|
333
|
+
_step4;
|
|
199
334
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
})
|
|
335
|
+
try {
|
|
336
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
337
|
+
var _item = _step4.value;
|
|
338
|
+
|
|
339
|
+
_classPrivateMethodGet(_this9, _insertObject, _insertObject2).call(_this9, type, _item, false);
|
|
340
|
+
}
|
|
341
|
+
} catch (err) {
|
|
342
|
+
_iterator4.e(err);
|
|
343
|
+
} finally {
|
|
344
|
+
_iterator4.f();
|
|
345
|
+
}
|
|
207
346
|
|
|
208
|
-
|
|
347
|
+
_this9.pubSub.publish("loading", {
|
|
348
|
+
status: "end",
|
|
349
|
+
model: type
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
}
|
package/dist/StoreObject.js
CHANGED
|
@@ -19,11 +19,44 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
|
|
19
19
|
|
|
20
20
|
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; }
|
|
21
21
|
|
|
22
|
+
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
23
|
+
|
|
24
|
+
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
25
|
+
|
|
26
|
+
function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
|
|
27
|
+
|
|
28
|
+
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; } }
|
|
29
|
+
|
|
30
|
+
function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
|
|
31
|
+
|
|
32
|
+
function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
|
|
33
|
+
|
|
34
|
+
function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
|
|
35
|
+
|
|
36
|
+
var _loaded = /*#__PURE__*/new WeakMap();
|
|
37
|
+
|
|
22
38
|
var Obj = /*#__PURE__*/_createClass(function Obj(values, model) {
|
|
23
39
|
var _this = this;
|
|
24
40
|
|
|
25
41
|
_classCallCheck(this, Obj);
|
|
26
42
|
|
|
43
|
+
_classPrivateFieldInitSpec(this, _loaded, {
|
|
44
|
+
writable: true,
|
|
45
|
+
value: false
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
_defineProperty(this, "load", function () {
|
|
49
|
+
if (_classPrivateFieldGet(_this, _loaded)) {
|
|
50
|
+
return Promise.resolve(_this);
|
|
51
|
+
} else {
|
|
52
|
+
return _this.getModel().load(_this).then(function () {
|
|
53
|
+
_classPrivateFieldSet(_this, _loaded, true);
|
|
54
|
+
|
|
55
|
+
return _this;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
27
60
|
_defineProperty(this, "getFingerprint", function () {
|
|
28
61
|
return (0, _fingerprint["default"])(_this.toJson());
|
|
29
62
|
});
|