Files: 32bfa7adad1fe9cf5b68bf33d66a08c36fab8de1 / computed.js
5697 bytesRaw
1 | /* A lazy binding take on computed */ |
2 | // - doesn't start watching observables until itself is watched, and then releases if unwatched |
3 | // - avoids memory/watcher leakage |
4 | // - attaches to inner observables if these are returned from value |
5 | // - doesn't broadcast if value is same as last value (and is `value type` or observable - can't make assuptions about reference types) |
6 | // - doesn't broadcast if value is computed.NO_CHANGE |
7 | |
8 | var resolve = require('./resolve') |
9 | var isObservable = require('./is-observable') |
10 | var isSame = require('./lib/is-same') |
11 | |
12 | module.exports = computed |
13 | |
14 | computed.NO_CHANGE = {} |
15 | computed.extended = extendedComputed |
16 | |
17 | function computed (observables, lambda, opts) { |
18 | // opts: nextTick, comparer, context |
19 | var instance = new ProtoComputed(observables, lambda, opts) |
20 | return instance.MutantComputed.bind(instance) |
21 | } |
22 | |
23 | // optimise memory usage |
24 | function ProtoComputed (observables, lambda, opts) { |
25 | if (!Array.isArray(observables)) { |
26 | observables = [observables] |
27 | } |
28 | this.values = [] |
29 | this.releases = [] |
30 | this.computedValue = null |
31 | this.outputValue = null |
32 | this.inner = null |
33 | this.updating = false |
34 | this.live = false |
35 | this.lazy = false |
36 | this.initialized = false |
37 | this.listeners = [] |
38 | this.observables = observables |
39 | this.lambda = lambda |
40 | this.opts = opts |
41 | this.comparer = opts && opts.comparer || null |
42 | this.context = opts && opts.context || {} |
43 | this.boundOnUpdate = this.onUpdate.bind(this) |
44 | this.boundUpdateNow = this.updateNow.bind(this) |
45 | } |
46 | |
47 | ProtoComputed.prototype = { |
48 | MutantComputed: function (listener) { |
49 | if (!listener) { |
50 | return this.getValue() |
51 | } |
52 | |
53 | if (typeof listener !== 'function') { |
54 | throw new Error('Listeners must be functions.') |
55 | } |
56 | |
57 | this.listeners.push(listener) |
58 | this.listen() |
59 | |
60 | return this.removeListener.bind(this, listener) |
61 | }, |
62 | removeListener: function (listener) { |
63 | for (var i = 0, len = this.listeners.length; i < len; i++) { |
64 | if (this.listeners[i] === listener) { |
65 | this.listeners.splice(i, 1) |
66 | break |
67 | } |
68 | } |
69 | if (!this.listeners.length) { |
70 | this.unlisten() |
71 | } |
72 | }, |
73 | listen: function () { |
74 | if (!this.live) { |
75 | for (var i = 0, len = this.observables.length; i < len; i++) { |
76 | if (isObservable(this.observables[i])) { |
77 | this.releases.push(this.observables[i](this.boundOnUpdate)) |
78 | } |
79 | } |
80 | if (this.inner) { |
81 | this.releaseInner = this.inner(this.onInnerUpdate.bind(this, this.inner)) |
82 | } |
83 | this.live = true |
84 | this.lazy = true |
85 | |
86 | if (this.opts && this.opts.onListen) { |
87 | var release = this.opts.onListen() |
88 | if (typeof release === 'function') { |
89 | this.releases.push(release) |
90 | } |
91 | } |
92 | } |
93 | }, |
94 | unlisten: function () { |
95 | if (this.live) { |
96 | this.live = false |
97 | |
98 | if (this.releaseInner) { |
99 | this.releaseInner() |
100 | this.releaseInner = null |
101 | } |
102 | |
103 | while (this.releases.length) { |
104 | this.releases.pop()() |
105 | } |
106 | |
107 | if (this.opts && this.opts.onUnlisten) { |
108 | this.opts.onUnlisten() |
109 | } |
110 | } |
111 | }, |
112 | update: function () { |
113 | var changed = false |
114 | for (var i = 0, len = this.observables.length; i < len; i++) { |
115 | var newValue = resolve(this.observables[i]) |
116 | if (!isSame(newValue, this.values[i], this.comparer)) { |
117 | changed = true |
118 | this.values[i] = newValue |
119 | } |
120 | } |
121 | |
122 | if (changed || !this.initialized) { |
123 | this.initialized = true |
124 | var newComputedValue = this.lambda.apply(this.context, this.values) |
125 | |
126 | if (newComputedValue === computed.NO_CHANGE) { |
127 | return false |
128 | } |
129 | |
130 | if (!isSame(newComputedValue, this.computedValue, this.comparer)) { |
131 | if (this.releaseInner) { |
132 | this.releaseInner() |
133 | this.inner = this.releaseInner = null |
134 | } |
135 | |
136 | this.computedValue = newComputedValue |
137 | |
138 | if (isObservable(newComputedValue)) { |
139 | // handle returning observable from computed |
140 | this.outputValue = newComputedValue() |
141 | this.inner = newComputedValue |
142 | if (this.live) { |
143 | this.releaseInner = this.inner(this.onInnerUpdate.bind(this, this.inner)) |
144 | } |
145 | } else { |
146 | this.outputValue = this.computedValue |
147 | } |
148 | return true |
149 | } |
150 | } |
151 | return false |
152 | }, |
153 | onUpdate: function () { |
154 | if (this.opts && this.opts.nextTick) { |
155 | if (!this.updating) { |
156 | this.updating = true |
157 | setImmediate(this.boundUpdateNow) |
158 | } |
159 | } else { |
160 | this.updateNow() |
161 | } |
162 | }, |
163 | onInnerUpdate: function (obs, value) { |
164 | if (obs === this.inner) { |
165 | if (!isSame(value, this.outputValue, this.comparer)) { |
166 | this.outputValue = value |
167 | this.broadcast() |
168 | } |
169 | } |
170 | }, |
171 | updateNow: function () { |
172 | this.updating = false |
173 | if (this.update()) { |
174 | this.broadcast() |
175 | } |
176 | }, |
177 | getValue: function () { |
178 | if (!this.live || this.lazy || this.updating) { |
179 | this.lazy = false |
180 | this.update() |
181 | if (this.inner) { |
182 | this.outputValue = resolve(this.inner) |
183 | } |
184 | } |
185 | return this.outputValue |
186 | }, |
187 | broadcast: function () { |
188 | // cache listeners in case modified during broadcast |
189 | var listeners = this.listeners.slice(0) |
190 | for (var i = 0, len = listeners.length; i < len; i++) { |
191 | listeners[i](this.outputValue) |
192 | } |
193 | } |
194 | } |
195 | |
196 | function extendedComputed (observables, update) { |
197 | var live = false |
198 | var lazy = false |
199 | |
200 | var instance = computed(observables, function () { |
201 | return update() |
202 | }, { |
203 | onListen: function () { live = lazy = true }, |
204 | onUnlisten: function () { live = false } |
205 | }) |
206 | |
207 | instance.checkUpdated = function () { |
208 | if (!live || lazy) { |
209 | lazy = false |
210 | update() |
211 | } |
212 | } |
213 | |
214 | return instance |
215 | } |
216 |
Built with git-ssb-web