@koordinates/xstate-tree 4.3.0-beta.5 → 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.
@@ -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: ", JSON.stringify((0, utils_1.difference)(ctx, oldCtx), null, 2));
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
- * @private
40
- *
41
- * Find the differences between two objects and push to a new object
42
- * (c) 2019 Chris Ferdinandi & Jascha Brinkmann, MIT License, https://gomakethings.com & https://twitter.com/jaschaio
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 (type1 === "[object Object]") {
86
- const objDiff = difference(item1, item2);
87
- if (Object.keys(objDiff).length > 0) {
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 (type1 === "[object Array]") {
93
- if (!arraysMatch(item1, item2)) {
94
- diffs[key] = item2;
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 (type1 === "[object Function]") {
99
- if (item1.toString() !== item2.toString()) {
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 diffs;
59
+ return result;
122
60
  }
123
61
  exports.difference = difference;
124
62
  /*
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@koordinates/xstate-tree",
3
3
  "main": "lib/index.js",
4
4
  "types": "lib/xstate-tree.d.ts",
5
- "version": "4.3.0-beta.5",
5
+ "version": "4.3.0-beta.6",
6
6
  "license": "MIT",
7
7
  "description": "Build UIs with Actors using xstate and React",
8
8
  "keywords": [