prevent infinite observe loops

pull/31/head
Rich-Harris 8 years ago
parent 2741eee8bf
commit 54b713ce5a

@ -302,7 +302,12 @@ export default function generate ( parsed, source, options ) {
if ( !callbacks ) continue;
for ( let i = 0; i < callbacks.length; i += 1 ) {
callbacks[i].call( component, newValue, oldValue );
const callback = callbacks[i];
if ( callback.__calling ) continue;
callback.__calling = true;
callback.call( component, newValue, oldValue );
callback.__calling = false;
}
}
}
@ -328,7 +333,12 @@ export default function generate ( parsed, source, options ) {
const group = options.defer ? observers.deferred : observers.immediate;
( group[ key ] || ( group[ key ] = [] ) ).push( callback );
if ( options.init !== false ) callback( state[ key ] );
if ( options.init !== false ) {
callback.__calling = true;
callback.call( component, state[ key ] );
callback.__calling = false;
}
return {
cancel () {

@ -0,0 +1,27 @@
export default {
data: {
thing: { a: 1 }
},
test ( assert, component ) {
const thing = component.get( 'thing' );
component.observe( 'thing', function ( thing ) {
thing.b = thing.a * 2;
this.set({ thing }); // triggers infinite loop, unless observer breaks it
});
assert.deepEqual( thing, {
a: 1,
b: 2
});
thing.a = 3;
component.set({ thing });
assert.deepEqual( thing, {
a: 3,
b: 6
});
}
};
Loading…
Cancel
Save