git ssb

1+

Matt McKegg / mutant



Commit 13c491b3bb6c89a2546fd4301ea7396625a55e9d

array: add transactions

Matt McKegg committed on 9/11/2016, 9:58:52 AM
Parent: 56963dca8e160319d630c173c0f3b003996ae919

Files changed

array.jschanged
lib/lazy-watcher.jschanged
array.jsView
@@ -66,12 +66,8 @@
6666 binder.broadcast()
6767 return valueOrObs
6868 }
6969
70-
71-
72- observable.indexOf
73-
7470 observable.pop = function () {
7571 var result = sources.pop()
7672 if (binder.live) tryInvoke(releases.pop())
7773 object.pop()
@@ -107,8 +103,12 @@
107103 binder.broadcast()
108104 }
109105 }
110106
107+ observable.transaction = function (cb) {
108+ binder.transaction(observable, cb)
109+ }
110+
111111 observable.set = function (values) {
112112 if (fixedIndexing) {
113113 var length = values && values.length || 0
114114 for (var i = 0; i < length; i++) {
lib/lazy-watcher.jsView
@@ -2,13 +2,22 @@
22 var lazy = false
33
44 var obj = {
55 live: false,
6+ suspended: false,
67 broadcast: broadcast,
78 update: update,
89 value: null,
910 listeners: [],
1011
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+
1120 onUpdate: function () {
1221 if (update()) {
1322 broadcast()
1423 }
@@ -58,10 +67,12 @@
5867
5968 // scoped
6069
6170 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+ }
6576 }
6677 }
6778 }

Built with git-ssb-web