Commit dacc7137a972984d5f5adfe48e91c35b8056adb9
lazy-watcher: allow use with prototypes
Matt McKegg committed on 10/6/2016, 4:24:27 AMParent: 97b585dee0f210742bd323dea327d09701a333d6
Files changed
lib/lazy-watcher.js | changed |
lib/lazy-watcher.js | ||
---|---|---|
@@ -1,6 +1,7 @@ | ||
1 | 1 | module.exports = function (update, onBind, onUnbind) { |
2 | 2 | var lazy = false |
3 | + var context = this | |
3 | 4 | |
4 | 5 | var obj = { |
5 | 6 | live: false, |
6 | 7 | suspended: false, |
@@ -17,22 +18,22 @@ | ||
17 | 18 | obj.broadcast() |
18 | 19 | }, |
19 | 20 | |
20 | 21 | onUpdate: function () { |
21 | - if (update()) { | |
22 | + if (update.apply(context)) { | |
22 | 23 | broadcast() |
23 | 24 | } |
24 | 25 | }, |
25 | 26 | |
26 | 27 | checkUpdated: function () { |
27 | 28 | if (!obj.live || lazy) { |
28 | 29 | lazy = false |
29 | - update() | |
30 | + update.apply(context) | |
30 | 31 | } |
31 | 32 | }, |
32 | 33 | |
33 | 34 | getValue: function () { |
34 | - obj.checkUpdated() | |
35 | + obj.checkUpdated.apply(context) | |
35 | 36 | return obj.value |
36 | 37 | }, |
37 | 38 | |
38 | 39 | addListener: function (listener) { |
@@ -44,9 +45,9 @@ | ||
44 | 45 | |
45 | 46 | if (!obj.live) { |
46 | 47 | obj.live = true |
47 | 48 | lazy = true |
48 | - onBind() | |
49 | + onBind.apply(context) | |
49 | 50 | } |
50 | 51 | |
51 | 52 | return function release () { |
52 | 53 | for (var i = 0, len = obj.listeners.length; i < len; i++) { |
@@ -56,9 +57,9 @@ | ||
56 | 57 | } |
57 | 58 | } |
58 | 59 | if (!obj.listeners.length && obj.live) { |
59 | 60 | obj.live = false |
60 | - onUnbind() | |
61 | + onUnbind.apply(context) | |
61 | 62 | } |
62 | 63 | } |
63 | 64 | } |
64 | 65 | } |
Built with git-ssb-web