This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
70 lines (53 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {detailedDiff} from 'deep-object-diff';
import {__SUPER_SECRET_CONTAINER_DEBUG_HOOK__} from 'unstated';
const UNSTATED = {
isEnabled: true,
isCollapsed: false,
logStateChanges: true,
containers: {},
get states() {
return Object.fromEntries(
Object.entries(this.containers).map(([key, value]) => [key, value.state])
);
},
logState() {
for (const [key, value] of Object.entries(this.containers)) {
console.log(`%c${key}\n`, 'font-weight:bold', value.state);
}
}
};
__SUPER_SECRET_CONTAINER_DEBUG_HOOK__(container => {
if (!UNSTATED.isEnabled) {
return;
}
const {name} = container.constructor;
UNSTATED.containers[name] = container;
let previousState = container.state;
container.subscribe(() => {
if (!(UNSTATED.isEnabled && UNSTATED.logStateChanges)) {
return;
}
const {state} = container;
const diff = detailedDiff(previousState, state);
const group = UNSTATED.isCollapsed ? console.groupCollapsed : console.group;
group(name);
const hasChanges = object => Object.keys(object).length > 0;
if (hasChanges(diff.added)) {
console.log('Added\n', diff.added);
}
if (hasChanges(diff.updated)) {
console.log('Updated\n', diff.updated);
}
if (hasChanges(diff.deleted)) {
console.log('Deleted\n', diff.deleted);
}
console.log('New state\n', state);
console.log('Old state\n', previousState);
console.groupEnd(name);
previousState = state;
});
});
if (typeof window !== 'undefined') {
window.UNSTATED = UNSTATED;
}
export default UNSTATED;