-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
state.es
38 lines (29 loc) · 1.13 KB
/
state.es
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
function State
// https://en.wikipedia.org/wiki/Immutable_object
// https://en.wikipedia.org/wiki/Persistent_data_structure
( context, history = [context], handler = _ => {} ) {
this.subscribe = callback => handler = callback
const
clone = context => JSON.parse
(JSON.stringify (context))
, describe = property =>
{
enumerable: true
, get: _ => history
[history.length-1] [property]
, set (value) {
const next = clone
(previous = history [history.length-1])
next [property] = value
handler (previous, next)
history [history.length] = next
}
}
for (property in context)
Object.defineProperty
(this, property, describe (property))
// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.3.6
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties
}