-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathadapter.js
200 lines (155 loc) · 4.3 KB
/
adapter.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// The default `.` adapter thats comes with Rivets.js. Allows subscribing to
// properties on plain objects, implemented in ES5 natives using
// `Object.defineProperty`.
const defined = (value) => {
return value !== undefined && value !== null
}
const ARRAY_METHODS = [
'push',
'pop',
'shift',
'unshift',
'sort',
'reverse',
'splice'
]
const adapter = {
id: '_rv',
counter: 0,
weakmap: {},
weakReference: function(obj) {
if (!obj.hasOwnProperty(this.id)) {
let id = this.counter++
Object.defineProperty(obj, this.id, {
value: id
})
}
if (!this.weakmap[obj[this.id]]) {
this.weakmap[obj[this.id]] = {
callbacks: {}
}
}
return this.weakmap[obj[this.id]]
},
cleanupWeakReference: function(ref, id) {
if (!Object.keys(ref.callbacks).length) {
if (!(ref.pointers && Object.keys(ref.pointers).length)) {
delete this.weakmap[id]
}
}
},
stubFunction: function(obj, fn) {
let original = obj[fn]
let map = this.weakReference(obj)
let weakmap = this.weakmap
obj[fn] = (...args) => {
let response = original.apply(obj, args)
Object.keys(map.pointers).forEach(r => {
let k = map.pointers[r]
if (defined(weakmap[r])) {
if (weakmap[r].callbacks[k] instanceof Array) {
weakmap[r].callbacks[k].forEach(callback => {
callback()
})
}
}
})
return response
}
},
observeMutations: function(obj, ref, keypath) {
if (obj instanceof Array) {
let map = this.weakReference(obj)
if (!defined(map.pointers)) {
map.pointers = {}
ARRAY_METHODS.forEach(fn => {
this.stubFunction(obj, fn)
})
}
if (!defined(map.pointers[ref])) {
map.pointers[ref] = []
}
if (map.pointers[ref].indexOf(keypath) === -1) {
map.pointers[ref].push(keypath)
}
}
},
unobserveMutations: function(obj, ref, keypath) {
if ((obj instanceof Array) && defined(obj[this.id])) {
let map = this.weakmap[obj[this.id]]
if (map) {
let pointers = map.pointers[ref]
if (pointers) {
let idx = pointers.indexOf(keypath)
if (idx > -1) {
pointers.splice(idx, 1)
}
if (!pointers.length) {
delete map.pointers[ref]
}
this.cleanupWeakReference(map, obj[this.id])
}
}
}
},
observe: function(obj, keypath, callback) {
let callbacks = this.weakReference(obj).callbacks
if (!defined(callbacks[keypath])) {
callbacks[keypath] = []
let desc = Object.getOwnPropertyDescriptor(obj, keypath)
if (!(desc && (desc.get || desc.set))) {
let value = obj[keypath]
Object.defineProperty(obj, keypath, {
enumerable: true,
get: () => {
return value
},
set: newValue => {
if (newValue !== value) {
this.unobserveMutations(value, obj[this.id], keypath)
value = newValue
let map = this.weakmap[obj[this.id]]
if (map) {
let callbacks = map.callbacks
if (callbacks[keypath]) {
callbacks[keypath].forEach(cb => {
cb()
})
}
this.observeMutations(newValue, obj[this.id], keypath)
}
}
}
})
}
}
if (callbacks[keypath].indexOf(callback) === -1) {
callbacks[keypath].push(callback)
}
this.observeMutations(obj[keypath], obj[this.id], keypath)
},
unobserve: function(obj, keypath, callback) {
let map = this.weakmap[obj[this.id]]
if (map) {
let callbacks = map.callbacks[keypath]
if (callbacks) {
let idx = callbacks.indexOf(callback)
if (idx > -1) {
callbacks.splice(idx, 1)
if (!callbacks.length) {
delete map.callbacks[keypath]
this.unobserveMutations(obj[keypath], obj[this.id], keypath)
}
}
this.cleanupWeakReference(map, obj[this.id])
}
}
},
get: function(obj, keypath) {
return obj[keypath]
},
set: (obj, keypath, value) => {
obj[keypath] = value
}
}
export default adapter