git ssb

1+

Matt McKegg / mutant



Commit f69a37d1862b7bc2b9590d1514f049129243de67

lazy-watcher: make easier to debug by moving listeners into obj

Matt McKegg committed on 9/7/2016, 1:27:40 PM
Parent: eac2c84077f06deea7722e1dc72af19e3a85c7ed

Files changed

lib/lazy-watcher.jschanged
lib/lazy-watcher.jsView
@@ -1,13 +1,13 @@
11 module.exports = function (update, onBind, onUnbind) {
22 var lazy = false
3- var listeners = []
43
54 var obj = {
65 live: false,
76 broadcast: broadcast,
87 update: update,
98 value: null,
9+ listeners: [],
1010
1111 onUpdate: function () {
1212 if (update()) {
1313 broadcast()
@@ -26,24 +26,24 @@
2626 if (typeof listener !== 'function') {
2727 throw new Error('Listeners must be functions.')
2828 }
2929
30- listeners.push(listener)
30+ obj.listeners.push(listener)
3131
3232 if (!obj.live) {
3333 obj.live = true
3434 lazy = true
3535 onBind()
3636 }
3737
3838 return function release () {
39- for (var i = 0, len = listeners.length; i < len; i++) {
40- if (listeners[i] === listener) {
41- listeners.splice(i, 1)
39+ for (var i = 0, len = obj.listeners.length; i < len; i++) {
40+ if (obj.listeners[i] === listener) {
41+ obj.listeners.splice(i, 1)
4242 break
4343 }
4444 }
45- if (!listeners.length && obj.live) {
45+ if (!obj.listeners.length && obj.live) {
4646 obj.live = false
4747 onUnbind()
4848 }
4949 }
@@ -54,9 +54,9 @@
5454
5555 // scoped
5656
5757 function broadcast () {
58- var cachedListeners = listeners.slice(0)
58+ var cachedListeners = obj.listeners.slice(0)
5959 for (var i = 0, len = cachedListeners.length; i < len; i++) {
6060 cachedListeners[i](obj.value)
6161 }
6262 }

Built with git-ssb-web