@webkrafters/react-observable-context 4.0.0 → 4.1.0-alpha.1

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.
@@ -1,4 +1,5 @@
1
1
  export default setState;
2
+ export type TagKey = import("./tag-functions").TagKey;
2
3
  export type HasArrayRoot<K extends KeyTypes = string> = HasRoot<K, Array<any>>;
3
4
  export type HasObjectRoot<K extends KeyTypes = string> = K extends number ? {
4
5
  [rootKey: number]: {
@@ -24,9 +25,8 @@ export type HasRoot<K extends KeyTypes = string, T> = K extends number ? {
24
25
  };
25
26
  export type KeyTypes = number | string | symbol;
26
27
  export type Listener<T extends import("../../types").State> = import("../../types").Listener<T>;
28
+ export type UpdatePayload<T> = import("../../types").UpdatePayload<T>;
27
29
  export type PartialState<T extends import("../../types").State> = import("../../types").PartialState<T>;
28
30
  export type State = import("../../types").State;
29
- export type Stats = {
30
- hasChanges: boolean;
31
- };
32
- declare function setState<T extends import("../../types").State>(state: T, newState: import("../../types").PartialState<T>, onStateChange?: Listener<T>): void;
31
+ export type Stats = import("../../types").UpdateStats;
32
+ declare function setState<T extends import("../../types").State>(state: T, changes: UpdatePayload<import("../../types").PartialState<T>>, onStateChange?: Listener<T>): void;
@@ -3,95 +3,122 @@ Object.defineProperty(exports, "__esModule", {
3
3
  value: true
4
4
  });
5
5
  exports["default"] = void 0;
6
- var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
7
- var _lodash2 = _interopRequireDefault(require("lodash.isequal"));
8
- var _lodash3 = _interopRequireDefault(require("lodash.isplainobject"));
6
+ var _lodash = _interopRequireDefault(require("lodash.isequal"));
7
+ var _lodash2 = _interopRequireDefault(require("lodash.isplainobject"));
8
+ var _utils = require("../../utils");
9
+ var _tagFunctions = _interopRequireWildcard(require("./tag-functions"));
10
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
9
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10
- 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; } } }; }
11
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
12
- 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."); }
13
- 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); }
14
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
15
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
16
- 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; }
17
- var isIndexBasedObj = function isIndexBasedObj(obj) {
18
- return Object.keys(obj).every(function (k) {
19
- var i = +k;
20
- return Number.isInteger(i) && i > -1;
21
- });
22
- };
23
- function setAtomic(state, newState, stateKey, stats) {
24
- if ((0, _lodash2["default"])(state[stateKey], newState[stateKey])) {
25
- return;
13
+ 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); }
14
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
16
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
17
+ var _default = setState;
18
+ exports["default"] = _default;
19
+ function isIndexBasedObj(obj) {
20
+ for (var k in obj) {
21
+ if (!(k in _tagFunctions["default"] || Number.isInteger(+k))) {
22
+ return false;
23
+ }
26
24
  }
27
- var isPlainObjectNewState = (0, _lodash3["default"])(newState[stateKey]);
28
- var isArrayNewState = Array.isArray(newState[stateKey]);
29
- if (Array.isArray(state[stateKey])) {
30
- if (isArrayNewState) {
31
- return setArray(state, newState, stateKey, stats);
25
+ return true;
26
+ }
27
+ function resolveTags(state, changes, stateKey, stats) {
28
+ var resolvedTags = [];
29
+ if ((0, _tagFunctions.isClosedTag)(changes[stateKey])) {
30
+ changes[stateKey] = _defineProperty({}, changes[stateKey], null);
31
+ }
32
+ if (!(0, _utils.isDataContainer)(changes[stateKey])) {
33
+ return resolvedTags;
34
+ }
35
+ for (var k in changes[stateKey]) {
36
+ if (!(stateKey in changes)) {
37
+ break;
32
38
  }
33
- if (isPlainObjectNewState && isIndexBasedObj(newState[stateKey])) {
34
- return setArrayIndex(state, newState, stateKey, stats);
39
+ if ((0, _tagFunctions.isClosedTag)(changes[stateKey][k])) {
40
+ changes[stateKey][k] = _defineProperty({}, changes[stateKey][k], null);
41
+ }
42
+ if (k in _tagFunctions["default"]) {
43
+ _tagFunctions["default"][k](state, stateKey, stats, changes);
44
+ resolvedTags.push(k);
35
45
  }
36
46
  }
37
- if (isPlainObjectNewState && (0, _lodash3["default"])(state[stateKey])) {
38
- return setPlainObject(state, newState, stateKey, stats);
47
+ return resolvedTags;
48
+ }
49
+ function set(state, changes, stats) {
50
+ for (var k in changes) {
51
+ setAtomic(state, changes, k, stats);
39
52
  }
40
- stats.hasChanges = true;
41
- state[stateKey] = isArrayNewState || isPlainObjectNewState ? (0, _lodash["default"])(newState[stateKey]) : newState[stateKey];
42
53
  }
43
- ;
44
- function setArray(state, newState, rootKey, stats) {
45
- var nsLength = newState[rootKey].length;
54
+ function setArray(state, changes, rootKey, stats) {
55
+ var nsLength = changes[rootKey].length;
46
56
  if (state[rootKey].length !== nsLength) {
47
57
  state[rootKey].length = nsLength;
48
58
  stats.hasChanges = true;
49
59
  }
50
60
  for (var i = 0; i < nsLength; i++) {
51
- setAtomic(state[rootKey], newState[rootKey], i, stats);
61
+ setAtomic(state[rootKey], changes[rootKey], i, stats);
52
62
  }
53
63
  }
54
- ;
55
- function setArrayIndex(state, newState, rootKey, stats) {
56
- var incomingIndexes = Object.keys(newState[rootKey]).map(function (i) {
57
- return +i;
58
- });
59
- var maxIncomingIndex = Math.max.apply(Math, _toConsumableArray(incomingIndexes));
64
+ function setArrayIndex(state, changes, rootKey, stats) {
65
+ var incomingIndexes = [];
66
+ for (var k in changes[rootKey]) {
67
+ var index = +k;
68
+ if (index < 0) {
69
+ index = state[rootKey].length + index;
70
+ changes[rootKey][index] = changes[rootKey][k];
71
+ delete changes[rootKey][k];
72
+ }
73
+ index >= 0 && incomingIndexes.push(index);
74
+ }
75
+ var maxIncomingIndex = Math.max.apply(Math, incomingIndexes);
60
76
  if (maxIncomingIndex >= state[rootKey].length) {
61
77
  state[rootKey].length = maxIncomingIndex + 1;
62
78
  stats.hasChanges = true;
63
79
  }
64
- var _iterator = _createForOfIteratorHelper(incomingIndexes),
65
- _step;
66
- try {
67
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
68
- var i = _step.value;
69
- setAtomic(state[rootKey], newState[rootKey], i, stats);
70
- }
71
- } catch (err) {
72
- _iterator.e(err);
73
- } finally {
74
- _iterator.f();
80
+ for (var _i = 0, _incomingIndexes = incomingIndexes; _i < _incomingIndexes.length; _i++) {
81
+ var i = _incomingIndexes[_i];
82
+ setAtomic(state[rootKey], changes[rootKey], i, stats);
75
83
  }
76
84
  }
77
- ;
78
- function setPlainObject(state, newState, rootKey, stats) {
79
- set(state[rootKey], newState[rootKey], stats);
80
- }
81
- ;
82
- function set(state, newState, stats) {
83
- for (var k in newState) {
84
- setAtomic(state, newState, k, stats);
85
+ function setAtomic(state, changes, stateKey, stats) {
86
+ if ((0, _lodash["default"])(state[stateKey], changes[stateKey])) {
87
+ return;
88
+ }
89
+ var tagsResolved = resolveTags(state, changes, stateKey, stats);
90
+ var isPlainObjectNewState = (0, _lodash2["default"])(changes[stateKey]);
91
+ var isArrayNewState = Array.isArray(changes[stateKey]);
92
+ if (Array.isArray(state[stateKey])) {
93
+ if (isArrayNewState) {
94
+ return setArray(state, changes, stateKey, stats);
95
+ }
96
+ if (isPlainObjectNewState && isIndexBasedObj(changes[stateKey])) {
97
+ return setArrayIndex(state, changes, stateKey, stats);
98
+ }
99
+ }
100
+ if (isPlainObjectNewState && (0, _lodash2["default"])(state[stateKey])) {
101
+ return setPlainObject(state, changes, stateKey, stats);
85
102
  }
103
+ if (tagsResolved.length || !(stateKey in changes)) {
104
+ return;
105
+ }
106
+ ;
107
+ stats.hasChanges = true;
108
+ state[stateKey] = isArrayNewState || isPlainObjectNewState ? (0, _utils.clonedeep)(changes[stateKey]) : changes[stateKey];
109
+ }
110
+ function setPlainObject(state, changes, rootKey, stats) {
111
+ set(state[rootKey], changes[rootKey], stats);
86
112
  }
87
- ;
88
- function setState(state, newState, onStateChange) {
113
+ function setState(state, changes, onStateChange) {
89
114
  var stats = {
90
115
  hasChanges: false
91
116
  };
92
- set(state, newState, stats);
93
- stats.hasChanges && (onStateChange === null || onStateChange === void 0 ? void 0 : onStateChange(newState));
94
- }
95
- ;
96
- var _default = setState;
97
- exports["default"] = _default;
117
+ var changeRequest = {
118
+ state: (0, _utils.clonedeep)(changes)
119
+ };
120
+ set({
121
+ state: state
122
+ }, changeRequest, stats);
123
+ stats.hasChanges && (onStateChange === null || onStateChange === void 0 ? void 0 : onStateChange(changes));
124
+ }
@@ -0,0 +1,37 @@
1
+ export function $clear(state: T, stateKey: K, stats: Stats, changes: (import("../../../types").ClearCommand | import("../../../types").BaseType)[] | (T & { [K_1 in K]: import("../../../types").ClearCommand & {
2
+ [x: string]: any;
3
+ }; })): void;
4
+ export const $delete: TagFunction<T, K, import("../../../types").DeleteCommand<T[K]>>;
5
+ export const $move: TagFunction<T, K, import("../../../types").MoveCommand>;
6
+ export const $push: TagFunction<T, K, import("../../../types").PushCommand>;
7
+ export const $replace: TagFunction<T, K, import("../../../types").ReplaceCommand>;
8
+ export function $set(state: T, stateKey: K, stats: Stats, changes: (import("../../../types").BaseType | import("../../../types").SetCommand<T[K]>)[] | (T & { [K_1 in K]: import("../../../types").SetCommand<T[K]> & {
9
+ [x: string]: any;
10
+ }; })): void;
11
+ export const $splice: TagFunction<T, K, import("../../../types").SpliceCommand>;
12
+ export default tagMap;
13
+ export const isClosedTag: (tag: TagKey | BaseType) => boolean;
14
+ export type TagKey = keyof typeof tagMap;
15
+ export type TagFunction<T extends any[] | import("../../../types").State, K extends keyof T, TAG extends {
16
+ "@@CLEAR"?: any;
17
+ "@@DELETE"?: any;
18
+ "@@MOVE"?: any;
19
+ "@@PUSH"?: any;
20
+ "@@REPLACE"?: any;
21
+ "@@SET"?: any;
22
+ "@@SPLICE"?: any;
23
+ }> = (state: T, stateKey: K, stats: Stats, changes?: (import("../../../types").BaseType | TAG)[] | (T & { [K_1 in K]: TAG & {
24
+ [x: string]: any;
25
+ }; })) => void;
26
+ export type State = import("../../../types").State;
27
+ export type Stats = import("../../../types").UpdateStats;
28
+ export type BaseType = import("../../../types").BaseType;
29
+ declare const tagMap: {
30
+ "@@CLEAR": TagFunction<T, K, import("../../../types").ClearCommand>;
31
+ "@@DELETE": TagFunction<T_1, K_1, import("../../../types").DeleteCommand<T_1[K_1]>>;
32
+ "@@MOVE": TagFunction<T_2, K_2, import("../../../types").MoveCommand>;
33
+ "@@PUSH": TagFunction<T_3, K_3, import("../../../types").PushCommand>;
34
+ "@@REPLACE": TagFunction<T_4, K_4, import("../../../types").ReplaceCommand>;
35
+ "@@SET": TagFunction<T_5, K_5, import("../../../types").SetCommand<T_5[K_5]>>;
36
+ "@@SPLICE": TagFunction<T_6, K_6, import("../../../types").SpliceCommand>;
37
+ };
@@ -0,0 +1,348 @@
1
+ "use strict";
2
+ 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); }
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isClosedTag = exports["default"] = exports.$splice = exports.$set = exports.$replace = exports.$push = exports.$move = exports.$delete = exports.$clear = void 0;
7
+ var _lodash = _interopRequireDefault(require("lodash.isempty"));
8
+ var _lodash2 = _interopRequireDefault(require("lodash.isequal"));
9
+ var _lodash3 = _interopRequireDefault(require("lodash.isplainobject"));
10
+ var _constants = require("../../../constants");
11
+ var _utils = require("../../../utils");
12
+ var _tagMap;
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
14
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
16
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
17
+ function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
18
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
19
+ 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."); }
20
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
21
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
22
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
23
+ 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."); }
24
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
25
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
26
+ 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; } } }; }
27
+ 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); }
28
+ 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; }
29
+ var $clear = function () {
30
+ var defaultPredicate = function defaultPredicate() {
31
+ return true;
32
+ };
33
+ var hasItems = function hasItems(state, stateKey) {
34
+ return state[stateKey].length;
35
+ };
36
+ var setDefault = function setDefault(state, stateKey, stats, changes) {
37
+ var predicate = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultPredicate;
38
+ var value = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : null;
39
+ if (predicate(state, stateKey, stats)) {
40
+ state[stateKey] = value;
41
+ stats.hasChanges = true;
42
+ }
43
+ finishTagRequest(changes, stateKey, _constants.CLEAR_TAG);
44
+ };
45
+ var clear = function clear(state, stateKey, stats, changes) {
46
+ if (!(stateKey in state)) {
47
+ return finishTagRequest(changes, stateKey, _constants.CLEAR_TAG);
48
+ }
49
+ var value = state[stateKey];
50
+ if (typeof value === 'undefined' || value === null) {
51
+ return finishTagRequest(changes, stateKey, _constants.CLEAR_TAG);
52
+ }
53
+ if ((0, _lodash3["default"])(value)) {
54
+ var hasChanges = false;
55
+ for (var k in value) {
56
+ delete state[stateKey][k];
57
+ hasChanges = true;
58
+ }
59
+ stats.hasChanges = stats.hasChanges || hasChanges;
60
+ return finishTagRequest(changes, stateKey, _constants.CLEAR_TAG);
61
+ }
62
+ var type = value.constructor.name;
63
+ if (type === 'String') {
64
+ return setDefault(state, stateKey, stats, changes, hasItems, '');
65
+ }
66
+ if (type === 'Array') {
67
+ return setDefault(state, stateKey, stats, changes, hasItems, []);
68
+ }
69
+ setDefault(state, stateKey, stats, changes);
70
+ };
71
+ return clear;
72
+ }();
73
+ exports.$clear = $clear;
74
+ var $delete = function $delete(state, stateKey, stats, changes) {
75
+ var _state$stateKey;
76
+ var deleteKeys = changes[stateKey][_constants.DELETE_TAG];
77
+ if (!Array.isArray(deleteKeys)) {
78
+ throw new TypeError("Invalid entry found at ".concat(_constants.DELETE_TAG, " change property: requires an array of state keys to delete."));
79
+ }
80
+ var finish = function finish() {
81
+ return finishTagRequest(changes, stateKey, _constants.DELETE_TAG);
82
+ };
83
+ var currValue;
84
+ try {
85
+ if (!deleteKeys.length) {
86
+ throw new Error('Delete called with no identified items to delete.');
87
+ }
88
+ ;
89
+ currValue = state[stateKey];
90
+ if ((0, _lodash["default"])(currValue)) {
91
+ throw new Error('Delete called on empty state.');
92
+ }
93
+ } catch (e) {
94
+ return finish();
95
+ }
96
+ deleteKeys = Array.from(new Set(deleteKeys));
97
+ var hasChanges = false;
98
+ if (!Array.isArray(currValue)) {
99
+ var _iterator = _createForOfIteratorHelper(deleteKeys),
100
+ _step;
101
+ try {
102
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
103
+ var k = _step.value;
104
+ if (!(0, _utils.getProperty)(currValue, k).exists) {
105
+ continue;
106
+ }
107
+ delete state[stateKey][k];
108
+ hasChanges = true;
109
+ }
110
+ } catch (err) {
111
+ _iterator.e(err);
112
+ } finally {
113
+ _iterator.f();
114
+ }
115
+ stats.hasChanges = stats.hasChanges || hasChanges;
116
+ return finish();
117
+ }
118
+ var currLen = currValue.length;
119
+ var deleteMap = {};
120
+ var _iterator2 = _createForOfIteratorHelper(deleteKeys),
121
+ _step2;
122
+ try {
123
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
124
+ var key = _step2.value;
125
+ var index = +key;
126
+ if (index > currLen) {
127
+ continue;
128
+ }
129
+ if (index < 0) {
130
+ index = currLen + index;
131
+ if (index < 0) {
132
+ continue;
133
+ }
134
+ }
135
+ deleteMap[index] = null;
136
+ }
137
+ } catch (err) {
138
+ _iterator2.e(err);
139
+ } finally {
140
+ _iterator2.f();
141
+ }
142
+ var newValue = [];
143
+ var numVisited = 0;
144
+ for (var numDeleted = numVisited, deleteLen = deleteKeys.length; numVisited < currLen; numVisited++) {
145
+ if (!(numVisited in deleteMap)) {
146
+ newValue.push(currValue[numVisited]);
147
+ continue;
148
+ }
149
+ if (++numDeleted === deleteLen) {
150
+ numVisited++;
151
+ break;
152
+ }
153
+ }
154
+ if (numVisited < currLen) {
155
+ newValue.push.apply(newValue, _toConsumableArray(currValue.slice(numVisited)));
156
+ }
157
+ if (currLen === newValue.length) {
158
+ return finish();
159
+ }
160
+ state[stateKey].length = 0;
161
+ (_state$stateKey = state[stateKey]).push.apply(_state$stateKey, newValue);
162
+ stats.hasChanges = true;
163
+ finish();
164
+ };
165
+ exports.$delete = $delete;
166
+ var $move = function $move(state, stateKey, stats, changes) {
167
+ var _state$stateKey2;
168
+ var args = changes[stateKey][_constants.MOVE_TAG];
169
+ if (!Array.isArray(args) || args.length < 2 || !Number.isInteger(args[0]) || !Number.isInteger(args[1])) {
170
+ throw new TypeError("Invalid entry found at ".concat(_constants.MOVE_TAG, " change property: expecting an array of at least 2 integer values [fromIndex, toIndex, numItems]. numItems is optional. Use negative index to count from array end."));
171
+ }
172
+ var finish = function finish() {
173
+ return finishTagRequest(changes, stateKey, _constants.MOVE_TAG);
174
+ };
175
+ var value = state[stateKey];
176
+ if (!Array.isArray(value)) {
177
+ return finish();
178
+ }
179
+ var sLen = value.length;
180
+ if (!sLen) {
181
+ return finish();
182
+ }
183
+ var _args = _slicedToArray(args, 3),
184
+ from = _args[0],
185
+ to = _args[1],
186
+ _args$ = _args[2],
187
+ numItems = _args$ === void 0 ? 1 : _args$;
188
+ if (!Number.isInteger(numItems) || numItems < 1) {
189
+ return finish();
190
+ }
191
+ if (from < 0) {
192
+ from = sLen + from;
193
+ }
194
+ if (from < 0 || from >= sLen) {
195
+ return finish();
196
+ }
197
+ if (to < 0) {
198
+ to = sLen + to;
199
+ }
200
+ if (to < 0 || to >= sLen) {
201
+ return finish();
202
+ }
203
+ if (from === to) {
204
+ return finish();
205
+ }
206
+ var maxTransferLen = sLen - from;
207
+ if (numItems > maxTransferLen) {
208
+ numItems = maxTransferLen;
209
+ }
210
+ (_state$stateKey2 = state[stateKey]).splice.apply(_state$stateKey2, [to, 0].concat(_toConsumableArray(value.splice(from, numItems))));
211
+ stats.hasChanges = true;
212
+ finish();
213
+ };
214
+ exports.$move = $move;
215
+ var $push = function $push(state, stateKey, stats, changes) {
216
+ var _state$stateKey3;
217
+ var args = changes[stateKey][_constants.PUSH_TAG];
218
+ if (!Array.isArray(args)) {
219
+ throw new TypeError("Invalid entry found at ".concat(_constants.PUSH_TAG, " change property: expecting an array of [].pudh(...) compliant argument values."));
220
+ }
221
+ if (!args.length || !Array.isArray(state[stateKey])) {
222
+ return finishTagRequest(changes, stateKey, _constants.PUSH_TAG);
223
+ }
224
+ (_state$stateKey3 = state[stateKey]).push.apply(_state$stateKey3, _toConsumableArray(args));
225
+ stats.hasChanges = true;
226
+ finishTagRequest(changes, stateKey, _constants.PUSH_TAG);
227
+ };
228
+ exports.$push = $push;
229
+ var $replace = function $replace(state, stateKey, stats, changes) {
230
+ applyReplaceCommand(_constants.REPLACE_TAG, state, changes, stateKey, stats);
231
+ };
232
+ exports.$replace = $replace;
233
+ var $set = function () {
234
+ var toString = Object.prototype.toString;
235
+ var set = function set(state, stateKey, stats, changes) {
236
+ if (toString.call(changes[stateKey][_constants.SET_TAG]) === '[object Function]') {
237
+ changes[stateKey][_constants.SET_TAG] = changes[stateKey][_constants.SET_TAG]((0, _utils.clonedeep)(state[stateKey]));
238
+ }
239
+ applyReplaceCommand(_constants.SET_TAG, state, changes, stateKey, stats);
240
+ };
241
+ return set;
242
+ }();
243
+ exports.$set = $set;
244
+ var $splice = function $splice(state, stateKey, stats, changes) {
245
+ var args = changes[stateKey][_constants.SPLICE_TAG];
246
+ if (!Array.isArray(args) || args.length < 2 || !Number.isInteger(args[0]) || !Number.isInteger(args[1])) {
247
+ throw new TypeError("Invalid entry found at ".concat(_constants.SPLICE_TAG, " change property: expecting an array of [].splice(...) compliant argument values."));
248
+ }
249
+ var _args2 = _toArray(args),
250
+ start = _args2[0],
251
+ deleteCount = _args2[1],
252
+ items = _args2.slice(2);
253
+ var iLen = items.length;
254
+ var value = state[stateKey];
255
+ if (!Array.isArray(value) || deleteCount < 1 && !iLen) {
256
+ return finishTagRequest(changes, stateKey, _constants.SPLICE_TAG);
257
+ }
258
+ if (deleteCount > 0) {
259
+ var sLen = value.length;
260
+ start = start < 0 ? Math.abs(start) > sLen ? 0 : sLen + start : start > sLen ? sLen : start;
261
+ var maxCount = sLen - start;
262
+ if (maxCount > iLen) {
263
+ maxCount = iLen;
264
+ }
265
+ if (maxCount > deleteCount) {
266
+ maxCount = deleteCount;
267
+ }
268
+ var numLeftTrimmed = 0;
269
+ for (; numLeftTrimmed < maxCount; numLeftTrimmed++) {
270
+ if (!(0, _lodash2["default"])(value[start + numLeftTrimmed], items[numLeftTrimmed])) {
271
+ break;
272
+ }
273
+ }
274
+ start += numLeftTrimmed;
275
+ items.splice(0, numLeftTrimmed);
276
+ iLen = items.length;
277
+ deleteCount -= numLeftTrimmed;
278
+ }
279
+ if (deleteCount > 0 || iLen) {
280
+ var _state$stateKey4;
281
+ (_state$stateKey4 = state[stateKey]).splice.apply(_state$stateKey4, [start, deleteCount].concat(_toConsumableArray(items)));
282
+ stats.hasChanges = true;
283
+ }
284
+ finishTagRequest(changes, stateKey, _constants.SPLICE_TAG);
285
+ };
286
+ exports.$splice = $splice;
287
+ var tagMap = (_tagMap = {}, _defineProperty(_tagMap, _constants.CLEAR_TAG, $clear), _defineProperty(_tagMap, _constants.DELETE_TAG, $delete), _defineProperty(_tagMap, _constants.MOVE_TAG, $move), _defineProperty(_tagMap, _constants.PUSH_TAG, $push), _defineProperty(_tagMap, _constants.REPLACE_TAG, $replace), _defineProperty(_tagMap, _constants.SET_TAG, $set), _defineProperty(_tagMap, _constants.SPLICE_TAG, $splice), _tagMap);
288
+ var _default = tagMap;
289
+ exports["default"] = _default;
290
+ var isClosedTag = function () {
291
+ var NO_PARAM_TAGS = _defineProperty({}, _constants.CLEAR_TAG, null);
292
+ return function (tag) {
293
+ return tag in NO_PARAM_TAGS;
294
+ };
295
+ }();
296
+ exports.isClosedTag = isClosedTag;
297
+ function applyReplaceCommand(tag, state, changes, stateKey, stats) {
298
+ var replacement = changes[stateKey][tag];
299
+ if (!((0, _utils.isDataContainer)(state[stateKey]) && (0, _utils.isDataContainer)(replacement))) {
300
+ if (state[stateKey] !== replacement) {
301
+ state[stateKey] = replacement;
302
+ stats.hasChanges = true;
303
+ }
304
+ return finishTagRequest(changes, stateKey, tag);
305
+ }
306
+ if ((0, _lodash2["default"])(state[stateKey], replacement)) {
307
+ return finishTagRequest(changes, stateKey, tag);
308
+ }
309
+ if (Array.isArray(replacement) && Array.isArray(state[stateKey])) {
310
+ state[stateKey].length = replacement.length;
311
+ }
312
+ for (var k in state[stateKey]) {
313
+ if (k in replacement) {
314
+ state[stateKey][k] = replacement[k];
315
+ } else {
316
+ delete state[stateKey][k];
317
+ }
318
+ }
319
+ for (var _k in replacement) {
320
+ state[stateKey][_k] = replacement[_k];
321
+ }
322
+ stats.hasChanges = true;
323
+ finishTagRequest(changes, stateKey, tag);
324
+ }
325
+ var finishTagRequest = function () {
326
+ var end = function end(changes, key) {
327
+ delete changes[key];
328
+ };
329
+ var runCloser = function runCloser(changes, key, tag) {
330
+ if (isClosedTag(tag)) {
331
+ return end(changes, key);
332
+ }
333
+ var keyCount = 0;
334
+ for (var k in changes[key]) {
335
+ if (++keyCount === 2) {
336
+ return end(changes[key], tag);
337
+ }
338
+ }
339
+ keyCount = 0;
340
+ for (var _k2 in changes) {
341
+ if (++keyCount === 2) {
342
+ return end(changes[key], tag);
343
+ }
344
+ }
345
+ end(changes, key);
346
+ };
347
+ return runCloser;
348
+ }();
@@ -7,6 +7,6 @@ declare class AccessorCache<T extends import("../../types").State> {
7
7
  [propertyPaths: string]: Readonly<any>;
8
8
  };
9
9
  unlinkClient(clientId: string): void;
10
- watchSource(changes: import("../../types").PartialState<T>): void;
10
+ watchSource(changes: import("../../types").UpdatePayload<import("../../types").PartialState<T>>): void;
11
11
  #private;
12
12
  }
@@ -4,11 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = void 0;
7
- var _lodash = _interopRequireDefault(require("lodash.get"));
8
- var _lodash2 = _interopRequireDefault(require("lodash.has"));
9
- var _lodash3 = _interopRequireDefault(require("lodash.isempty"));
10
- var _lodash4 = _interopRequireDefault(require("lodash.isequal"));
7
+ var _lodash = _interopRequireDefault(require("lodash.isempty"));
8
+ var _lodash2 = _interopRequireDefault(require("lodash.isequal"));
11
9
  var _constants = require("../../constants");
10
+ var _utils = require("../../utils");
12
11
  var _atom = _interopRequireDefault(require("../atom"));
13
12
  var _accessor = _interopRequireDefault(require("../accessor"));
14
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
@@ -62,7 +61,7 @@ var AccessorCache = function () {
62
61
  for (var _len = arguments.length, propertyPaths = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
63
62
  propertyPaths[_key - 1] = arguments[_key];
64
63
  }
65
- if ((0, _lodash3["default"])(propertyPaths)) {
64
+ if ((0, _lodash["default"])(propertyPaths)) {
66
65
  propertyPaths = [_constants.FULL_STATE_SELECTOR];
67
66
  }
68
67
  var cacheKey = JSON.stringify(propertyPaths);
@@ -104,11 +103,11 @@ var AccessorCache = function () {
104
103
  var atoms = _classPrivateFieldGet(this, _atoms);
105
104
  var updatedPaths = [];
106
105
  for (var path in atoms) {
107
- if (path !== _constants.FULL_STATE_SELECTOR && !(0, _lodash2["default"])(originChanges, path)) {
106
+ if (path !== _constants.FULL_STATE_SELECTOR && !(0, _utils.getProperty)(originChanges, path).exists) {
108
107
  continue;
109
108
  }
110
109
  var newAtomVal = _classPrivateMethodGet(this, _getOriginAt, _getOriginAt2).call(this, path);
111
- if ((0, _lodash4["default"])(newAtomVal, atoms[path].value)) {
110
+ if ((0, _lodash2["default"])(newAtomVal, atoms[path].value)) {
112
111
  continue;
113
112
  }
114
113
  atoms[path].setValue(newAtomVal);
@@ -146,7 +145,7 @@ function _createAccessor2(cacheKey, propertyPaths) {
146
145
  return _classPrivateFieldGet(this, _accessors)[cacheKey];
147
146
  }
148
147
  function _getOriginAt2(propertyPath) {
149
- return propertyPath !== _constants.FULL_STATE_SELECTOR ? (0, _lodash["default"])(_classPrivateFieldGet(this, _origin), propertyPath) : _classPrivateFieldGet(this, _origin);
148
+ return propertyPath !== _constants.FULL_STATE_SELECTOR ? (0, _utils.getProperty)(_classPrivateFieldGet(this, _origin), propertyPath).value : _classPrivateFieldGet(this, _origin);
150
149
  }
151
150
  var _default = AccessorCache;
152
151
  exports["default"] = _default;