git ssb

1+

Matt McKegg / mutant



Commit 099e49713653c62ab39055b073d7b9d37da7d803

html-element: batch binding checks together

Matt McKegg committed on 11/19/2016, 1:32:51 PM
Parent: 74165a097e2ec4fa70d115e148aa12c5d51bbbf2

Files changed

html-element.jschanged
animation-frame-proxy.jsadded
animation-frame.jsadded
html-element.jsView
@@ -3,8 +3,10 @@
33 var parseTag = require('./lib/parse-tag')
44 var walk = require('./lib/walk')
55 var watch = require('./watch')
66 var caches = new global.WeakMap()
7 +var bindQueue = []
8 +var currentlyBinding = false
79 var watcher = null
810 var invalidateNextTick = require('./lib/invalidate-next-tick')
911
1012 module.exports = function (tag, attributes, children) {
@@ -75,13 +77,25 @@
7577 maybeBind(child, this)
7678 }
7779
7880 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]
8094 if (getRootNode(opts.target) === opts.document) {
8195 walk(node, rebind)
8296 }
83- })
97 + }
8498 }
8599
86100 function checkWatcher (document) {
87101 if (!watcher && global.MutationObserver) {
animation-frame-proxy.jsView
@@ -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.jsView
@@ -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