From 33afb7e49a1b194dca7848416c856d486eaeca61 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Apr 2018 11:30:36 -0400 Subject: [PATCH 1/8] add some onstate/onupdate tests --- test/runtime/samples/onupdate/_config.js | 21 +++++++++++++++++++ test/runtime/samples/onupdate/main.html | 12 +++++++++++ .../samples/onstate-arrow-no-this/errors.json | 1 + .../samples/onstate-arrow-no-this/input.html | 5 +++++ .../samples/onstate-arrow-this/errors.json | 12 +++++++++++ .../samples/onstate-arrow-this/input.html | 7 +++++++ .../onupdate-arrow-no-this/errors.json | 1 + .../samples/onupdate-arrow-no-this/input.html | 5 +++++ .../samples/onupdate-arrow-this/errors.json | 12 +++++++++++ .../samples/onupdate-arrow-this/input.html | 7 +++++++ 10 files changed, 83 insertions(+) create mode 100644 test/runtime/samples/onupdate/_config.js create mode 100644 test/runtime/samples/onupdate/main.html create mode 100644 test/validator/samples/onstate-arrow-no-this/errors.json create mode 100644 test/validator/samples/onstate-arrow-no-this/input.html create mode 100644 test/validator/samples/onstate-arrow-this/errors.json create mode 100644 test/validator/samples/onstate-arrow-this/input.html create mode 100644 test/validator/samples/onupdate-arrow-no-this/errors.json create mode 100644 test/validator/samples/onupdate-arrow-no-this/input.html create mode 100644 test/validator/samples/onupdate-arrow-this/errors.json create mode 100644 test/validator/samples/onupdate-arrow-this/input.html diff --git a/test/runtime/samples/onupdate/_config.js b/test/runtime/samples/onupdate/_config.js new file mode 100644 index 000000000000..1603c5a74a12 --- /dev/null +++ b/test/runtime/samples/onupdate/_config.js @@ -0,0 +1,21 @@ +export default { + solo: true, + 'skip-ssr': true, + + data: { + value: 'hello!' + }, + + html: ` +

hello!

+

hello!

+ `, + + test(assert, component, target) { + component.set({ value: 'goodbye!' }); + assert.htmlEqual(target.innerHTML, ` +

goodbye!

+

goodbye!

+ `); + } +}; diff --git a/test/runtime/samples/onupdate/main.html b/test/runtime/samples/onupdate/main.html new file mode 100644 index 000000000000..ea65e9f3d27e --- /dev/null +++ b/test/runtime/samples/onupdate/main.html @@ -0,0 +1,12 @@ +

{{value}}

+

+ + \ No newline at end of file diff --git a/test/validator/samples/onstate-arrow-no-this/errors.json b/test/validator/samples/onstate-arrow-no-this/errors.json new file mode 100644 index 000000000000..fe51488c7066 --- /dev/null +++ b/test/validator/samples/onstate-arrow-no-this/errors.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/samples/onstate-arrow-no-this/input.html b/test/validator/samples/onstate-arrow-no-this/input.html new file mode 100644 index 000000000000..dcd651f66a08 --- /dev/null +++ b/test/validator/samples/onstate-arrow-no-this/input.html @@ -0,0 +1,5 @@ + diff --git a/test/validator/samples/onstate-arrow-this/errors.json b/test/validator/samples/onstate-arrow-this/errors.json new file mode 100644 index 000000000000..cfcba87f4e19 --- /dev/null +++ b/test/validator/samples/onstate-arrow-this/errors.json @@ -0,0 +1,12 @@ +[{ + "message": "'onstate' should be a function expression, not an arrow function expression", + "pos": 29, + "loc": { + "line": 3, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } +}] diff --git a/test/validator/samples/onstate-arrow-this/input.html b/test/validator/samples/onstate-arrow-this/input.html new file mode 100644 index 000000000000..f0921ea81dc1 --- /dev/null +++ b/test/validator/samples/onstate-arrow-this/input.html @@ -0,0 +1,7 @@ + diff --git a/test/validator/samples/onupdate-arrow-no-this/errors.json b/test/validator/samples/onupdate-arrow-no-this/errors.json new file mode 100644 index 000000000000..fe51488c7066 --- /dev/null +++ b/test/validator/samples/onupdate-arrow-no-this/errors.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/samples/onupdate-arrow-no-this/input.html b/test/validator/samples/onupdate-arrow-no-this/input.html new file mode 100644 index 000000000000..72c35b0df94f --- /dev/null +++ b/test/validator/samples/onupdate-arrow-no-this/input.html @@ -0,0 +1,5 @@ + diff --git a/test/validator/samples/onupdate-arrow-this/errors.json b/test/validator/samples/onupdate-arrow-this/errors.json new file mode 100644 index 000000000000..e4c1a3a44140 --- /dev/null +++ b/test/validator/samples/onupdate-arrow-this/errors.json @@ -0,0 +1,12 @@ +[{ + "message": "'onupdate' should be a function expression, not an arrow function expression", + "pos": 29, + "loc": { + "line": 3, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } +}] diff --git a/test/validator/samples/onupdate-arrow-this/input.html b/test/validator/samples/onupdate-arrow-this/input.html new file mode 100644 index 000000000000..3f4dcd897d2d --- /dev/null +++ b/test/validator/samples/onupdate-arrow-this/input.html @@ -0,0 +1,7 @@ + From c1573dbf2c0e29089b97572a8b339d8d9055010e Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Apr 2018 13:09:59 -0400 Subject: [PATCH 2/8] implement onstate and onupdate --- src/generators/Generator.ts | 8 +++ src/generators/dom/index.ts | 24 ++++++-- src/shared/index.js | 32 +++++----- src/validate/js/propValidators/index.ts | 4 ++ src/validate/js/propValidators/onstate.ts | 14 +++++ src/validate/js/propValidators/onupdate.ts | 14 +++++ store.js | 41 +++++++------ test/js/samples/action/expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../component-static/expected-bundle.js | 53 +++++----------- .../computed-collapsed-if/expected-bundle.js | 53 +++++----------- .../css-media-query/expected-bundle.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../deconflict-builtins/_actual-bundle-v2.js | 53 +++++----------- .../deconflict-builtins/expected-bundle-v2.js | 53 +++++----------- .../deconflict-builtins/expected-bundle.js | 53 +++++----------- .../deconflict-globals/expected-bundle.js | 60 +++++++------------ .../js/samples/deconflict-globals/expected.js | 7 ++- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../do-use-dataset/_actual-bundle-v2.js | 53 +++++----------- .../do-use-dataset/expected-bundle-v2.js | 53 +++++----------- .../samples/do-use-dataset/expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../event-handlers-custom/expected-bundle.js | 53 +++++----------- .../head-no-whitespace/_actual-bundle-v2.js | 53 +++++----------- .../head-no-whitespace/expected-bundle-v2.js | 53 +++++----------- .../head-no-whitespace/expected-bundle.js | 53 +++++----------- .../if-block-no-update/_actual-bundle-v2.js | 53 +++++----------- .../if-block-no-update/expected-bundle-v2.js | 53 +++++----------- .../if-block-no-update/expected-bundle.js | 53 +++++----------- .../if-block-simple/_actual-bundle-v2.js | 53 +++++----------- .../if-block-simple/expected-bundle-v2.js | 53 +++++----------- .../if-block-simple/expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../inline-style-optimized/expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../samples/legacy-default/expected-bundle.js | 53 +++++----------- .../legacy-input-type/expected-bundle.js | 53 +++++----------- .../legacy-quote-class/expected-bundle.js | 53 +++++----------- .../samples/media-bindings/expected-bundle.js | 53 +++++----------- .../non-imported-component/expected-bundle.js | 53 +++++----------- .../expected-bundle.js | 60 +++++++------------ .../onrender-onteardown-rewritten/expected.js | 7 ++- .../samples/setup-method/expected-bundle.js | 53 +++++----------- test/js/samples/svg-title/expected-bundle.js | 53 +++++----------- test/js/samples/title/_actual-bundle-v2.js | 53 +++++----------- test/js/samples/title/expected-bundle-v2.js | 53 +++++----------- test/js/samples/title/expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../expected-bundle.js | 53 +++++----------- .../_actual-bundle-v2.js | 53 +++++----------- .../expected-bundle-v2.js | 53 +++++----------- .../window-binding-scroll/expected-bundle.js | 53 +++++----------- .../onstate-before-oncreate/_config.js | 8 +++ .../samples/onstate-before-oncreate/main.html | 16 +++++ test/runtime/samples/onstate-event/_config.js | 49 +++++++++++++++ test/runtime/samples/onstate-event/main.html | 2 + test/runtime/samples/onstate/_config.js | 20 +++++++ test/runtime/samples/onstate/main.html | 10 ++++ test/runtime/samples/onupdate/_config.js | 1 - .../samples/onstate-arrow-this/errors.json | 1 + .../samples/onupdate-arrow-this/errors.json | 1 + 87 files changed, 1193 insertions(+), 2737 deletions(-) create mode 100644 src/validate/js/propValidators/onstate.ts create mode 100644 src/validate/js/propValidators/onupdate.ts create mode 100644 test/runtime/samples/onstate-before-oncreate/_config.js create mode 100644 test/runtime/samples/onstate-before-oncreate/main.html create mode 100644 test/runtime/samples/onstate-event/_config.js create mode 100644 test/runtime/samples/onstate-event/main.html create mode 100644 test/runtime/samples/onstate/_config.js create mode 100644 test/runtime/samples/onstate/main.html diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 6d49faf906d8..5dbb202259c2 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -646,6 +646,14 @@ export default class Generator { addDeclaration('ondestroy', templateProperties.ondestroy.value); } + if (templateProperties.onstate && dom) { + addDeclaration('onstate', templateProperties.onstate.value); + } + + if (templateProperties.onupdate && dom) { + addDeclaration('onupdate', templateProperties.onupdate.value); + } + if (templateProperties.preload) { addDeclaration('preload', templateProperties.preload.value); } diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 51f4664ae7c0..4de05981afaa 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -171,6 +171,8 @@ export default function dom( initialState.push(`options.data`); + const hasInitHooks = !!(templateProperties.oncreate || templateProperties.onstate || templateProperties.onupdate); + const constructorBody = deindent` ${options.dev && `this._debugName = '${debugName}';`} ${options.dev && !generator.customElement && @@ -199,6 +201,9 @@ export default function dom( ${generator.bindingGroups.length && `this._bindingGroups = [${Array(generator.bindingGroups.length).fill('[]').join(', ')}];`} + ${templateProperties.onstate && `this._handlers.state = [%onstate];`} + ${templateProperties.onupdate && `this._handlers.update = [%onupdate];`} + ${(templateProperties.ondestroy || storeProps.length) && ( `this._handlers.destroy = [${ [templateProperties.ondestroy && `%ondestroy`, storeProps.length && `@removeFromStore`].filter(Boolean).join(', ') @@ -216,9 +221,18 @@ export default function dom( `if (!document.getElementById("${generator.stylesheet.id}-style")) @add_css();`) } - ${templateProperties.oncreate && `var _oncreate = %oncreate.bind(this);`} + ${hasInitHooks && deindent` + var self = this; + var _oncreate = function() { + var initial = { changed: { ${expectedProperties.map(p => `${p}: 1`).join(', ')} }, current: self._state, previous: undefined }; + ${templateProperties.onstate && `%onstate.call(self, initial);`} + ${templateProperties.oncreate && `%oncreate.call(self);`} + ${templateProperties.onupdate && `%onupdate.call(self, initial);`} + self.fire("update", initial); + }; + `} - ${(templateProperties.oncreate || generator.hasComponents || generator.hasComplexBindings || generator.hasIntroTransitions) && deindent` + ${(hasInitHooks || generator.hasComponents || generator.hasComplexBindings || generator.hasIntroTransitions) && deindent` if (!options.root) { this._oncreate = []; ${(generator.hasComponents || generator.hasComplexBindings) && `this._beforecreate = [];`} @@ -230,7 +244,7 @@ export default function dom( this._fragment = @create_main_fragment(this, this._state); - ${(templateProperties.oncreate) && deindent` + ${hasInitHooks && deindent` this.root._oncreate.push(_oncreate); `} @@ -253,10 +267,10 @@ export default function dom( `} this._mount(options.target, options.anchor); - ${(generator.hasComponents || generator.hasComplexBindings || templateProperties.oncreate || generator.hasIntroTransitions) && deindent` + ${(generator.hasComponents || generator.hasComplexBindings || hasInitHooks || generator.hasIntroTransitions) && deindent` ${generator.hasComponents && `this._lock = true;`} ${(generator.hasComponents || generator.hasComplexBindings) && `@callAll(this._beforecreate);`} - ${(generator.hasComponents || templateProperties.oncreate) && `@callAll(this._oncreate);`} + ${(generator.hasComponents || hasInitHooks) && `@callAll(this._oncreate);`} ${(generator.hasComponents || generator.hasIntroTransitions) && `@callAll(this._aftercreate);`} ${generator.hasComponents && `this._lock = false;`} `} diff --git a/src/shared/index.js b/src/shared/index.js index 5a8c9e470b5a..5beb57cecae6 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -62,7 +62,13 @@ export function fire(eventName, data) { if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -71,7 +77,6 @@ export function get(key) { } export function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -81,24 +86,15 @@ export function init(component, options) { } export function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } export function observeDev(key, callback, options) { @@ -169,9 +165,9 @@ export function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/src/validate/js/propValidators/index.ts b/src/validate/js/propValidators/index.ts index 8641ed36b9e9..9f14ebc3547e 100644 --- a/src/validate/js/propValidators/index.ts +++ b/src/validate/js/propValidators/index.ts @@ -3,6 +3,8 @@ import actions from './actions'; import computed from './computed'; import oncreate from './oncreate'; import ondestroy from './ondestroy'; +import onstate from './onstate'; +import onupdate from './onupdate'; import onrender from './onrender'; import onteardown from './onteardown'; import helpers from './helpers'; @@ -24,6 +26,8 @@ export default { computed, oncreate, ondestroy, + onstate, + onupdate, onrender, onteardown, helpers, diff --git a/src/validate/js/propValidators/onstate.ts b/src/validate/js/propValidators/onstate.ts new file mode 100644 index 000000000000..c8aa1f5127f9 --- /dev/null +++ b/src/validate/js/propValidators/onstate.ts @@ -0,0 +1,14 @@ +import usesThisOrArguments from '../utils/usesThisOrArguments'; +import { Validator } from '../../index'; +import { Node } from '../../../interfaces'; + +export default function onstate(validator: Validator, prop: Node) { + if (prop.value.type === 'ArrowFunctionExpression') { + if (usesThisOrArguments(prop.value.body)) { + validator.error(prop, { + code: `invalid-onstate-property`, + message: `'onstate' should be a function expression, not an arrow function expression` + }); + } + } +} diff --git a/src/validate/js/propValidators/onupdate.ts b/src/validate/js/propValidators/onupdate.ts new file mode 100644 index 000000000000..5fd649d9473c --- /dev/null +++ b/src/validate/js/propValidators/onupdate.ts @@ -0,0 +1,14 @@ +import usesThisOrArguments from '../utils/usesThisOrArguments'; +import { Validator } from '../../index'; +import { Node } from '../../../interfaces'; + +export default function onupdate(validator: Validator, prop: Node) { + if (prop.value.type === 'ArrowFunctionExpression') { + if (usesThisOrArguments(prop.value.body)) { + validator.error(prop, { + code: `invalid-onupdate-property`, + message: `'onupdate' should be a function expression, not an arrow function expression` + }); + } + } +} diff --git a/store.js b/store.js index 52acd346889f..1447c78eaa0b 100644 --- a/store.js +++ b/store.js @@ -5,12 +5,14 @@ import { _differsImmutable, dispatchObservers, get, - observe + observe, + on, + fire } from './shared.js'; function Store(state, options) { this._observers = { pre: blankObject(), post: blankObject() }; - this._changeHandlers = []; + this._handlers = {}; this._dependents = []; this._computed = blankObject(); @@ -105,21 +107,20 @@ assign(Store.prototype, { this._sortComputedProperties(); }, + fire: fire, + get: get, + // TODO remove this method observe: observe, - onchange: function(callback) { - this._changeHandlers.push(callback); - - var store = this; + on: on, - return { - cancel: function() { - var index = store._changeHandlers.indexOf(callback); - if (~index) store._changeHandlers.splice(index, 1); - } - }; + onchange: function(callback) { + // TODO remove this method + return this.on('state', function(event) { + callback(event.current, event.changed); + }); }, set: function(newState) { @@ -139,11 +140,11 @@ assign(Store.prototype, { this._sortedComputedProperties[i].update(this._state, changed); } - for (var i = 0; i < this._changeHandlers.length; i += 1) { - this._changeHandlers[i](this._state, changed); - } - - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire('state', { + changed: changed, + current: this._state, + previous: oldState + }); var dependents = this._dependents.slice(); // guard against mutations for (var i = 0; i < dependents.length; i += 1) { @@ -162,7 +163,11 @@ assign(Store.prototype, { if (dirty) dependent.component.set(componentState); } - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire('update', { + changed: changed, + current: this._state, + previous: oldState + }); } }); diff --git a/test/js/samples/action/expected-bundle.js b/test/js/samples/action/expected-bundle.js index 68fdff503d42..e316279a1c8a 100644 --- a/test/js/samples/action/expected-bundle.js +++ b/test/js/samples/action/expected-bundle.js @@ -35,34 +35,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -71,7 +56,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -81,24 +65,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -140,9 +115,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/collapses-text-around-comments/_actual-bundle-v2.js b/test/js/samples/collapses-text-around-comments/_actual-bundle-v2.js index 83e680be30f1..af5c88e15220 100644 --- a/test/js/samples/collapses-text-around-comments/_actual-bundle-v2.js +++ b/test/js/samples/collapses-text-around-comments/_actual-bundle-v2.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle-v2.js b/test/js/samples/collapses-text-around-comments/expected-bundle-v2.js index 83e680be30f1..af5c88e15220 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle-v2.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle-v2.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index 83e680be30f1..af5c88e15220 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/component-static-immutable/expected-bundle.js b/test/js/samples/component-static-immutable/expected-bundle.js index 572dd7543923..27aa216840e0 100644 --- a/test/js/samples/component-static-immutable/expected-bundle.js +++ b/test/js/samples/component-static-immutable/expected-bundle.js @@ -27,34 +27,19 @@ function _differsImmutable(a, b) { return a != a ? b == b : a !== b; } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -63,7 +48,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -73,24 +57,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -132,9 +107,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/component-static-immutable2/expected-bundle.js b/test/js/samples/component-static-immutable2/expected-bundle.js index 572dd7543923..27aa216840e0 100644 --- a/test/js/samples/component-static-immutable2/expected-bundle.js +++ b/test/js/samples/component-static-immutable2/expected-bundle.js @@ -27,34 +27,19 @@ function _differsImmutable(a, b) { return a != a ? b == b : a !== b; } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -63,7 +48,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -73,24 +57,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -132,9 +107,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/component-static/expected-bundle.js b/test/js/samples/component-static/expected-bundle.js index 9895844ac647..91fb33207b16 100644 --- a/test/js/samples/component-static/expected-bundle.js +++ b/test/js/samples/component-static/expected-bundle.js @@ -23,34 +23,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -59,7 +44,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -69,24 +53,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -128,9 +103,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index c2d577187f93..95e448f87489 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -23,34 +23,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -59,7 +44,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -69,24 +53,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -128,9 +103,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 65abf18b539e..79e3e3e456b6 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js index 66d6da680dca..e320ba4218b5 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js @@ -35,34 +35,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -71,7 +56,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -81,24 +65,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -140,9 +115,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/deconflict-builtins/_actual-bundle-v2.js b/test/js/samples/deconflict-builtins/_actual-bundle-v2.js index 6680adfdb427..a8dcb7e5f464 100644 --- a/test/js/samples/deconflict-builtins/_actual-bundle-v2.js +++ b/test/js/samples/deconflict-builtins/_actual-bundle-v2.js @@ -53,34 +53,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -89,7 +74,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -99,24 +83,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -158,9 +133,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/deconflict-builtins/expected-bundle-v2.js b/test/js/samples/deconflict-builtins/expected-bundle-v2.js index 6680adfdb427..a8dcb7e5f464 100644 --- a/test/js/samples/deconflict-builtins/expected-bundle-v2.js +++ b/test/js/samples/deconflict-builtins/expected-bundle-v2.js @@ -53,34 +53,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -89,7 +74,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -99,24 +83,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -158,9 +133,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/deconflict-builtins/expected-bundle.js b/test/js/samples/deconflict-builtins/expected-bundle.js index eac899bc36e9..ee08884e3d1b 100644 --- a/test/js/samples/deconflict-builtins/expected-bundle.js +++ b/test/js/samples/deconflict-builtins/expected-bundle.js @@ -53,34 +53,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -89,7 +74,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -99,24 +83,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -158,9 +133,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/deconflict-globals/expected-bundle.js b/test/js/samples/deconflict-globals/expected-bundle.js index c5967ec8b602..61cc091f93ce 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -23,34 +23,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -59,7 +44,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -69,24 +53,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -128,9 +103,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } @@ -191,7 +166,12 @@ function SvelteComponent(options) { init(this, options); this._state = assign(data_1(), options.data); - var _oncreate = oncreate.bind(this); + var self = this; + var _oncreate = function() { + var initial = { changed: { }, current: self._state, previous: undefined }; + oncreate.call(self); + self.fire("update", initial); + }; if (!options.root) { this._oncreate = []; diff --git a/test/js/samples/deconflict-globals/expected.js b/test/js/samples/deconflict-globals/expected.js index 8e00df8d4a8e..99ef4f5659b4 100644 --- a/test/js/samples/deconflict-globals/expected.js +++ b/test/js/samples/deconflict-globals/expected.js @@ -30,7 +30,12 @@ function SvelteComponent(options) { init(this, options); this._state = assign(data_1(), options.data); - var _oncreate = oncreate.bind(this); + var self = this; + var _oncreate = function() { + var initial = { changed: { }, current: self._state, previous: undefined }; + oncreate.call(self); + self.fire("update", initial); + }; if (!options.root) { this._oncreate = []; diff --git a/test/js/samples/dev-warning-missing-data-computed/_actual-bundle-v2.js b/test/js/samples/dev-warning-missing-data-computed/_actual-bundle-v2.js index 84a148c901d5..5ba9581cf59b 100644 --- a/test/js/samples/dev-warning-missing-data-computed/_actual-bundle-v2.js +++ b/test/js/samples/dev-warning-missing-data-computed/_actual-bundle-v2.js @@ -50,34 +50,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -86,7 +71,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -96,24 +80,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function observeDev(key, callback, options) { @@ -180,9 +155,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/dev-warning-missing-data-computed/expected-bundle-v2.js b/test/js/samples/dev-warning-missing-data-computed/expected-bundle-v2.js index 84a148c901d5..5ba9581cf59b 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected-bundle-v2.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected-bundle-v2.js @@ -50,34 +50,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -86,7 +71,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -96,24 +80,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function observeDev(key, callback, options) { @@ -180,9 +155,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js b/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js index 84a148c901d5..5ba9581cf59b 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js @@ -50,34 +50,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -86,7 +71,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -96,24 +80,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function observeDev(key, callback, options) { @@ -180,9 +155,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/do-use-dataset/_actual-bundle-v2.js b/test/js/samples/do-use-dataset/_actual-bundle-v2.js index 883194110d60..37bc15d30240 100644 --- a/test/js/samples/do-use-dataset/_actual-bundle-v2.js +++ b/test/js/samples/do-use-dataset/_actual-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/do-use-dataset/expected-bundle-v2.js b/test/js/samples/do-use-dataset/expected-bundle-v2.js index 883194110d60..37bc15d30240 100644 --- a/test/js/samples/do-use-dataset/expected-bundle-v2.js +++ b/test/js/samples/do-use-dataset/expected-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/do-use-dataset/expected-bundle.js b/test/js/samples/do-use-dataset/expected-bundle.js index 883194110d60..37bc15d30240 100644 --- a/test/js/samples/do-use-dataset/expected-bundle.js +++ b/test/js/samples/do-use-dataset/expected-bundle.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/dont-use-dataset-in-legacy/_actual-bundle-v2.js b/test/js/samples/dont-use-dataset-in-legacy/_actual-bundle-v2.js index 1e231ec05680..be43fd0e9b4d 100644 --- a/test/js/samples/dont-use-dataset-in-legacy/_actual-bundle-v2.js +++ b/test/js/samples/dont-use-dataset-in-legacy/_actual-bundle-v2.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/dont-use-dataset-in-legacy/expected-bundle-v2.js b/test/js/samples/dont-use-dataset-in-legacy/expected-bundle-v2.js index 1e231ec05680..be43fd0e9b4d 100644 --- a/test/js/samples/dont-use-dataset-in-legacy/expected-bundle-v2.js +++ b/test/js/samples/dont-use-dataset-in-legacy/expected-bundle-v2.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js b/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js index 1e231ec05680..be43fd0e9b4d 100644 --- a/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js +++ b/test/js/samples/dont-use-dataset-in-legacy/expected-bundle.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/dont-use-dataset-in-svg/_actual-bundle-v2.js b/test/js/samples/dont-use-dataset-in-svg/_actual-bundle-v2.js index 0b60ba22e2e4..1092b9ab86de 100644 --- a/test/js/samples/dont-use-dataset-in-svg/_actual-bundle-v2.js +++ b/test/js/samples/dont-use-dataset-in-svg/_actual-bundle-v2.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/dont-use-dataset-in-svg/expected-bundle-v2.js b/test/js/samples/dont-use-dataset-in-svg/expected-bundle-v2.js index 0b60ba22e2e4..1092b9ab86de 100644 --- a/test/js/samples/dont-use-dataset-in-svg/expected-bundle-v2.js +++ b/test/js/samples/dont-use-dataset-in-svg/expected-bundle-v2.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js b/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js index 0b60ba22e2e4..1092b9ab86de 100644 --- a/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js +++ b/test/js/samples/dont-use-dataset-in-svg/expected-bundle.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/each-block-changed-check/_actual-bundle-v2.js b/test/js/samples/each-block-changed-check/_actual-bundle-v2.js index 1ce468ca12c1..f7d0080cbdac 100644 --- a/test/js/samples/each-block-changed-check/_actual-bundle-v2.js +++ b/test/js/samples/each-block-changed-check/_actual-bundle-v2.js @@ -55,34 +55,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -91,7 +76,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -101,24 +85,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -160,9 +135,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/each-block-changed-check/expected-bundle-v2.js b/test/js/samples/each-block-changed-check/expected-bundle-v2.js index 1ce468ca12c1..f7d0080cbdac 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle-v2.js +++ b/test/js/samples/each-block-changed-check/expected-bundle-v2.js @@ -55,34 +55,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -91,7 +76,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -101,24 +85,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -160,9 +135,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index 75814833836b..a3023dc6b1b6 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -55,34 +55,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -91,7 +76,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -101,24 +85,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -160,9 +135,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index 7c17164c744c..196f0e1c942f 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -35,34 +35,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -71,7 +56,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -81,24 +65,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -140,9 +115,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/head-no-whitespace/_actual-bundle-v2.js b/test/js/samples/head-no-whitespace/_actual-bundle-v2.js index 396cf8fb8c55..e17d8ede6182 100644 --- a/test/js/samples/head-no-whitespace/_actual-bundle-v2.js +++ b/test/js/samples/head-no-whitespace/_actual-bundle-v2.js @@ -35,34 +35,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -71,7 +56,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -81,24 +65,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -140,9 +115,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/head-no-whitespace/expected-bundle-v2.js b/test/js/samples/head-no-whitespace/expected-bundle-v2.js index 396cf8fb8c55..e17d8ede6182 100644 --- a/test/js/samples/head-no-whitespace/expected-bundle-v2.js +++ b/test/js/samples/head-no-whitespace/expected-bundle-v2.js @@ -35,34 +35,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -71,7 +56,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -81,24 +65,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -140,9 +115,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/head-no-whitespace/expected-bundle.js b/test/js/samples/head-no-whitespace/expected-bundle.js index 396cf8fb8c55..e17d8ede6182 100644 --- a/test/js/samples/head-no-whitespace/expected-bundle.js +++ b/test/js/samples/head-no-whitespace/expected-bundle.js @@ -35,34 +35,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -71,7 +56,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -81,24 +65,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -140,9 +115,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/if-block-no-update/_actual-bundle-v2.js b/test/js/samples/if-block-no-update/_actual-bundle-v2.js index 29d151e15d64..5569d332f8a6 100644 --- a/test/js/samples/if-block-no-update/_actual-bundle-v2.js +++ b/test/js/samples/if-block-no-update/_actual-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/if-block-no-update/expected-bundle-v2.js b/test/js/samples/if-block-no-update/expected-bundle-v2.js index 29d151e15d64..5569d332f8a6 100644 --- a/test/js/samples/if-block-no-update/expected-bundle-v2.js +++ b/test/js/samples/if-block-no-update/expected-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index 59d3e9656b5e..c48994fc3bac 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/if-block-simple/_actual-bundle-v2.js b/test/js/samples/if-block-simple/_actual-bundle-v2.js index 72dbdec8897c..336cd0bc5d15 100644 --- a/test/js/samples/if-block-simple/_actual-bundle-v2.js +++ b/test/js/samples/if-block-simple/_actual-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/if-block-simple/expected-bundle-v2.js b/test/js/samples/if-block-simple/expected-bundle-v2.js index 72dbdec8897c..336cd0bc5d15 100644 --- a/test/js/samples/if-block-simple/expected-bundle-v2.js +++ b/test/js/samples/if-block-simple/expected-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index 1c0f7f3fae10..47065fea5866 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized-multiple/_actual-bundle-v2.js b/test/js/samples/inline-style-optimized-multiple/_actual-bundle-v2.js index eb528e4ebf0d..035b3d9414a6 100644 --- a/test/js/samples/inline-style-optimized-multiple/_actual-bundle-v2.js +++ b/test/js/samples/inline-style-optimized-multiple/_actual-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized-multiple/expected-bundle-v2.js b/test/js/samples/inline-style-optimized-multiple/expected-bundle-v2.js index eb528e4ebf0d..035b3d9414a6 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle-v2.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js index eb528e4ebf0d..035b3d9414a6 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized-url/_actual-bundle-v2.js b/test/js/samples/inline-style-optimized-url/_actual-bundle-v2.js index a4d2dda6696e..b08091489f6e 100644 --- a/test/js/samples/inline-style-optimized-url/_actual-bundle-v2.js +++ b/test/js/samples/inline-style-optimized-url/_actual-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized-url/expected-bundle-v2.js b/test/js/samples/inline-style-optimized-url/expected-bundle-v2.js index a4d2dda6696e..b08091489f6e 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle-v2.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized-url/expected-bundle.js b/test/js/samples/inline-style-optimized-url/expected-bundle.js index a4d2dda6696e..b08091489f6e 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized/_actual-bundle-v2.js b/test/js/samples/inline-style-optimized/_actual-bundle-v2.js index 2b78d3932a8a..fcd458c35683 100644 --- a/test/js/samples/inline-style-optimized/_actual-bundle-v2.js +++ b/test/js/samples/inline-style-optimized/_actual-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized/expected-bundle-v2.js b/test/js/samples/inline-style-optimized/expected-bundle-v2.js index 2b78d3932a8a..fcd458c35683 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle-v2.js +++ b/test/js/samples/inline-style-optimized/expected-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-optimized/expected-bundle.js b/test/js/samples/inline-style-optimized/expected-bundle.js index 2b78d3932a8a..fcd458c35683 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle.js +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-unoptimized/_actual-bundle-v2.js b/test/js/samples/inline-style-unoptimized/_actual-bundle-v2.js index 670a2c02f64a..f7034d900720 100644 --- a/test/js/samples/inline-style-unoptimized/_actual-bundle-v2.js +++ b/test/js/samples/inline-style-unoptimized/_actual-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-unoptimized/expected-bundle-v2.js b/test/js/samples/inline-style-unoptimized/expected-bundle-v2.js index 670a2c02f64a..f7034d900720 100644 --- a/test/js/samples/inline-style-unoptimized/expected-bundle-v2.js +++ b/test/js/samples/inline-style-unoptimized/expected-bundle-v2.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/inline-style-unoptimized/expected-bundle.js b/test/js/samples/inline-style-unoptimized/expected-bundle.js index 670a2c02f64a..f7034d900720 100644 --- a/test/js/samples/inline-style-unoptimized/expected-bundle.js +++ b/test/js/samples/inline-style-unoptimized/expected-bundle.js @@ -39,34 +39,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -75,7 +60,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -85,24 +69,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -144,9 +119,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/input-without-blowback-guard/expected-bundle.js b/test/js/samples/input-without-blowback-guard/expected-bundle.js index 3a0d2ab0e5f0..4b8909150b99 100644 --- a/test/js/samples/input-without-blowback-guard/expected-bundle.js +++ b/test/js/samples/input-without-blowback-guard/expected-bundle.js @@ -47,34 +47,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -83,7 +68,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -93,24 +77,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -152,9 +127,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/legacy-default/expected-bundle.js b/test/js/samples/legacy-default/expected-bundle.js index 38455aa8bc2f..b400d303d7c7 100644 --- a/test/js/samples/legacy-default/expected-bundle.js +++ b/test/js/samples/legacy-default/expected-bundle.js @@ -57,34 +57,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -93,7 +78,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -103,24 +87,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -162,9 +137,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/legacy-input-type/expected-bundle.js b/test/js/samples/legacy-input-type/expected-bundle.js index 9c478dd3d9b2..ff4b0b5e2b23 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -41,34 +41,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -77,7 +62,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -87,24 +71,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -146,9 +121,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/legacy-quote-class/expected-bundle.js b/test/js/samples/legacy-quote-class/expected-bundle.js index 84afafca0255..fc088bf34a55 100644 --- a/test/js/samples/legacy-quote-class/expected-bundle.js +++ b/test/js/samples/legacy-quote-class/expected-bundle.js @@ -58,34 +58,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -94,7 +79,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -104,24 +88,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -163,9 +138,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/media-bindings/expected-bundle.js b/test/js/samples/media-bindings/expected-bundle.js index b4479b8ff1f3..7d8c7624eab7 100644 --- a/test/js/samples/media-bindings/expected-bundle.js +++ b/test/js/samples/media-bindings/expected-bundle.js @@ -51,34 +51,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -87,7 +72,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -97,24 +81,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -156,9 +131,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index 47366ed109ab..06d1b0d4ea60 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -37,34 +37,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -73,7 +58,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -83,24 +67,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -142,9 +117,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index 87e038ea8dd2..28b9ab51ef74 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -23,34 +23,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -59,7 +44,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -69,24 +53,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -128,9 +103,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } @@ -186,7 +161,12 @@ function SvelteComponent(options) { this._handlers.destroy = [ondestroy]; - var _oncreate = oncreate.bind(this); + var self = this; + var _oncreate = function() { + var initial = { changed: { }, current: self._state, previous: undefined }; + oncreate.call(self); + self.fire("update", initial); + }; if (!options.root) { this._oncreate = []; diff --git a/test/js/samples/onrender-onteardown-rewritten/expected.js b/test/js/samples/onrender-onteardown-rewritten/expected.js index 1d91737faadf..0de4ce702fe4 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected.js @@ -26,7 +26,12 @@ function SvelteComponent(options) { this._handlers.destroy = [ondestroy]; - var _oncreate = oncreate.bind(this); + var self = this; + var _oncreate = function() { + var initial = { changed: { }, current: self._state, previous: undefined }; + oncreate.call(self); + self.fire("update", initial); + }; if (!options.root) { this._oncreate = []; diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index 3a002059aad6..8e8036f6e797 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -23,34 +23,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -59,7 +44,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -69,24 +53,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -128,9 +103,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/svg-title/expected-bundle.js b/test/js/samples/svg-title/expected-bundle.js index 837d81e0e295..804ce01d7c45 100644 --- a/test/js/samples/svg-title/expected-bundle.js +++ b/test/js/samples/svg-title/expected-bundle.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/title/_actual-bundle-v2.js b/test/js/samples/title/_actual-bundle-v2.js index 83c8815d6fbd..df4a5e21253d 100644 --- a/test/js/samples/title/_actual-bundle-v2.js +++ b/test/js/samples/title/_actual-bundle-v2.js @@ -23,34 +23,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -59,7 +44,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -69,24 +53,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -128,9 +103,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/title/expected-bundle-v2.js b/test/js/samples/title/expected-bundle-v2.js index 83c8815d6fbd..df4a5e21253d 100644 --- a/test/js/samples/title/expected-bundle-v2.js +++ b/test/js/samples/title/expected-bundle-v2.js @@ -23,34 +23,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -59,7 +44,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -69,24 +53,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -128,9 +103,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/title/expected-bundle.js b/test/js/samples/title/expected-bundle.js index 83c8815d6fbd..df4a5e21253d 100644 --- a/test/js/samples/title/expected-bundle.js +++ b/test/js/samples/title/expected-bundle.js @@ -23,34 +23,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -59,7 +44,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -69,24 +53,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -128,9 +103,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/use-elements-as-anchors/_actual-bundle-v2.js b/test/js/samples/use-elements-as-anchors/_actual-bundle-v2.js index 7aefaab6c8c4..5fe8f188a29a 100644 --- a/test/js/samples/use-elements-as-anchors/_actual-bundle-v2.js +++ b/test/js/samples/use-elements-as-anchors/_actual-bundle-v2.js @@ -47,34 +47,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -83,7 +68,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -93,24 +77,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -152,9 +127,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle-v2.js b/test/js/samples/use-elements-as-anchors/expected-bundle-v2.js index 7aefaab6c8c4..5fe8f188a29a 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle-v2.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle-v2.js @@ -47,34 +47,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -83,7 +68,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -93,24 +77,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -152,9 +127,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index 722574e06329..9ac8ca4ebeec 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -47,34 +47,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -83,7 +68,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -93,24 +77,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -152,9 +127,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/window-binding-scroll/_actual-bundle-v2.js b/test/js/samples/window-binding-scroll/_actual-bundle-v2.js index a98de2af6353..a6afbf9b14aa 100644 --- a/test/js/samples/window-binding-scroll/_actual-bundle-v2.js +++ b/test/js/samples/window-binding-scroll/_actual-bundle-v2.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/window-binding-scroll/expected-bundle-v2.js b/test/js/samples/window-binding-scroll/expected-bundle-v2.js index a98de2af6353..a6afbf9b14aa 100644 --- a/test/js/samples/window-binding-scroll/expected-bundle-v2.js +++ b/test/js/samples/window-binding-scroll/expected-bundle-v2.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/js/samples/window-binding-scroll/expected-bundle.js b/test/js/samples/window-binding-scroll/expected-bundle.js index a98de2af6353..a6afbf9b14aa 100644 --- a/test/js/samples/window-binding-scroll/expected-bundle.js +++ b/test/js/samples/window-binding-scroll/expected-bundle.js @@ -43,34 +43,19 @@ function _differs(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } -function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); if (!handlers) return; for (var i = 0; i < handlers.length; i += 1) { - handlers[i].call(this, data); + var handler = handlers[i]; + + if (!handler.__calling) { + handler.__calling = true; + handler.call(this, data); + handler.__calling = false; + } } } @@ -79,7 +64,6 @@ function get(key) { } function init(component, options) { - component._observers = { pre: blankObject(), post: blankObject() }; component._handlers = blankObject(); component._bind = options._bind; @@ -89,24 +73,15 @@ function init(component, options) { } function observe(key, callback, options) { - var group = options && options.defer - ? this._observers.post - : this._observers.pre; - - (group[key] || (group[key] = [])).push(callback); + var fn = callback.bind(this); if (!options || options.init !== false) { - callback.__calling = true; - callback.call(this, this._state[key]); - callback.__calling = false; + fn(this.get()[key], undefined); } - return { - cancel: function() { - var index = group[key].indexOf(callback); - if (~index) group[key].splice(index, 1); - } - }; + return this.on(options && options.defer ? 'update' : 'state', function(event) { + if (event.changed[key]) fn(event.current[key], event.previous && event.previous[key]); + }); } function on(eventName, handler) { @@ -148,9 +123,9 @@ function _set(newState) { if (this._bind) this._bind(changed, this._state); if (this._fragment) { - dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this.fire("state", { changed: changed, current: this._state, previous: oldState }); this._fragment.p(changed, this._state); - dispatchObservers(this, this._observers.post, changed, this._state, oldState); + this.fire("update", { changed: changed, current: this._state, previous: oldState }); } } diff --git a/test/runtime/samples/onstate-before-oncreate/_config.js b/test/runtime/samples/onstate-before-oncreate/_config.js new file mode 100644 index 000000000000..ff2b87b71228 --- /dev/null +++ b/test/runtime/samples/onstate-before-oncreate/_config.js @@ -0,0 +1,8 @@ +export default { + 'skip-ssr': true, + + test(assert, component, target) { + assert.ok(component.onstateRanBeforeOncreate); + assert.ok(!component.onupdateRanBeforeOncreate); + } +}; diff --git a/test/runtime/samples/onstate-before-oncreate/main.html b/test/runtime/samples/onstate-before-oncreate/main.html new file mode 100644 index 000000000000..9d4aa980ea81 --- /dev/null +++ b/test/runtime/samples/onstate-before-oncreate/main.html @@ -0,0 +1,16 @@ + \ No newline at end of file diff --git a/test/runtime/samples/onstate-event/_config.js b/test/runtime/samples/onstate-event/_config.js new file mode 100644 index 000000000000..d3f24376f9ed --- /dev/null +++ b/test/runtime/samples/onstate-event/_config.js @@ -0,0 +1,49 @@ +export default { + 'skip-ssr': true, + + data: { + foo: 'woo!' + }, + + html: ` +

woo!

+

undefined

+ `, + + test(assert, component, target) { + const history = []; + + component.on('state', ({ changed, current, previous }) => { + history.push({ changed, current, previous }); + component.set({ bar: current.foo.toUpperCase() }); + }); + + component.set({ foo: 'yeah!' }); + assert.htmlEqual(target.innerHTML, ` +

yeah!

+

YEAH!

+ `); + + component.set({ unused: 'x' }); + + assert.deepEqual(history, [ + { + changed: { foo: true }, + current: { foo: 'yeah!' }, + previous: { foo: 'woo!' } + }, + // this is NOT received, because Svelte will not allow + // an event handler to trigger itself recursively + // { + // changed: { bar: true }, + // current: { foo: 'yeah!', bar: 'YEAH!' }, + // previous: { foo: 'yeah!' } + // }, + { + changed: { unused: true }, + current: { foo: 'yeah!', bar: 'YEAH!', unused: 'x' }, + previous: { foo: 'yeah!', bar: 'YEAH!' } + } + ]); + } +}; diff --git a/test/runtime/samples/onstate-event/main.html b/test/runtime/samples/onstate-event/main.html new file mode 100644 index 000000000000..3699be60dbb6 --- /dev/null +++ b/test/runtime/samples/onstate-event/main.html @@ -0,0 +1,2 @@ +

{{foo}}

+

{{bar}}

\ No newline at end of file diff --git a/test/runtime/samples/onstate/_config.js b/test/runtime/samples/onstate/_config.js new file mode 100644 index 000000000000..48712bb41f22 --- /dev/null +++ b/test/runtime/samples/onstate/_config.js @@ -0,0 +1,20 @@ +export default { + 'skip-ssr': true, + + data: { + foo: 'woo!' + }, + + html: ` +

woo!

+

WOO!

+ `, + + test(assert, component, target) { + component.set({ foo: 'yeah!' }); + assert.htmlEqual(target.innerHTML, ` +

yeah!

+

YEAH!

+ `); + } +}; diff --git a/test/runtime/samples/onstate/main.html b/test/runtime/samples/onstate/main.html new file mode 100644 index 000000000000..5e2cbaee8c3d --- /dev/null +++ b/test/runtime/samples/onstate/main.html @@ -0,0 +1,10 @@ +

{{foo}}

+

{{bar}}

+ + \ No newline at end of file diff --git a/test/runtime/samples/onupdate/_config.js b/test/runtime/samples/onupdate/_config.js index 1603c5a74a12..41e604193794 100644 --- a/test/runtime/samples/onupdate/_config.js +++ b/test/runtime/samples/onupdate/_config.js @@ -1,5 +1,4 @@ export default { - solo: true, 'skip-ssr': true, data: { diff --git a/test/validator/samples/onstate-arrow-this/errors.json b/test/validator/samples/onstate-arrow-this/errors.json index cfcba87f4e19..c1788d56de94 100644 --- a/test/validator/samples/onstate-arrow-this/errors.json +++ b/test/validator/samples/onstate-arrow-this/errors.json @@ -1,4 +1,5 @@ [{ + "code": "invalid-onstate-property", "message": "'onstate' should be a function expression, not an arrow function expression", "pos": 29, "loc": { diff --git a/test/validator/samples/onupdate-arrow-this/errors.json b/test/validator/samples/onupdate-arrow-this/errors.json index e4c1a3a44140..b4b1b8231e93 100644 --- a/test/validator/samples/onupdate-arrow-this/errors.json +++ b/test/validator/samples/onupdate-arrow-this/errors.json @@ -1,4 +1,5 @@ [{ + "code": "invalid-onupdate-property", "message": "'onupdate' should be a function expression, not an arrow function expression", "pos": 29, "loc": { From 9a9da249ce69449f37f8e2de4d3671c901c2012d Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Apr 2018 13:15:32 -0400 Subject: [PATCH 3/8] remove unused dispatchObservers --- src/shared/index.js | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/shared/index.js b/src/shared/index.js index 5beb57cecae6..3d3e8b56d0dc 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -35,27 +35,6 @@ export function _differsImmutable(a, b) { return a != a ? b == b : a !== b; } -export function dispatchObservers(component, group, changed, newState, oldState) { - for (var key in group) { - if (!changed[key]) continue; - - var newValue = newState[key]; - var oldValue = oldState[key]; - - var callbacks = group[key]; - if (!callbacks) continue; - - for (var i = 0; i < callbacks.length; i += 1) { - var callback = callbacks[i]; - if (callback.__calling) continue; - - callback.__calling = true; - callback.call(component, newValue, oldValue); - callback.__calling = false; - } - } -} - export function fire(eventName, data) { var handlers = eventName in this._handlers && this._handlers[eventName].slice(); From 2a43fe94b35db1df5cfce765015aae80cfda6aa3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Apr 2018 13:27:37 -0400 Subject: [PATCH 4/8] deprecate observe --- src/shared/index.js | 2 ++ .../dev-warning-missing-data-computed/_actual-bundle-v2.js | 2 ++ .../dev-warning-missing-data-computed/expected-bundle-v2.js | 2 ++ .../dev-warning-missing-data-computed/expected-bundle.js | 2 ++ 4 files changed, 8 insertions(+) diff --git a/src/shared/index.js b/src/shared/index.js index 3d3e8b56d0dc..3ff0a2d421c3 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -77,6 +77,8 @@ export function observe(key, callback, options) { } export function observeDev(key, callback, options) { + console.warn("this.observe(key, (newValue, oldValue) => {...}) is deprecated. Use\n\n // runs before DOM updates\n this.on('state', ({ changed, current, previous }) => {...});\n\n // runs after DOM updates\n this.on('update', ...);\n\n...or add the observe method from the svelte-extras package"); + var c = (key = '' + key).search(/[.[]/); if (c > -1) { var message = diff --git a/test/js/samples/dev-warning-missing-data-computed/_actual-bundle-v2.js b/test/js/samples/dev-warning-missing-data-computed/_actual-bundle-v2.js index 5ba9581cf59b..b580ce115577 100644 --- a/test/js/samples/dev-warning-missing-data-computed/_actual-bundle-v2.js +++ b/test/js/samples/dev-warning-missing-data-computed/_actual-bundle-v2.js @@ -92,6 +92,8 @@ function observe(key, callback, options) { } function observeDev(key, callback, options) { + console.warn("this.observe(key, (newValue, oldValue) => {...}) is deprecated. Use\n\n // runs before DOM updates\n this.on('state', ({ changed, current, previous }) => {...});\n\n // runs after DOM updates\n this.on('update', ...);\n\n...or add the observe method from the svelte-extras package"); + var c = (key = '' + key).search(/[.[]/); if (c > -1) { var message = diff --git a/test/js/samples/dev-warning-missing-data-computed/expected-bundle-v2.js b/test/js/samples/dev-warning-missing-data-computed/expected-bundle-v2.js index 5ba9581cf59b..b580ce115577 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected-bundle-v2.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected-bundle-v2.js @@ -92,6 +92,8 @@ function observe(key, callback, options) { } function observeDev(key, callback, options) { + console.warn("this.observe(key, (newValue, oldValue) => {...}) is deprecated. Use\n\n // runs before DOM updates\n this.on('state', ({ changed, current, previous }) => {...});\n\n // runs after DOM updates\n this.on('update', ...);\n\n...or add the observe method from the svelte-extras package"); + var c = (key = '' + key).search(/[.[]/); if (c > -1) { var message = diff --git a/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js b/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js index 5ba9581cf59b..b580ce115577 100644 --- a/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js +++ b/test/js/samples/dev-warning-missing-data-computed/expected-bundle.js @@ -92,6 +92,8 @@ function observe(key, callback, options) { } function observeDev(key, callback, options) { + console.warn("this.observe(key, (newValue, oldValue) => {...}) is deprecated. Use\n\n // runs before DOM updates\n this.on('state', ({ changed, current, previous }) => {...});\n\n // runs after DOM updates\n this.on('update', ...);\n\n...or add the observe method from the svelte-extras package"); + var c = (key = '' + key).search(/[.[]/); if (c > -1) { var message = From 20b940a73a4573484f072833ecaf375361bd99dc Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Apr 2018 13:30:16 -0400 Subject: [PATCH 5/8] deprecate store.onchange --- store.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/store.js b/store.js index 1447c78eaa0b..2bf08dcf7e75 100644 --- a/store.js +++ b/store.js @@ -118,6 +118,8 @@ assign(Store.prototype, { onchange: function(callback) { // TODO remove this method + console.warn("store.onchange is deprecated in favour of store.on('state', event => {...})"); + return this.on('state', function(event) { callback(event.current, event.changed); }); From 43ea6bdf28d20754d125b60546ed1465059e4fe3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Apr 2018 13:31:58 -0400 Subject: [PATCH 6/8] add note to self --- src/generators/nodes/Component.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index 25d7508ac223..7f441c024f3f 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -275,6 +275,7 @@ export default class Component extends Node { hasStoreBindings && 'newStoreState = {}', ].filter(Boolean).join(', '); + // TODO use component.on('state', ...) instead of _bind componentInitProperties.push(deindent` _bind: function(changed, childState) { var ${initialisers}; From 6f4de8f237ebff3d02d52a3c3bada62d1179cabd Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Apr 2018 14:01:03 -0400 Subject: [PATCH 7/8] prevent double-firing of update on init --- src/generators/dom/index.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 4de05981afaa..e83fd5701a75 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -224,11 +224,10 @@ export default function dom( ${hasInitHooks && deindent` var self = this; var _oncreate = function() { - var initial = { changed: { ${expectedProperties.map(p => `${p}: 1`).join(', ')} }, current: self._state, previous: undefined }; - ${templateProperties.onstate && `%onstate.call(self, initial);`} + var changed = { ${expectedProperties.map(p => `${p}: 1`).join(', ')} }; + ${templateProperties.onstate && `%onstate.call(self, { changed: changed, current: self._state });`} ${templateProperties.oncreate && `%oncreate.call(self);`} - ${templateProperties.onupdate && `%onupdate.call(self, initial);`} - self.fire("update", initial); + self.fire("update", { changed: changed, current: self._state }); }; `} From 3d8c768c819f86e9c0da875abe8c96a3955504b4 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Apr 2018 14:12:58 -0400 Subject: [PATCH 8/8] update tests --- test/js/samples/deconflict-globals/expected-bundle.js | 4 ++-- test/js/samples/deconflict-globals/expected.js | 4 ++-- .../samples/onrender-onteardown-rewritten/expected-bundle.js | 4 ++-- test/js/samples/onrender-onteardown-rewritten/expected.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/js/samples/deconflict-globals/expected-bundle.js b/test/js/samples/deconflict-globals/expected-bundle.js index 61cc091f93ce..17898cf41485 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -168,9 +168,9 @@ function SvelteComponent(options) { var self = this; var _oncreate = function() { - var initial = { changed: { }, current: self._state, previous: undefined }; + var changed = { }; oncreate.call(self); - self.fire("update", initial); + self.fire("update", { changed: changed, current: self._state }); }; if (!options.root) { diff --git a/test/js/samples/deconflict-globals/expected.js b/test/js/samples/deconflict-globals/expected.js index 99ef4f5659b4..91192f4ccaa3 100644 --- a/test/js/samples/deconflict-globals/expected.js +++ b/test/js/samples/deconflict-globals/expected.js @@ -32,9 +32,9 @@ function SvelteComponent(options) { var self = this; var _oncreate = function() { - var initial = { changed: { }, current: self._state, previous: undefined }; + var changed = { }; oncreate.call(self); - self.fire("update", initial); + self.fire("update", { changed: changed, current: self._state }); }; if (!options.root) { diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index 28b9ab51ef74..4b1a9fc4812e 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -163,9 +163,9 @@ function SvelteComponent(options) { var self = this; var _oncreate = function() { - var initial = { changed: { }, current: self._state, previous: undefined }; + var changed = { }; oncreate.call(self); - self.fire("update", initial); + self.fire("update", { changed: changed, current: self._state }); }; if (!options.root) { diff --git a/test/js/samples/onrender-onteardown-rewritten/expected.js b/test/js/samples/onrender-onteardown-rewritten/expected.js index 0de4ce702fe4..fed341a7f96e 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected.js @@ -28,9 +28,9 @@ function SvelteComponent(options) { var self = this; var _oncreate = function() { - var initial = { changed: { }, current: self._state, previous: undefined }; + var changed = { }; oncreate.call(self); - self.fire("update", initial); + self.fire("update", { changed: changed, current: self._state }); }; if (!options.root) {