-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
85 lines (68 loc) · 2.36 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var Observ = require("observ")
// circular dep between ArrayMethods & this file
module.exports = ObservArray
var splice = require("./splice.js")
var put = require("./put.js")
var set = require("./set.js")
var transaction = require("./transaction.js")
var ArrayMethods = require("./array-methods.js")
var addListener = require("./add-listener.js")
/* ObservArray := (Array<T>) => Observ<
Array<T> & { _diff: Array }
> & {
splice: (index: Number, amount: Number, rest...: T) =>
Array<T>,
push: (values...: T) => Number,
filter: (lambda: Function, thisValue: Any) => Array<T>,
indexOf: (item: T, fromIndex: Number) => Number
}
Fix to make it more like ObservHash.
I.e. you write observables into it.
reading methods take plain JS objects to read
and the value of the array is always an array of plain
objsect.
The observ array instance itself would have indexed
properties that are the observables
*/
function ObservArray(initialList) {
// list is the internal mutable list observ instances that
// all methods on `obs` dispatch to.
var list = initialList
var initialState = []
// copy state out of initialList into initialState
list.forEach(function (observ, index) {
initialState[index] = typeof observ === "function" ?
observ() : observ
})
var obs = Observ(initialState)
obs.splice = splice
// override set and store original for later use
obs._observSet = obs.set
obs.set = set
obs.get = get
obs.getLength = getLength
obs.put = put
obs.transaction = transaction
// you better not mutate this list directly
// this is the list of observs instances
obs._list = list
var removeListeners = list.map(function (observ) {
return typeof observ === "function" ?
addListener(obs, observ) :
null
});
// this is a list of removal functions that must be called
// when observ instances are removed from `obs.list`
// not calling this means we do not GC our observ change
// listeners. Which causes rage bugs
obs._removeListeners = removeListeners
obs._type = "observ-array"
obs._version = "3"
return ArrayMethods(obs, list)
}
function get(index) {
return this._list[index]
}
function getLength() {
return this._list.length
}