Commit f69a37d1862b7bc2b9590d1514f049129243de67
lazy-watcher: make easier to debug by moving listeners into obj
Matt McKegg committed on 9/7/2016, 1:27:40 PMParent: eac2c84077f06deea7722e1dc72af19e3a85c7ed
Files changed
lib/lazy-watcher.js | changed |
lib/lazy-watcher.js | ||
---|---|---|
@@ -1,13 +1,13 @@ | ||
1 | 1 | module.exports = function (update, onBind, onUnbind) { |
2 | 2 | var lazy = false |
3 | - var listeners = [] | |
4 | 3 | |
5 | 4 | var obj = { |
6 | 5 | live: false, |
7 | 6 | broadcast: broadcast, |
8 | 7 | update: update, |
9 | 8 | value: null, |
9 | + listeners: [], | |
10 | 10 | |
11 | 11 | onUpdate: function () { |
12 | 12 | if (update()) { |
13 | 13 | broadcast() |
@@ -26,24 +26,24 @@ | ||
26 | 26 | if (typeof listener !== 'function') { |
27 | 27 | throw new Error('Listeners must be functions.') |
28 | 28 | } |
29 | 29 | |
30 | - listeners.push(listener) | |
30 | + obj.listeners.push(listener) | |
31 | 31 | |
32 | 32 | if (!obj.live) { |
33 | 33 | obj.live = true |
34 | 34 | lazy = true |
35 | 35 | onBind() |
36 | 36 | } |
37 | 37 | |
38 | 38 | 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) | |
42 | 42 | break |
43 | 43 | } |
44 | 44 | } |
45 | - if (!listeners.length && obj.live) { | |
45 | + if (!obj.listeners.length && obj.live) { | |
46 | 46 | obj.live = false |
47 | 47 | onUnbind() |
48 | 48 | } |
49 | 49 | } |
@@ -54,9 +54,9 @@ | ||
54 | 54 | |
55 | 55 | // scoped |
56 | 56 | |
57 | 57 | function broadcast () { |
58 | - var cachedListeners = listeners.slice(0) | |
58 | + var cachedListeners = obj.listeners.slice(0) | |
59 | 59 | for (var i = 0, len = cachedListeners.length; i < len; i++) { |
60 | 60 | cachedListeners[i](obj.value) |
61 | 61 | } |
62 | 62 | } |
Built with git-ssb-web