Commit 099e49713653c62ab39055b073d7b9d37da7d803
html-element: batch binding checks together
Matt McKegg committed on 11/19/2016, 1:32:51 PMParent: 74165a097e2ec4fa70d115e148aa12c5d51bbbf2
Files changed
html-element.js | changed |
animation-frame-proxy.js | added |
animation-frame.js | added |
html-element.js | ||
---|---|---|
@@ -3,8 +3,10 @@ | ||
3 | 3 … | var parseTag = require('./lib/parse-tag') |
4 | 4 … | var walk = require('./lib/walk') |
5 | 5 … | var watch = require('./watch') |
6 | 6 … | var caches = new global.WeakMap() |
7 … | +var bindQueue = [] | |
8 … | +var currentlyBinding = false | |
7 | 9 … | var watcher = null |
8 | 10 … | var invalidateNextTick = require('./lib/invalidate-next-tick') |
9 | 11 … | |
10 | 12 … | module.exports = function (tag, attributes, children) { |
@@ -75,13 +77,25 @@ | ||
75 | 77 … | maybeBind(child, this) |
76 | 78 … | } |
77 | 79 … | |
78 | 80 … | function maybeBind (node, opts) { |
79 | - setImmediate(function () { | |
81 … | + bindQueue.push([node, opts]) | |
82 … | + if (!currentlyBinding) { | |
83 … | + currentlyBinding = true | |
84 … | + setImmediate(flushBindQueue) | |
85 … | + } | |
86 … | +} | |
87 … | + | |
88 … | +function flushBindQueue () { | |
89 … | + currentlyBinding = false | |
90 … | + while (bindQueue.length) { | |
91 … | + var item = bindQueue.shift() | |
92 … | + var node = item[0] | |
93 … | + var opts = item[1] | |
80 | 94 … | if (getRootNode(opts.target) === opts.document) { |
81 | 95 … | walk(node, rebind) |
82 | 96 … | } |
83 | - }) | |
97 … | + } | |
84 | 98 … | } |
85 | 99 … | |
86 | 100 … | function checkWatcher (document) { |
87 | 101 … | if (!watcher && global.MutationObserver) { |
animation-frame-proxy.js | ||
---|---|---|
@@ -1,0 +1,8 @@ | ||
1 … | +var Proxy = require('./proxy') | |
2 … | +var animationFrame = require('./animation-frame') | |
3 … | + | |
4 … | +module.exports = function AnimationFrameProxy (fn) { | |
5 … | + var obs = Proxy() | |
6 … | + animationFrame(() => obs.set(fn())) | |
7 … | + return obs | |
8 … | +} |
animation-frame.js | ||
---|---|---|
@@ -1,0 +1,28 @@ | ||
1 … | +var queue = [] | |
2 … | +var running = false | |
3 … | +var max = 1 / 60 / 2 | |
4 … | + | |
5 … | +module.exports = function (fn) { | |
6 … | + if (typeof fn !== 'function') { | |
7 … | + throw new Error('Must be a function') | |
8 … | + } | |
9 … | + queue.push(fn) | |
10 … | + if (!running) { | |
11 … | + running = true | |
12 … | + window.requestAnimationFrame(flush) | |
13 … | + } | |
14 … | +} | |
15 … | + | |
16 … | +function flush () { | |
17 … | + var startedAt = Date.now() | |
18 … | + | |
19 … | + while (queue.length && Date.now() - startedAt < max) { | |
20 … | + queue.shift()() | |
21 … | + } | |
22 … | + | |
23 … | + if (queue.length) { | |
24 … | + window.requestAnimationFrame(flush) | |
25 … | + } else { | |
26 … | + running = false | |
27 … | + } | |
28 … | +} |
Built with git-ssb-web