dataflux 1.4.2 → 1.5.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 CHANGED
@@ -178,7 +178,7 @@ const subscriptions = [
178
178
  ["author"], // No filter function, all objects returned
179
179
  ];
180
180
 
181
- const callback = ([books, authors]) => {
181
+ const callback = ({book, author}) => {
182
182
  // Objects are ready
183
183
  };
184
184
 
@@ -275,6 +275,7 @@ All the possible options for a model creation are (they are all optional):
275
275
  | axios | It allows to specify an axios instance to be used for the queries. If not specified, a new one will be used. | A new axios instance |
276
276
  | parseMoment | Automatically creates Moment.js objects out of ISO8601 strings. E.g., if an object has a property `createdAt: "2022-01-07T21:38:50.295Z"`, this will be transformed to a moment object. | |
277
277
  | hiddenFields | An array of attribute names that will never be sent back to the API. E.g., if you set `hiddenFields: ["pages"]`, a book object can contain an attribute `pages` locally, but this will be stripped out in PUT/POST requests. |
278
+ | deep | A boolean defining if nested objects should be enriched with the object methods. | true |
278
279
 
279
280
  ### Operations
280
281
  As described in the table above, there are four possible operations: **retrieve, insert, update,** and **delete**. An operation can be defined as an operation object or a function.
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.dateRegex = exports.BasicObj = void 0;
7
+
8
+ var _moment = _interopRequireDefault(require("moment/moment"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
+
12
+ 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); } }
13
+
14
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
15
+
16
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17
+
18
+ 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; }
19
+
20
+ function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
21
+
22
+ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
23
+
24
+ function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
25
+
26
+ function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
27
+
28
+ function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
29
+
30
+ var dateRegex = new RegExp("^[0-9][0-9][0-9][0-9]-[0-9].*T[0-9].*Z$");
31
+ exports.dateRegex = dateRegex;
32
+
33
+ var _setHidden = /*#__PURE__*/new WeakMap();
34
+
35
+ var BasicObj = /*#__PURE__*/_createClass(function BasicObj(values, model) {
36
+ var _this = this;
37
+
38
+ _classCallCheck(this, BasicObj);
39
+
40
+ _classPrivateFieldInitSpec(this, _setHidden, {
41
+ writable: true,
42
+ value: {}
43
+ });
44
+
45
+ _defineProperty(this, "get", function (attribute, defaultValue) {
46
+ var _ref, _classPrivateFieldGet2;
47
+
48
+ return (_ref = (_classPrivateFieldGet2 = _classPrivateFieldGet(_this, _setHidden)[attribute]) !== null && _classPrivateFieldGet2 !== void 0 ? _classPrivateFieldGet2 : _this[attribute]) !== null && _ref !== void 0 ? _ref : defaultValue;
49
+ });
50
+
51
+ _defineProperty(this, "set", function (attribute, value, hidden) {
52
+ if (hidden) {
53
+ _classPrivateFieldGet(_this, _setHidden)[attribute] = value;
54
+ } else {
55
+ if (attribute === "id") {
56
+ throw new Error("You cannot change the ID");
57
+ }
58
+
59
+ _this[attribute] = value;
60
+ }
61
+
62
+ return _this.update();
63
+ });
64
+
65
+ _defineProperty(this, "setConstant", function (attribute, value) {
66
+ var _classPrivateFieldGet3;
67
+
68
+ _classPrivateFieldGet(_this, _setHidden)[attribute] = (_classPrivateFieldGet3 = _classPrivateFieldGet(_this, _setHidden)[attribute]) !== null && _classPrivateFieldGet3 !== void 0 ? _classPrivateFieldGet3 : value;
69
+ });
70
+
71
+ _defineProperty(this, "toJSON", function () {
72
+ var attrs = Object.keys(_this);
73
+ var out = {};
74
+
75
+ for (var _i = 0, _attrs = attrs; _i < _attrs.length; _i++) {
76
+ var a = _attrs[_i];
77
+
78
+ if (_this[a] instanceof _moment["default"]) {
79
+ out[a] = _this[a].toISOString();
80
+ } else if (_this[a] instanceof Date) {
81
+ out[a] = (0, _moment["default"])(_this[a]).toISOString();
82
+ } else if (_this[a].toJSON) {
83
+ out[a] = _this[a].toJSON();
84
+ } else if (Array.isArray(_this[a]) && _this[a].every(function (i) {
85
+ return i.toJSON;
86
+ })) {
87
+ out[a] = _this[a].map(function (i) {
88
+ return i.toJSON();
89
+ });
90
+ } else if (typeof _this[a] !== "function") {
91
+ out[a] = _this[a];
92
+ }
93
+ }
94
+
95
+ return out;
96
+ });
97
+
98
+ _defineProperty(this, "toString", function () {
99
+ return JSON.stringify(_this.toJSON());
100
+ });
101
+
102
+ _defineProperty(this, "update", function () {
103
+ return Promise.resolve();
104
+ });
105
+ });
106
+
107
+ exports.BasicObj = BasicObj;
package/dist/Model.js CHANGED
@@ -110,7 +110,9 @@ var _updateObjects = /*#__PURE__*/new WeakMap();
110
110
  var _deleteObjects = /*#__PURE__*/new WeakMap();
111
111
 
112
112
  var Model = /*#__PURE__*/_createClass(function Model(name) {
113
- var _this = this;
113
+ var _this = this,
114
+ _options$deep,
115
+ _options$parseMoment;
114
116
 
115
117
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
116
118
 
@@ -375,7 +377,8 @@ var Model = /*#__PURE__*/_createClass(function Model(name) {
375
377
  _classPrivateFieldSet(this, _type, name);
376
378
 
377
379
  this.options = {
378
- parseMoment: options.parseMoment
380
+ deep: (_options$deep = options.deep) !== null && _options$deep !== void 0 ? _options$deep : true,
381
+ parseMoment: (_options$parseMoment = options.parseMoment) !== null && _options$parseMoment !== void 0 ? _options$parseMoment : false
379
382
  };
380
383
 
381
384
  _classPrivateFieldSet(this, _store, null);
package/dist/Obj.js CHANGED
@@ -9,16 +9,36 @@ var _fingerprint = _interopRequireDefault(require("./fingerprint"));
9
9
 
10
10
  var _uuid = require("uuid");
11
11
 
12
+ var _BasicObj2 = require("./BasicObj");
13
+
12
14
  var _moment = _interopRequireDefault(require("moment/moment"));
13
15
 
16
+ var _SubObj = _interopRequireDefault(require("./SubObj"));
17
+
14
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
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
+
16
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); } }
17
23
 
18
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; }
19
25
 
20
26
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
21
27
 
28
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
29
+
30
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
31
+
32
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
33
+
34
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
35
+
36
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
37
+
38
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
39
+
40
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
41
+
22
42
  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; }
23
43
 
24
44
  function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
@@ -35,134 +55,98 @@ function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!priva
35
55
 
36
56
  function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
37
57
 
38
- var dateRegex = new RegExp("^[0-9][0-9][0-9][0-9]-[0-9].*T[0-9].*Z$");
39
-
40
58
  var _loaded = /*#__PURE__*/new WeakMap();
41
59
 
42
- var _setHidden = /*#__PURE__*/new WeakMap();
43
-
44
- var Obj = /*#__PURE__*/_createClass(function Obj(values, _model) {
45
- var _this = this;
46
-
47
- _classCallCheck(this, Obj);
60
+ var Obj = /*#__PURE__*/function (_BasicObj) {
61
+ _inherits(Obj, _BasicObj);
48
62
 
49
- _classPrivateFieldInitSpec(this, _loaded, {
50
- writable: true,
51
- value: false
52
- });
63
+ var _super = _createSuper(Obj);
53
64
 
54
- _classPrivateFieldInitSpec(this, _setHidden, {
55
- writable: true,
56
- value: {}
57
- });
65
+ function Obj(values, _model) {
66
+ var _this;
58
67
 
59
- _defineProperty(this, "load", function () {
60
- if (_classPrivateFieldGet(_this, _loaded)) {
61
- return Promise.resolve(_this);
62
- } else {
63
- var model = _this.getModel();
64
-
65
- return model.load(_this).then(function () {
66
- _classPrivateFieldSet(_this, _loaded, true);
67
-
68
- return model.getStore().update([_this], true); // Propagate update
69
- }).then(function () {
70
- return _this;
71
- }); // return always this
72
- }
73
- });
68
+ _classCallCheck(this, Obj);
74
69
 
75
- _defineProperty(this, "getFingerprint", function () {
76
- return (0, _fingerprint["default"])(_this.toJSON());
77
- });
70
+ _this = _super.call(this, values, _model);
78
71
 
79
- _defineProperty(this, "get", function (attribute, defaultValue) {
80
- var _ref, _classPrivateFieldGet2;
72
+ _classPrivateFieldInitSpec(_assertThisInitialized(_this), _loaded, {
73
+ writable: true,
74
+ value: false
75
+ });
81
76
 
82
- return (_ref = (_classPrivateFieldGet2 = _classPrivateFieldGet(_this, _setHidden)[attribute]) !== null && _classPrivateFieldGet2 !== void 0 ? _classPrivateFieldGet2 : _this[attribute]) !== null && _ref !== void 0 ? _ref : defaultValue;
83
- });
77
+ _defineProperty(_assertThisInitialized(_this), "load", function () {
78
+ if (_classPrivateFieldGet(_assertThisInitialized(_this), _loaded)) {
79
+ return Promise.resolve(_assertThisInitialized(_this));
80
+ } else {
81
+ var model = _this.getModel();
84
82
 
85
- _defineProperty(this, "getRelation", function (type, filterFunction) {
86
- return _this.getModel().getRelation(_this, type, filterFunction);
87
- });
83
+ return model.load(_assertThisInitialized(_this)).then(function () {
84
+ _classPrivateFieldSet(_assertThisInitialized(_this), _loaded, true);
88
85
 
89
- _defineProperty(this, "set", function (attribute, value, hidden) {
90
- if (hidden) {
91
- _classPrivateFieldGet(_this, _setHidden)[attribute] = value;
92
- } else {
93
- if (attribute === "id") {
94
- throw new Error("You cannot change the ID");
86
+ return model.getStore().update([_assertThisInitialized(_this)], true); // Propagate update
87
+ }).then(function () {
88
+ return _assertThisInitialized(_this);
89
+ }); // return always this
95
90
  }
96
-
97
- _this[attribute] = value;
98
- }
99
-
100
- return _this.getModel().getStore().update([_this]);
101
- });
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
-
109
- _defineProperty(this, "save", function () {
110
- return _this.getModel().getStore().save([_this]);
111
- });
112
-
113
- _defineProperty(this, "destroy", function () {
114
- return _this.getModel().getStore()["delete"]([_this]);
115
- });
116
-
117
- _defineProperty(this, "toJSON", function () {
118
- var attrs = Object.keys(_this);
119
- var out = {};
120
-
121
- for (var _i = 0, _attrs = attrs; _i < _attrs.length; _i++) {
122
- var a = _attrs[_i];
123
-
124
- if (_this[a] instanceof _moment["default"]) {
125
- out[a] = _this[a].toISOString();
126
- } else if (_this[a] instanceof Date) {
127
- out[a] = (0, _moment["default"])(_this[a]).toISOString();
128
- } else if (typeof _this[a] !== "function") {
129
- out[a] = _this[a];
91
+ });
92
+
93
+ _defineProperty(_assertThisInitialized(_this), "getFingerprint", function () {
94
+ return (0, _fingerprint["default"])(_this.toJSON());
95
+ });
96
+
97
+ _defineProperty(_assertThisInitialized(_this), "getRelation", function (type, filterFunction) {
98
+ return _this.getModel().getRelation(_assertThisInitialized(_this), type, filterFunction);
99
+ });
100
+
101
+ _defineProperty(_assertThisInitialized(_this), "save", function () {
102
+ return _this.getModel().getStore().save([_assertThisInitialized(_this)]);
103
+ });
104
+
105
+ _defineProperty(_assertThisInitialized(_this), "destroy", function () {
106
+ return _this.getModel().getStore()["delete"]([_assertThisInitialized(_this)]);
107
+ });
108
+
109
+ _defineProperty(_assertThisInitialized(_this), "update", function () {
110
+ return _this.getModel().getStore().update([_assertThisInitialized(_this)]);
111
+ });
112
+
113
+ Object.keys(values).forEach(function (key) {
114
+ var value = values[key];
115
+
116
+ if (_model.options.parseMoment && _BasicObj2.dateRegex.test(value)) {
117
+ var mmnt = (0, _moment["default"])(value);
118
+ _this[key] = mmnt.isValid() ? mmnt : value;
119
+ } else if (_model.options.deep && _typeof(value) === "object" && !Array.isArray(value)) {
120
+ _this[key] = new _SubObj["default"](_assertThisInitialized(_this), value, _model);
121
+ } else if (_model.options.deep && Array.isArray(value)) {
122
+ _this[key] = value.map(function (i) {
123
+ return new _SubObj["default"](_assertThisInitialized(_this), i, _model);
124
+ });
125
+ } else {
126
+ _this[key] = value;
130
127
  }
131
- }
128
+ });
132
129
 
133
- return out;
134
- });
130
+ _this.getModel = function () {
131
+ return _model;
132
+ };
135
133
 
136
- _defineProperty(this, "toString", function () {
137
- return JSON.stringify(_this.toJSON());
138
- });
134
+ var id;
139
135
 
140
- this.getModel = function () {
141
- return _model;
142
- };
143
-
144
- var frEach = _model.options.parseMoment ? function (key) {
145
- if (dateRegex.test(values[key])) {
146
- var mmnt = (0, _moment["default"])(values[key]);
147
- _this[key] = mmnt.isValid() ? mmnt : values[key];
136
+ if (_this.id && (typeof _this.id === "string" || typeof _this.id === "number")) {
137
+ id = _this.id.toString();
148
138
  } else {
149
- _this[key] = values[key];
139
+ id = (0, _uuid.v4)();
150
140
  }
151
- } : function (key) {
152
- return _this[key] = values[key];
153
- };
154
- Object.keys(values).forEach(frEach);
155
- var id;
156
-
157
- if (this.id && (typeof this.id === "string" || typeof this.id === "number")) {
158
- id = this.id.toString();
159
- } else {
160
- id = (0, _uuid.v4)();
141
+
142
+ _this.getId = function () {
143
+ return id;
144
+ };
145
+
146
+ return _this;
161
147
  }
162
148
 
163
- this.getId = function () {
164
- return id;
165
- };
166
- });
149
+ return _createClass(Obj);
150
+ }(_BasicObj2.BasicObj);
167
151
 
168
152
  exports["default"] = Obj;
@@ -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(_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; } } }; }
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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
20
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
21
21
 
22
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
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 _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 _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; }
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
- return Promise.all(subscriptions.map(function (sub, index) {
96
- var attrs = Array.from(Array(index + 1)).map(function () {
97
- return null;
98
- });
99
- return _this.subscribe(sub[0], sub[1], function (data) {
100
- attrs[index] = data;
101
- return callback.apply(void 0, _toConsumableArray(attrs));
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 (_ref) {
213
- var callback = _ref.callback,
214
- filterFunction = _ref.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 _i = 0, _objects = objects; _i < _objects.length; _i++) {
288
- var object = _objects[_i];
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 (_ref2) {
310
- var callback = _ref2.callback,
311
- filterFunction = _ref2.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/dist/SubObj.js ADDED
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = void 0;
7
+
8
+ var _uuid = require("uuid");
9
+
10
+ var _BasicObj2 = require("./BasicObj");
11
+
12
+ var _moment = _interopRequireDefault(require("moment/moment"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
+
16
+ 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); }
17
+
18
+ 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); } }
19
+
20
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
21
+
22
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
23
+
24
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
25
+
26
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
27
+
28
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
29
+
30
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
31
+
32
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
33
+
34
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
35
+
36
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
37
+
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; }
39
+
40
+ function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
41
+
42
+ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
43
+
44
+ function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
45
+
46
+ function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
47
+
48
+ function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
49
+
50
+ function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
51
+
52
+ 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; } }
53
+
54
+ var _model = /*#__PURE__*/new WeakMap();
55
+
56
+ var _parent = /*#__PURE__*/new WeakMap();
57
+
58
+ var SubObj = /*#__PURE__*/function (_BasicObj) {
59
+ _inherits(SubObj, _BasicObj);
60
+
61
+ var _super = _createSuper(SubObj);
62
+
63
+ function SubObj(parent, values, model) {
64
+ var _this;
65
+
66
+ _classCallCheck(this, SubObj);
67
+
68
+ _this = _super.call(this, values, model);
69
+
70
+ _classPrivateFieldInitSpec(_assertThisInitialized(_this), _model, {
71
+ writable: true,
72
+ value: void 0
73
+ });
74
+
75
+ _classPrivateFieldInitSpec(_assertThisInitialized(_this), _parent, {
76
+ writable: true,
77
+ value: void 0
78
+ });
79
+
80
+ _defineProperty(_assertThisInitialized(_this), "save", function () {
81
+ return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore().save([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
82
+ });
83
+
84
+ _defineProperty(_assertThisInitialized(_this), "destroy", function () {
85
+ return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore()["delete"]([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
86
+ });
87
+
88
+ _defineProperty(_assertThisInitialized(_this), "update", function () {
89
+ return _classPrivateFieldGet(_assertThisInitialized(_this), _model).getStore().update([_classPrivateFieldGet(_assertThisInitialized(_this), _parent)]);
90
+ });
91
+
92
+ _classPrivateFieldSet(_assertThisInitialized(_this), _model, model);
93
+
94
+ _classPrivateFieldSet(_assertThisInitialized(_this), _parent, parent);
95
+
96
+ Object.keys(values).forEach(function (key) {
97
+ var value = values[key];
98
+
99
+ if (model.options.parseMoment && _BasicObj2.dateRegex.test(value)) {
100
+ var mmnt = (0, _moment["default"])(value);
101
+ _this[key] = mmnt.isValid() ? mmnt : value;
102
+ } else if (model.options.deep && _typeof(value) === "object" && !Array.isArray(value)) {
103
+ _this[key] = new SubObj(_assertThisInitialized(_this), value, model);
104
+ } else if (model.options.deep && Array.isArray(value)) {
105
+ _this[key] = value.map(function (i) {
106
+ return new SubObj(_assertThisInitialized(_this), i, model);
107
+ });
108
+ } else {
109
+ _this[key] = value;
110
+ }
111
+ });
112
+
113
+ _this.getId = function () {
114
+ return (0, _uuid.v4)();
115
+ };
116
+
117
+ return _this;
118
+ }
119
+
120
+ return _createClass(SubObj);
121
+ }(_BasicObj2.BasicObj);
122
+
123
+ exports["default"] = SubObj;
@@ -12,6 +12,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
12
12
  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); }
13
13
 
14
14
  var getDataStringHook = function getDataStringHook(hook) {
15
+ var _hook$fields;
16
+
15
17
  var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
16
18
  var axios = arguments.length > 2 ? arguments[2] : undefined;
17
19
  var options = {
@@ -25,7 +27,7 @@ var getDataStringHook = function getDataStringHook(hook) {
25
27
  options.headers = hook.headers;
26
28
  }
27
29
 
28
- if (hook.fields) {
30
+ if (hook !== null && hook !== void 0 && (_hook$fields = hook.fields) !== null && _hook$fields !== void 0 && _hook$fields.length) {
29
31
  setFields(options, hook);
30
32
  }
31
33
 
@@ -55,7 +57,9 @@ var createHookItem = function createHookItem(optionItem, defaultMethod, defaultU
55
57
  } else {
56
58
  return {
57
59
  method: defaultMethod,
58
- url: defaultUrl
60
+ url: defaultUrl,
61
+ fields: options.fields || [],
62
+ headers: options.headers || {}
59
63
  };
60
64
  }
61
65
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dataflux",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
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",