@reactables/core 0.7.1-alpha.4 → 0.8.0-alpha.2
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/dist/Helpers/jsonDiff.d.ts +27 -0
- package/dist/Helpers/jsonDiff.test.d.ts +1 -0
- package/dist/index.js +82 -4
- package/package.json +1 -4
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modified from https://github.com/AsyncBanana/microdiff
|
|
3
|
+
* v 1.3.2
|
|
4
|
+
* @description handles serializable (JSON) objects only
|
|
5
|
+
*/
|
|
6
|
+
export interface DifferenceCreate {
|
|
7
|
+
type: 'CREATE';
|
|
8
|
+
path: (string | number)[];
|
|
9
|
+
value: unknown;
|
|
10
|
+
}
|
|
11
|
+
export interface DifferenceRemove {
|
|
12
|
+
type: 'REMOVE';
|
|
13
|
+
path: (string | number)[];
|
|
14
|
+
oldValue: unknown;
|
|
15
|
+
}
|
|
16
|
+
export interface DifferenceChange {
|
|
17
|
+
type: 'CHANGE';
|
|
18
|
+
path: (string | number)[];
|
|
19
|
+
value: unknown;
|
|
20
|
+
oldValue: unknown;
|
|
21
|
+
}
|
|
22
|
+
export type Difference = DifferenceCreate | DifferenceRemove | DifferenceChange;
|
|
23
|
+
interface Options {
|
|
24
|
+
cyclesFix: boolean;
|
|
25
|
+
}
|
|
26
|
+
declare const jsonDiff: (obj: Record<string, any> | any[], newObj: Record<string, any> | any[], options?: Partial<Options>, _stack?: Record<string, any>[]) => Difference[];
|
|
27
|
+
export default jsonDiff;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var operators = require('rxjs/operators');
|
|
6
6
|
var rxjs = require('rxjs');
|
|
7
|
-
var jsonDiff = require('json-diff');
|
|
8
7
|
|
|
9
8
|
/******************************************************************************
|
|
10
9
|
Copyright (c) Microsoft Corporation.
|
|
@@ -99,6 +98,60 @@ var createSlice = function (config) {
|
|
|
99
98
|
};
|
|
100
99
|
};
|
|
101
100
|
|
|
101
|
+
var jsonDiff = function (obj, newObj, options, _stack) {
|
|
102
|
+
if (options === void 0) { options = { cyclesFix: true }; }
|
|
103
|
+
if (_stack === void 0) { _stack = []; }
|
|
104
|
+
var diffs = [];
|
|
105
|
+
var isObjArray = Array.isArray(obj);
|
|
106
|
+
var _loop_1 = function (key) {
|
|
107
|
+
var objKey = obj[key];
|
|
108
|
+
var path = isObjArray ? +key : key;
|
|
109
|
+
if (!(key in newObj)) {
|
|
110
|
+
diffs.push({
|
|
111
|
+
type: 'REMOVE',
|
|
112
|
+
path: [path],
|
|
113
|
+
oldValue: obj[key]
|
|
114
|
+
});
|
|
115
|
+
return "continue";
|
|
116
|
+
}
|
|
117
|
+
var newObjKey = newObj[key];
|
|
118
|
+
var areObjects = typeof objKey === 'object' && typeof newObjKey === 'object';
|
|
119
|
+
if (objKey && newObjKey && areObjects && (!options.cyclesFix || !_stack.includes(objKey))) {
|
|
120
|
+
var nestedDiffs = jsonDiff(objKey, newObjKey, options, options.cyclesFix ? _stack.concat([objKey]) : []);
|
|
121
|
+
// eslint-disable-next-line prefer-spread
|
|
122
|
+
diffs.push.apply(diffs, nestedDiffs.map(function (difference) {
|
|
123
|
+
difference.path.unshift(path);
|
|
124
|
+
return difference;
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
else if (objKey !== newObjKey &&
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
129
|
+
//@ts-ignore
|
|
130
|
+
!(areObjects && (isNaN(objKey) ? objKey + '' === newObjKey + '' : +objKey === +newObjKey))) {
|
|
131
|
+
diffs.push({
|
|
132
|
+
path: [path],
|
|
133
|
+
type: 'CHANGE',
|
|
134
|
+
value: newObjKey,
|
|
135
|
+
oldValue: objKey
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
for (var key in obj) {
|
|
140
|
+
_loop_1(key);
|
|
141
|
+
}
|
|
142
|
+
var isNewObjArray = Array.isArray(newObj);
|
|
143
|
+
for (var key in newObj) {
|
|
144
|
+
if (!(key in obj)) {
|
|
145
|
+
diffs.push({
|
|
146
|
+
type: 'CREATE',
|
|
147
|
+
path: [isNewObjArray ? +key : key],
|
|
148
|
+
value: newObj[key]
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return diffs;
|
|
153
|
+
};
|
|
154
|
+
|
|
102
155
|
var getScopedEffectSignature = function (actionType, key) {
|
|
103
156
|
return "type: ".concat(actionType, ", scoped: true").concat(key ? ",key:".concat(key) : '');
|
|
104
157
|
};
|
|
@@ -137,12 +190,37 @@ var HubFactory = function (_a) {
|
|
|
137
190
|
var debugName = "[Rx Name] ".concat(name || 'undefined', " - ");
|
|
138
191
|
var seedState = initialState !== undefined ? initialState : reducer();
|
|
139
192
|
var state$ = messages$.pipe(operators.tap(function (action) {
|
|
140
|
-
debug && console.log(debugName, '[
|
|
193
|
+
debug && console.log(debugName, '[Message Received]', action);
|
|
141
194
|
}), operators.scan(reducer, seedState), operators.startWith(null, seedState), operators.pairwise(), operators.tap(function (_a) {
|
|
142
195
|
var prevState = _a[0], newState = _a[1];
|
|
143
196
|
if (debug) {
|
|
144
|
-
|
|
145
|
-
|
|
197
|
+
if (prevState &&
|
|
198
|
+
typeof prevState === 'object' &&
|
|
199
|
+
newState &&
|
|
200
|
+
typeof newState === 'object') {
|
|
201
|
+
try {
|
|
202
|
+
var reduceDiff = function (diff) {
|
|
203
|
+
return diff.reduce(function (acc, change) {
|
|
204
|
+
var _a;
|
|
205
|
+
return (__assign(__assign({}, acc), (_a = {}, _a[change.path.join('|')] = change, _a)));
|
|
206
|
+
}, {});
|
|
207
|
+
};
|
|
208
|
+
var difference = reduceDiff(jsonDiff(prevState, newState));
|
|
209
|
+
console.log(debugName, '[Diff]:', Object.keys(difference).length ? difference : null, '[State]:', newState);
|
|
210
|
+
}
|
|
211
|
+
catch (e) {
|
|
212
|
+
console.log('Error Reading Diff:', e, '[State]:', newState);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
var hasDiff = prevState !== newState;
|
|
217
|
+
console.log(debugName, '[Diff]:', hasDiff
|
|
218
|
+
? {
|
|
219
|
+
oldValue: prevState,
|
|
220
|
+
newValue: newState
|
|
221
|
+
}
|
|
222
|
+
: null, '[State]:', newState);
|
|
223
|
+
}
|
|
146
224
|
}
|
|
147
225
|
}), operators.map(function (pair) { return pair[1]; }));
|
|
148
226
|
if (storeValue) {
|