Commit 2b5c272499d06a199576e602fc62fb998954ad18
computed: add nextTick and immutableTypes options
Matt McKegg committed on 9/4/2016, 12:56:40 PMParent: 74b2772da6668585f5a6c10d0e02e7d463880464
Files changed
computed.js | changed |
computed.js | ||
---|---|---|
@@ -11,9 +11,9 @@ | ||
11 | 11 | module.exports = computed |
12 | 12 | |
13 | 13 | computed.NO_CHANGE = {} |
14 | 14 | |
15 | -function computed (observables, lambda) { | |
15 | +function computed (observables, lambda, opts) { | |
16 | 16 | if (!Array.isArray(observables)) { |
17 | 17 | observables = [observables] |
18 | 18 | } |
19 | 19 | |
@@ -22,8 +22,9 @@ | ||
22 | 22 | var computedValue = null |
23 | 23 | |
24 | 24 | var inner = null |
25 | 25 | var releaseInner = null |
26 | + var updating = false | |
26 | 27 | |
27 | 28 | var live = false |
28 | 29 | var lazy = false |
29 | 30 | var initialized = false |
@@ -91,9 +92,9 @@ | ||
91 | 92 | function update () { |
92 | 93 | var changed = false |
93 | 94 | for (var i = 0, len = observables.length; i < len; i++) { |
94 | 95 | var newValue = resolve(observables[i]) |
95 | - if (newValue !== values[i] || isReferenceType(newValue)) { | |
96 | + if (newValue !== values[i] || isMutable(newValue)) { | |
96 | 97 | changed = true |
97 | 98 | values[i] = newValue |
98 | 99 | } |
99 | 100 | } |
@@ -105,9 +106,9 @@ | ||
105 | 106 | if (newComputedValue === computed.NO_CHANGE) { |
106 | 107 | return false |
107 | 108 | } |
108 | 109 | |
109 | - if (newComputedValue !== computedValue || (isReferenceType(newComputedValue) && !isObservable(newComputedValue))) { | |
110 | + if (newComputedValue !== computedValue || (isMutable(newComputedValue) && !isObservable(newComputedValue))) { | |
110 | 111 | if (releaseInner) { |
111 | 112 | releaseInner() |
112 | 113 | inner = releaseInner = null |
113 | 114 | } |
@@ -128,15 +129,27 @@ | ||
128 | 129 | return false |
129 | 130 | } |
130 | 131 | |
131 | 132 | function onInnerUpdate (value) { |
132 | - if (value !== computedValue || isReferenceType(computedValue)) { | |
133 | + if (value !== computedValue || isMutable(computedValue)) { | |
133 | 134 | computedValue = value |
134 | 135 | broadcast(listeners, computedValue) |
135 | 136 | } |
136 | 137 | } |
137 | 138 | |
138 | 139 | function onUpdate () { |
140 | + if (opts && opts.nextTick) { | |
141 | + if (!updating) { | |
142 | + updating = true | |
143 | + setImmediate(updateNow) | |
144 | + } | |
145 | + } else { | |
146 | + updateNow() | |
147 | + } | |
148 | + } | |
149 | + | |
150 | + function updateNow () { | |
151 | + updating = false | |
139 | 152 | if (update()) { |
140 | 153 | broadcast(listeners, computedValue) |
141 | 154 | } |
142 | 155 | } |
@@ -147,8 +160,16 @@ | ||
147 | 160 | update() |
148 | 161 | } |
149 | 162 | return computedValue |
150 | 163 | } |
164 | + | |
165 | + function isMutable (value) { | |
166 | + if (opts && opts.immutableTypes && opts.immutableTypes.some(type => value instanceof type)) { | |
167 | + return false | |
168 | + } else { | |
169 | + return isReferenceType(value) | |
170 | + } | |
171 | + } | |
151 | 172 | } |
152 | 173 | |
153 | 174 | function isReferenceType (value) { |
154 | 175 | return typeof value === 'object' && value !== null |
Built with git-ssb-web