-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathko-inspector.comparer.js
executable file
·132 lines (115 loc) · 4.2 KB
/
ko-inspector.comparer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
if (typeof koInspector == 'undefined') {
koInspector = {};
}
koInspector.Comparer = function () {
this.storedObject = {};
this.cache = [];
this.dirtyFlagCache = [];
return this;
};
koInspector.Comparer.prototype.addDirtyFlags = function (newObject) {
for (p in newObject) {
if (ko.isObservable(newObject[p])) {
if (typeof newObject[p].isDirty === 'undefined') {
newObject[p].isDirty = new koInspector.DirtyFlag(newObject[p]);
}
}
var unwrapped = ko.utils.unwrapObservable(newObject[p]);
if (this.isObject(unwrapped)) {
// if (this.cycleReplacer(unwrapped, this.dirtyFlagCache)) {
// break;
// }
this.addDirtyFlags(unwrapped);
}
}
};
koInspector.Comparer.prototype.isObject = function (object) {
return object && typeof object === 'object' && typeof object.getMonth === 'undefined'
};
koInspector.Comparer.prototype.resetDirtyFlag = function (object) {
object.isDirty(false);
};
koInspector.Comparer.prototype.compare = function (newObject) {
this.addDirtyFlags(ko.utils.unwrapObservable(newObject));
this.recurse(ko.utils.unwrapObservable(newObject), this.storedObject);
};
koInspector.Comparer.prototype.recurse = function (newObject, object) {
for (p in newObject) {
if (ko.isObservable(newObject[p])) { // observable object
var unwrapped = ko.utils.unwrapObservable(newObject[p]);
if (object[p]) { // object is old
this.updateObservable(unwrapped, newObject, object, p);
} else { // object is new
this.parseObject(unwrapped, newObject, object, p);
}
} else { // normal object
if (object[p]) {
this.updateObject(newObject[p], newObject, object, p);
} else {
this.parseObject(newObject[p], newObject, object, p);
}
}
}
};
koInspector.Comparer.prototype.updateObservable = function (unwrapped, newObject, object, p) {
if (this.isObject(object[p].value)) {
if (newObject[p].isDirty()) {
this.resetDirtyFlag(newObject[p]);
object[p].count++;
object[p].value = {};
}
this.recurse(unwrapped, object[p].value)
} else {
if (newObject[p].isDirty()) {
this.resetDirtyFlag(newObject[p]);
object[p].count++;
object[p].value = unwrapped;
object[p].isObservable = true;
}
}
if (object[p]) {
object[p].subscribers = ko.isObservable(newObject[p]) ? newObject[p].getSubscriptionsCount() : 0;
}
};
koInspector.Comparer.prototype.updateObject = function (unwrapped, newObject, object, p) {
if (this.isObject(object[p].value)) {
this.recurse(newObject[p], object[p].value)
} else {
if (object[p].value !== newObject[p]) {
object[p].count++;
object[p].value = newObject[p];
object[p].isObservable = false;
}
}
};
koInspector.Comparer.prototype.parseObject = function (unwrapped, newObject, object, p) {
if (this.isObject(unwrapped)) {
// if (this.cycleReplacer(unwrapped, this.cache)) {
// return;
// }
object[p] = {
value: {},
count: 0,
isObservable: ko.isObservable(newObject[p]),
subscribers: ko.isObservable(newObject[p]) ? newObject[p].getSubscriptionsCount() : 0,
guid: koInspector.guid.create()
};
this.recurse(unwrapped, object[p].value);
} else {
object[p] = {
value: unwrapped,
count: 0,
isObservable: ko.isObservable(newObject[p]),
subscribers: ko.isObservable(newObject[p]) ? newObject[p].getSubscriptionsCount() : 0,
}
};
};
koInspector.Comparer.prototype.cycleReplacer = function (object, cache) {
if (typeof object === 'object' && object !== null) {
if (cache.indexOf(object) !== -1) {
return true;
}
cache.push(object);
return false;
}
};