git ssb

1+

Matt McKegg / mutant



Commit 2b5c272499d06a199576e602fc62fb998954ad18

computed: add nextTick and immutableTypes options

Matt McKegg committed on 9/4/2016, 12:56:40 PM
Parent: 74b2772da6668585f5a6c10d0e02e7d463880464

Files changed

computed.jschanged
computed.jsView
@@ -11,9 +11,9 @@
1111 module.exports = computed
1212
1313 computed.NO_CHANGE = {}
1414
15-function computed (observables, lambda) {
15+function computed (observables, lambda, opts) {
1616 if (!Array.isArray(observables)) {
1717 observables = [observables]
1818 }
1919
@@ -22,8 +22,9 @@
2222 var computedValue = null
2323
2424 var inner = null
2525 var releaseInner = null
26+ var updating = false
2627
2728 var live = false
2829 var lazy = false
2930 var initialized = false
@@ -91,9 +92,9 @@
9192 function update () {
9293 var changed = false
9394 for (var i = 0, len = observables.length; i < len; i++) {
9495 var newValue = resolve(observables[i])
95- if (newValue !== values[i] || isReferenceType(newValue)) {
96+ if (newValue !== values[i] || isMutable(newValue)) {
9697 changed = true
9798 values[i] = newValue
9899 }
99100 }
@@ -105,9 +106,9 @@
105106 if (newComputedValue === computed.NO_CHANGE) {
106107 return false
107108 }
108109
109- if (newComputedValue !== computedValue || (isReferenceType(newComputedValue) && !isObservable(newComputedValue))) {
110+ if (newComputedValue !== computedValue || (isMutable(newComputedValue) && !isObservable(newComputedValue))) {
110111 if (releaseInner) {
111112 releaseInner()
112113 inner = releaseInner = null
113114 }
@@ -128,15 +129,27 @@
128129 return false
129130 }
130131
131132 function onInnerUpdate (value) {
132- if (value !== computedValue || isReferenceType(computedValue)) {
133+ if (value !== computedValue || isMutable(computedValue)) {
133134 computedValue = value
134135 broadcast(listeners, computedValue)
135136 }
136137 }
137138
138139 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
139152 if (update()) {
140153 broadcast(listeners, computedValue)
141154 }
142155 }
@@ -147,8 +160,16 @@
147160 update()
148161 }
149162 return computedValue
150163 }
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+ }
151172 }
152173
153174 function isReferenceType (value) {
154175 return typeof value === 'object' && value !== null

Built with git-ssb-web