git ssb

1+

Matt McKegg / mutant



Commit 1b503e67aebb1a6a8373f20984ff3e6e745ecaa0

cache listeners on broadcast in case listeners change during iteration

Matt McKegg committed on 7/22/2016, 12:27:52 PM
Parent: 955ee4ba0914a6c2c9a1158bdfa7ea7a186ca900

Files changed

computed.jschanged
map.jschanged
value.jschanged
computed.jsView
@@ -130,19 +130,15 @@
130130
131131 function onInnerUpdate (value) {
132132 if (value !== computedValue || isReferenceType(computedValue)) {
133133 computedValue = value
134- for (var i = 0, len = listeners.length; i < len; i++) {
135- listeners[i](computedValue)
136- }
134 + broadcast(listeners, computedValue)
137135 }
138136 }
139137
140138 function onUpdate () {
141139 if (update()) {
142- for (var i = 0, len = listeners.length; i < len; i++) {
143- listeners[i](computedValue)
144- }
140 + broadcast(listeners, computedValue)
145141 }
146142 }
147143
148144 function getValue () {
@@ -156,4 +152,12 @@
156152
157153 function isReferenceType (value) {
158154 return typeof value === 'object' && value !== null
159155 }
156 +
157 +function broadcast (listeners, value) {
158 + // cache listeners in case modified during broadcast
159 + listeners = listeners.slice(0)
160 + for (var i = 0, len = listeners.length; i < len; i++) {
161 + listeners[i](value)
162 + }
163 +}
map.jsView
@@ -154,10 +154,11 @@
154154 })
155155 }
156156
157157 function broadcast () {
158- for (var i = 0, len = listeners.length; i < len; i++) {
159- listeners[i](values)
158 + var cachedListeners = listeners.slice(0)
159 + for (var i = 0, len = cachedListeners.length; i < len; i++) {
160 + cachedListeners[i](values)
160161 }
161162 }
162163
163164 function getValue () {
value.jsView
@@ -6,10 +6,11 @@
66
77 observable.set = function (v) {
88 value = v
99
10- for (var i = 0, len = listeners.length; i < len; i++) {
11- listeners[i](v)
10 + var cachedListeners = listeners.slice(0)
11 + for (var i = 0, len = cachedListeners.length; i < len; i++) {
12 + cachedListeners[i](v)
1213 }
1314 }
1415
1516 return observable

Built with git-ssb-web