Commit 7842de264aaac4d297a99352606ade6c44ee4d50
computed: short circuit nextTick if value is read directly when live
Matt McKegg committed on 11/9/2017, 11:20:54 PMParent: fd48051c9a0ccc1e5fd254db048758d8eff91383
Files changed
computed.js | changed |
test/computed.js | changed |
computed.js | ||
---|---|---|
@@ -194,8 +194,11 @@ | ||
194 | 194 … | // temporarily become live to watch for changes until next cycle to stop |
195 | 195 … | // potential double refresh and handle weird race conditions |
196 | 196 … | this.listen() // triggers update |
197 | 197 … | 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() | |
198 | 201 … | } |
199 | 202 … | return this.outputValue |
200 | 203 … | }, |
201 | 204 … | broadcast: function () { |
test/computed.js | ||
---|---|---|
@@ -56,4 +56,29 @@ | ||
56 | 56 … | t.deepEqual(obs(), 2) |
57 | 57 … | t.end() |
58 | 58 … | }) |
59 | 59 … | }) |
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