-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr-data.js
More file actions
30 lines (30 loc) · 878 Bytes
/
r-data.js
File metadata and controls
30 lines (30 loc) · 878 Bytes
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
class RData {
constructor(initialValues) {
Object.defineProperty(this, '_values', {value: {}})
Object.defineProperty(this, '_handlers', {value: {}})
if (!initialValues) return
for (const key in initialValues) {
if (initialValues.hasOwnProperty(key)) {
const value = initialValues[key]
this.register(key, value)
}
}
}
register(name, initialValue) {
this._values[name] = initialValue
this._handlers[name] = []
Object.defineProperty(this, name, {
enumerable: true,
get: () => this._values[name],
set: (newVal) => {
const oldVal = this._values[name]
if (oldVal === newVal) return
this._values[name] = newVal
this._handlers[name].forEach(handler => handler(newVal, oldVal))
},
})
}
onChange(name, handler) {
this._handlers[name].push(handler)
}
}