@koordinates/xstate-tree 4.3.0-beta.4 → 4.3.0-beta.6
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/lib/testingUtilities.js +1 -1
- package/lib/utils.js +15 -77
- package/lib/xstateTree.js +3 -2
- package/package.json +1 -1
package/lib/testingUtilities.js
CHANGED
|
@@ -121,7 +121,7 @@ function buildTestRootComponent(machine, logger) {
|
|
|
121
121
|
(0, xstateTree_1.recursivelySend)(interpreter, event);
|
|
122
122
|
}
|
|
123
123
|
function changeHandler(ctx, oldCtx) {
|
|
124
|
-
logger("onChange: ",
|
|
124
|
+
logger("onChange: ", (0, utils_1.difference)(oldCtx, ctx));
|
|
125
125
|
onChangeEmitter.emit("changed", ctx);
|
|
126
126
|
}
|
|
127
127
|
function onEventHandler(e) {
|
package/lib/utils.js
CHANGED
|
@@ -35,90 +35,28 @@ function isLikelyPageLoad() {
|
|
|
35
35
|
return performance.now() < 5000;
|
|
36
36
|
}
|
|
37
37
|
exports.isLikelyPageLoad = isLikelyPageLoad;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
* @param {Object} obj1 The original object
|
|
44
|
-
* @param {Object} obj2 The object to compare against it
|
|
45
|
-
* @return {Object} An object of differences between the two
|
|
46
|
-
*/
|
|
47
|
-
function difference(obj1, obj2) {
|
|
48
|
-
if (!obj2 || Object.prototype.toString.call(obj2) !== "[object Object]") {
|
|
49
|
-
return obj1;
|
|
50
|
-
}
|
|
51
|
-
const diffs = {};
|
|
52
|
-
let key;
|
|
53
|
-
/**
|
|
54
|
-
* Check if two arrays are equal
|
|
55
|
-
* @param {Array} arr1 The first array
|
|
56
|
-
* @param {Array} arr2 The second array
|
|
57
|
-
* @return {Boolean} If true, both arrays are equal
|
|
58
|
-
*/
|
|
59
|
-
const arraysMatch = function arraysMatch(arr1, arr2) {
|
|
60
|
-
if (arr1.length !== arr2.length)
|
|
61
|
-
return false;
|
|
62
|
-
for (let i = 0; i < arr1.length; i++) {
|
|
63
|
-
if (arr1[i] !== arr2[i])
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
return true;
|
|
67
|
-
};
|
|
68
|
-
/**
|
|
69
|
-
* Compare two items and push non-matches to object
|
|
70
|
-
* @param {*} item1 The first item
|
|
71
|
-
* @param {*} item2 The second item
|
|
72
|
-
* @param {String} key The key in our object
|
|
73
|
-
*/
|
|
74
|
-
function compare(item1, item2, key) {
|
|
75
|
-
const type1 = Object.prototype.toString.call(item1);
|
|
76
|
-
const type2 = Object.prototype.toString.call(item2);
|
|
77
|
-
if (type2 === "[object Undefined]") {
|
|
78
|
-
diffs[key] = null;
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (type1 !== type2) {
|
|
82
|
-
diffs[key] = item2;
|
|
83
|
-
return;
|
|
38
|
+
function difference(a, b) {
|
|
39
|
+
const result = {};
|
|
40
|
+
for (const key in b) {
|
|
41
|
+
if (!a.hasOwnProperty(key)) {
|
|
42
|
+
result[key] = b[key];
|
|
84
43
|
}
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
diffs[key] = objDiff;
|
|
44
|
+
else if (Array.isArray(b[key]) && Array.isArray(a[key])) {
|
|
45
|
+
if (JSON.stringify(b[key]) !== JSON.stringify(a[key])) {
|
|
46
|
+
result[key] = b[key];
|
|
89
47
|
}
|
|
90
|
-
return;
|
|
91
48
|
}
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
49
|
+
else if (typeof b[key] === "object" && typeof a[key] === "object") {
|
|
50
|
+
const value = difference(a[key], b[key]);
|
|
51
|
+
if (Object.keys(value).length > 0) {
|
|
52
|
+
result[key] = value;
|
|
95
53
|
}
|
|
96
|
-
return;
|
|
97
54
|
}
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
diffs[key] = item2;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
if (item1 !== item2) {
|
|
105
|
-
diffs[key] = item2;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
for (key in obj1) {
|
|
110
|
-
if (obj1.hasOwnProperty(key)) {
|
|
111
|
-
compare(obj1[key], obj2[key], key);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
for (key in obj2) {
|
|
115
|
-
if (obj2.hasOwnProperty(key)) {
|
|
116
|
-
if (!obj1[key] && obj1[key] !== obj2[key]) {
|
|
117
|
-
diffs[key] = obj2[key];
|
|
118
|
-
}
|
|
55
|
+
else if (b[key] !== a[key]) {
|
|
56
|
+
result[key] = b[key];
|
|
119
57
|
}
|
|
120
58
|
}
|
|
121
|
-
return
|
|
59
|
+
return result;
|
|
122
60
|
}
|
|
123
61
|
exports.difference = difference;
|
|
124
62
|
/*
|
package/lib/xstateTree.js
CHANGED
|
@@ -152,7 +152,7 @@ function XstateTreeView({ interpreter }) {
|
|
|
152
152
|
// This is needed because the inState function needs to be recreated if the
|
|
153
153
|
// current state the machine is in changes. But _only_ then
|
|
154
154
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
155
|
-
[current.value]);
|
|
155
|
+
[current === null || current === void 0 ? void 0 : current.value]);
|
|
156
156
|
const selectorsProxy = (0, useConstant_1.useConstant)(() => {
|
|
157
157
|
return new Proxy({}, {
|
|
158
158
|
get: (_target, prop) => {
|
|
@@ -206,9 +206,10 @@ exports.XstateTreeView = XstateTreeView;
|
|
|
206
206
|
* @internal
|
|
207
207
|
*/
|
|
208
208
|
function recursivelySend(service, event) {
|
|
209
|
+
var _a;
|
|
209
210
|
const children = ([...service.children.values()] || []).filter((s) => s.id.includes("-slot"));
|
|
210
211
|
// If the service can't handle the event, don't send it
|
|
211
|
-
if (service.
|
|
212
|
+
if ((_a = service.getSnapshot()) === null || _a === void 0 ? void 0 : _a.nextEvents.includes(event.type)) {
|
|
212
213
|
try {
|
|
213
214
|
service.send(event);
|
|
214
215
|
}
|
package/package.json
CHANGED