Skip to content

Commit b944583

Browse files
committed
add "idle-proxy" and "once-idle" helpers, add idle option to most types
1 parent 099e497 commit b944583

14 files changed

+54
-39
lines changed

animation-frame-proxy.js

-8
This file was deleted.

array.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ function Array (defaultValues, opts) {
1919
var binder = LazyWatcher(update, listen, unlisten)
2020
binder.value = object
2121

22-
if (opts && opts.nextTick) {
23-
binder.nextTick = true
24-
}
22+
if (opts && opts.idle) binder.idle = true
23+
if (opts && opts.nextTick) binder.nextTick = true
2524

2625
if (defaultValues && defaultValues.length) {
2726
forEach(defaultValues, add)

computed.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
var resolve = require('./resolve')
99
var isObservable = require('./is-observable')
1010
var isSame = require('./lib/is-same')
11+
var onceIdle = require('./once-idle')
1112

1213
module.exports = computed
1314

@@ -151,7 +152,12 @@ ProtoComputed.prototype = {
151152
return false
152153
},
153154
onUpdate: function () {
154-
if (this.opts && this.opts.nextTick) {
155+
if (this.opts && this.opts.idle) {
156+
if (!this.updating) {
157+
this.updating = true
158+
onceIdle(this.boundUpdateNow)
159+
}
160+
} else if (this.opts && this.opts.nextTick) {
155161
if (!this.updating) {
156162
this.updating = true
157163
setImmediate(this.boundUpdateNow)

dict.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ function Dict (defaultValues, opts) {
1919
var binder = LazyWatcher(update, listen, unlisten)
2020
binder.value = object
2121

22-
if (opts && opts.nextTick) {
23-
binder.nextTick = true
24-
}
22+
if (opts && opts.nextTick) binder.nextTick = true
23+
if (opts && opts.idle) binder.idle = true
2524

2625
if (defaultValues) {
2726
forEachPair(defaultValues, put)

idle-proxy.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var Proxy = require('./proxy')
2+
var onceIdle = require('./once-idle')
3+
4+
module.exports = function IdleProxy (fn) {
5+
var obs = Proxy()
6+
onceIdle(() => obs.set(fn()))
7+
return obs
8+
}

lib/lazy-watcher.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var onceIdle = require('../once-idle')
2+
13
module.exports = function (update, onBind, onUnbind) {
24
var lazy = false
35
var context = this
@@ -6,6 +8,7 @@ module.exports = function (update, onBind, onUnbind) {
68
var obj = {
79
live: false,
810
nextTick: false,
11+
idle: false,
912
suspended: false,
1013
update: update,
1114
value: null,
@@ -31,7 +34,12 @@ module.exports = function (update, onBind, onUnbind) {
3134
},
3235

3336
onUpdate: function () {
34-
if (obj.nextTick) {
37+
if (obj.idle) {
38+
if (!updating) {
39+
updating = true
40+
onceIdle(obj.updateAndBroadcast)
41+
}
42+
} else if (obj.nextTick) {
3543
if (!updating) {
3644
updating = true
3745
setImmediate(obj.updateAndBroadcast)

map.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var resolve = require('./resolve')
22
var LazyWatcher = require('./lib/lazy-watcher')
33
var isSame = require('./lib/is-same')
44
var addCollectionMethods = require('./lib/add-collection-methods')
5+
var onceIdle = require('./once-idle')
56

67
module.exports = Map
78

@@ -12,9 +13,8 @@ function Map (obs, lambda, opts) {
1213
var releases = []
1314
var binder = LazyWatcher(update, listen, unlisten)
1415

15-
if (opts && opts.nextTick) {
16-
binder.nextTick = true
17-
}
16+
if (opts && opts.nextTick) binder.nextTick = true
17+
if (opts && opts.idle) binder.idle = true
1818

1919
var itemInvalidators = new global.Map()
2020
var lastValues = new global.Map()
@@ -263,7 +263,9 @@ function Map (obs, lambda, opts) {
263263
}
264264

265265
function doSoon (fn) {
266-
if (opts.delayTime) {
266+
if (opts.idle) {
267+
onceIdle(fn)
268+
} else if (opts.delayTime) {
267269
setTimeout(fn, opts.delayTime)
268270
} else {
269271
setImmediate(fn)

animation-frame.js once-idle.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var queue = []
22
var running = false
3-
var max = 1 / 60 / 2
3+
var max = 1000 / 60
44

55
module.exports = function (fn) {
66
if (typeof fn !== 'function') {
@@ -9,7 +9,7 @@ module.exports = function (fn) {
99
queue.push(fn)
1010
if (!running) {
1111
running = true
12-
window.requestAnimationFrame(flush)
12+
window.requestIdleCallback(flush)
1313
}
1414
}
1515

@@ -21,7 +21,7 @@ function flush () {
2121
}
2222

2323
if (queue.length) {
24-
window.requestAnimationFrame(flush)
24+
window.requestIdleCallback(flush)
2525
} else {
2626
running = false
2727
}

proxy-collection.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ function ProxyCollection (source, opts) {
1010
var binder = LazyWatcher(update, listen, unlisten)
1111
binder.value = resolve(source)
1212

13-
if (opts && opts.nextTick) {
14-
binder.nextTick = true
15-
}
13+
if (opts && opts.nextTick) binder.nextTick = true
14+
if (opts && opts.idle) binder.idle = true
1615

1716
var observable = function MutantProxyCollection (listener) {
1817
if (!listener) {

proxy-dict.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ function ProxyDict (source, opts) {
1010
var binder = LazyWatcher(update, listen, unlisten)
1111
binder.value = resolve(source)
1212

13-
if (opts && opts.nextTick) {
14-
binder.nextTick = true
15-
}
13+
if (opts && opts.nextTick) binder.nextTick = true
14+
if (opts && opts.idle) binder.idle = true
1615

1716
var observable = function MutantProxyDict (listener) {
1817
if (!listener) {

proxy.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ function Proxy (source, opts) {
1010
var binder = LazyWatcher(update, listen, unlisten)
1111
binder.value = resolve(source)
1212

13-
if (opts && opts.nextTick) {
14-
binder.nextTick = true
15-
}
13+
if (opts && opts.nextTick) binder.nextTick = true
14+
if (opts && opts.idle) binder.idle = true
1615

1716
var observable = function MutantProxy (listener) {
1817
if (!listener) {

set.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ function ProtoSet (defaultValues, opts) {
2424
self.binder = LazyWatcher.call(self, self._update, self._listen, self._unlisten)
2525
self.binder.value = this.object
2626

27-
if (opts && opts.nextTick) {
28-
self.binder.nextTick = true
29-
}
27+
if (opts && opts.nextTick) self.binder.nextTick = true
28+
if (opts && opts.idle) self.binder.idle = true
3029

3130
if (defaultValues && defaultValues.length) {
3231
defaultValues.forEach(function (valueOrObs) {

struct.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ function Struct (properties, opts) {
1616
var binder = LazyWatcher(update, listen, unlisten)
1717
binder.value = object
1818

19-
if (opts && opts.nextTick) {
20-
binder.nextTick = true
21-
}
19+
if (opts && opts.nextTick) binder.nextTick = true
20+
if (opts && opts.idle) binder.idle = true
2221

2322
var comparer = opts && opts.comparer || null
2423

watch-all.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var resolve = require('./resolve')
22
var isObservable = require('./is-observable')
3+
var onceIdle = require('./once-idle')
34

45
module.exports = watchAll
56

@@ -19,7 +20,12 @@ function watchAll (observables, listener, opts) {
1920
}
2021

2122
function broadcast () {
22-
if (opts && opts.nextTick) {
23+
if (opts && opts.idle) {
24+
if (!broadcasting) {
25+
broadcasting = true
26+
onceIdle(broadcastNow)
27+
}
28+
} else if (opts && opts.nextTick) {
2329
if (!broadcasting) {
2430
broadcasting = true
2531
setImmediate(broadcastNow)

0 commit comments

Comments
 (0)