git ssb

1+

Matt McKegg / mutant



Commit 7842de264aaac4d297a99352606ade6c44ee4d50

computed: short circuit nextTick if value is read directly when live

Matt McKegg committed on 11/9/2017, 11:20:54 PM
Parent: fd48051c9a0ccc1e5fd254db048758d8eff91383

Files changed

computed.jschanged
test/computed.jschanged
computed.jsView
@@ -194,8 +194,11 @@
194194 // temporarily become live to watch for changes until next cycle to stop
195195 // potential double refresh and handle weird race conditions
196196 this.listen() // triggers update
197197 setImmediate(this.boundUnlisten) // only runs if no listeners have been added
198 + } else if (this.updating) {
199 + // short circuit nextTick if this value is read directly
200 + this.updateNow()
198201 }
199202 return this.outputValue
200203 },
201204 broadcast: function () {
test/computed.jsView
@@ -56,4 +56,29 @@
5656 t.deepEqual(obs(), 2)
5757 t.end()
5858 })
5959 })
60 +
61 +test('computed nextTick, observe', function (t) {
62 + var events = []
63 + var value = Value(1)
64 + var obs = computed(value, x => x * 100, {nextTick: true})
65 + obs(value => events.push(value))
66 + value.set(2)
67 + t.deepEqual(events, [])
68 + value.set(3)
69 + t.deepEqual(events, [])
70 +
71 + setImmediate(() => {
72 + t.deepEqual(events, [300])
73 + t.end()
74 + })
75 +})
76 +
77 +test('computed nextTick, same tick `get` (short-circuit delay)', function (t) {
78 + var value = Value(1)
79 + var obs = computed(value, x => x * 100, {nextTick: true})
80 + t.equal(resolve(obs), 100)
81 + value.set(2)
82 + t.equal(resolve(obs), 200)
83 + t.end()
84 +})

Built with git-ssb-web