Commit 13c491b3bb6c89a2546fd4301ea7396625a55e9d
array: add transactions
Matt McKegg committed on 9/11/2016, 9:58:52 AMParent: 56963dca8e160319d630c173c0f3b003996ae919
Files changed
array.js | changed |
lib/lazy-watcher.js | changed |
array.js | ||
---|---|---|
@@ -66,12 +66,8 @@ | ||
66 | 66 | binder.broadcast() |
67 | 67 | return valueOrObs |
68 | 68 | } |
69 | 69 | |
70 | - | |
71 | - | |
72 | - observable.indexOf | |
73 | - | |
74 | 70 | observable.pop = function () { |
75 | 71 | var result = sources.pop() |
76 | 72 | if (binder.live) tryInvoke(releases.pop()) |
77 | 73 | object.pop() |
@@ -107,8 +103,12 @@ | ||
107 | 103 | binder.broadcast() |
108 | 104 | } |
109 | 105 | } |
110 | 106 | |
107 | + observable.transaction = function (cb) { | |
108 | + binder.transaction(observable, cb) | |
109 | + } | |
110 | + | |
111 | 111 | observable.set = function (values) { |
112 | 112 | if (fixedIndexing) { |
113 | 113 | var length = values && values.length || 0 |
114 | 114 | for (var i = 0; i < length; i++) { |
lib/lazy-watcher.js | ||
---|---|---|
@@ -2,13 +2,22 @@ | ||
2 | 2 | var lazy = false |
3 | 3 | |
4 | 4 | var obj = { |
5 | 5 | live: false, |
6 | + suspended: false, | |
6 | 7 | broadcast: broadcast, |
7 | 8 | update: update, |
8 | 9 | value: null, |
9 | 10 | listeners: [], |
10 | 11 | |
12 | + transaction: function (value, cb) { | |
13 | + var originalValue = obj.suspended | |
14 | + obj.suspended = true | |
15 | + cb(value) | |
16 | + obj.suspended = originalValue | |
17 | + obj.broadcast() | |
18 | + }, | |
19 | + | |
11 | 20 | onUpdate: function () { |
12 | 21 | if (update()) { |
13 | 22 | broadcast() |
14 | 23 | } |
@@ -58,10 +67,12 @@ | ||
58 | 67 | |
59 | 68 | // scoped |
60 | 69 | |
61 | 70 | function broadcast () { |
62 | - var cachedListeners = obj.listeners.slice(0) | |
63 | - for (var i = 0, len = cachedListeners.length; i < len; i++) { | |
64 | - cachedListeners[i](obj.value) | |
71 | + if (!obj.suspended) { | |
72 | + var cachedListeners = obj.listeners.slice(0) | |
73 | + for (var i = 0, len = cachedListeners.length; i < len; i++) { | |
74 | + cachedListeners[i](obj.value) | |
75 | + } | |
65 | 76 | } |
66 | 77 | } |
67 | 78 | } |
Built with git-ssb-web