diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 2f88185fff..1d25c4bcfc 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -123,7 +123,7 @@ export default function dom( const condition = `${deps.map(dep => `changed.${dep}`).join(' || ')}`; - const statement = `if (@differs(state.${key}, (state.${key} = %computed-${key}(${deps + const statement = `if (this._differs(state.${key}, (state.${key} = %computed-${key}(${deps .map(dep => `state.${dep}`) .join(', ')})))) changed.${key} = true;`; @@ -174,7 +174,7 @@ export default function dom( ? `@proto` : deindent` { - ${['destroy', 'get', 'fire', 'observe', 'on', 'set', 'teardown', '_set', '_mount', '_unmount'] + ${['destroy', 'get', 'fire', 'observe', 'on', 'set', 'teardown', '_set', '_mount', '_unmount', '_differs'] .map(n => `${n}: @${n === 'teardown' ? 'destroy' : n}`) .join(',\n')} }`; @@ -360,6 +360,8 @@ export default function dom( `); } + const immutable = templateProperties.immutable ? templateProperties.immutable.value.value : options.immutable; + builder.addBlock(deindent` ${options.dev && deindent` ${name}.prototype._checkReadOnly = function _checkReadOnly(newState) { @@ -379,6 +381,8 @@ export default function dom( ${templateProperties.setup && `%setup(${name});`} ${templateProperties.preload && `${name}.preload = %preload;`} + + ${immutable && `${name}.prototype._differs = @_differsImmutable;`} `); const usedHelpers = new Set(); diff --git a/src/interfaces.ts b/src/interfaces.ts index 9773d330d3..82de8cd047 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -52,6 +52,7 @@ export interface CompileOptions { cssOutputFilename?: string; dev?: boolean; + immutable?: boolean; shared?: boolean | string; cascade?: boolean; hydratable?: boolean; diff --git a/src/shared/index.js b/src/shared/index.js index ef160ebf07..cf953e4bf1 100644 --- a/src/shared/index.js +++ b/src/shared/index.js @@ -25,8 +25,12 @@ export function destroyDev(detach) { }; } -export function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +export function _differs(a, b) { + return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +} + +export function _differsImmutable(a, b) { + return a != a ? b == b : a !== b; } export function dispatchObservers(component, group, changed, newState, oldState) { @@ -150,7 +154,7 @@ export function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -211,7 +215,8 @@ export var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; export var protoDev = { @@ -225,5 +230,6 @@ export var protoDev = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; diff --git a/src/validate/js/propValidators/immutable.ts b/src/validate/js/propValidators/immutable.ts new file mode 100644 index 0000000000..b54521d9e4 --- /dev/null +++ b/src/validate/js/propValidators/immutable.ts @@ -0,0 +1,11 @@ +import { Validator } from '../../'; +import { Node } from '../../../interfaces'; + +export default function immutable(validator: Validator, prop: Node) { + if (prop.value.type !== 'Literal' || typeof prop.value.value !== 'boolean') { + validator.error( + `'immutable' must be a boolean literal`, + prop.value.start + ); + } +} diff --git a/src/validate/js/propValidators/index.ts b/src/validate/js/propValidators/index.ts index 942c757601..05819b0837 100644 --- a/src/validate/js/propValidators/index.ts +++ b/src/validate/js/propValidators/index.ts @@ -15,6 +15,7 @@ import tag from './tag'; import transitions from './transitions'; import setup from './setup'; import store from './store'; +import immutable from './immutable'; export default { data, @@ -34,4 +35,5 @@ export default { transitions, setup, store, + immutable, }; diff --git a/store.js b/store.js index f31c659080..63a99cca05 100644 --- a/store.js +++ b/store.js @@ -1,13 +1,14 @@ import { assign, blankObject, - differs, + _differs, + _differsImmutable, dispatchObservers, get, observe } from './shared.js'; -function Store(state) { +function Store(state, options) { this._observers = { pre: blankObject(), post: blankObject() }; this._changeHandlers = []; this._dependents = []; @@ -16,6 +17,7 @@ function Store(state) { this._sortedComputedProperties = []; this._state = assign({}, state); + this._differs = options && options.immutable ? _differsImmutable : _differs; } assign(Store.prototype, { @@ -88,7 +90,7 @@ assign(Store.prototype, { if (dirty) { var newValue = fn.apply(null, values); - if (differs(newValue, value)) { + if (store._differs(newValue, value)) { value = newValue; changed[key] = true; state[key] = value; @@ -124,7 +126,7 @@ assign(Store.prototype, { for (var key in newState) { if (this._computed[key]) throw new Error("'" + key + "' is a read-only property"); - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; 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 33854eb0e6..8520eae5a7 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -51,8 +51,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -151,7 +151,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -189,7 +189,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/component-static-immutable/_config.js b/test/js/samples/component-static-immutable/_config.js new file mode 100644 index 0000000000..06713941e0 --- /dev/null +++ b/test/js/samples/component-static-immutable/_config.js @@ -0,0 +1,5 @@ +export default { + options: { + immutable: true + } +}; \ No newline at end of file diff --git a/test/js/samples/component-static-immutable/expected-bundle.js b/test/js/samples/component-static-immutable/expected-bundle.js new file mode 100644 index 0000000000..18ca967e92 --- /dev/null +++ b/test/js/samples/component-static-immutable/expected-bundle.js @@ -0,0 +1,235 @@ +function noop() {} + +function assign(target) { + var k, + source, + i = 1, + len = arguments.length; + for (; i < len; i++) { + source = arguments[i]; + for (k in source) target[k] = source[k]; + } + + return target; +} + +function blankObject() { + return Object.create(null); +} + +function destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; + + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; +} + +function _differs(a, b) { + return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +} + +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); + } +} + +function get(key) { + return key ? this._state[key] : this._state; +} + +function init(component, options) { + component._observers = { pre: blankObject(), post: blankObject() }; + component._handlers = blankObject(); + component._bind = options._bind; + + component.options = options; + component.root = options.root || component; + component.store = component.root.store || options.store; +} + +function observe(key, callback, options) { + var group = options && options.defer + ? this._observers.post + : this._observers.pre; + + (group[key] || (group[key] = [])).push(callback); + + if (!options || options.init !== false) { + callback.__calling = true; + callback.call(this, this._state[key]); + callback.__calling = false; + } + + return { + cancel: function() { + var index = group[key].indexOf(callback); + if (~index) group[key].splice(index, 1); + } + }; +} + +function on(eventName, handler) { + if (eventName === 'teardown') return this.on('destroy', handler); + + var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + var index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; +} + +function set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; +} + +function _set(newState) { + var oldState = this._state, + changed = {}, + dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + } + if (!dirty) return; + + this._state = assign({}, oldState, newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this._fragment.p(changed, this._state); + dispatchObservers(this, this._observers.post, changed, this._state, oldState); + } +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + +function _mount(target, anchor) { + this._fragment.m(target, anchor); +} + +function _unmount() { + if (this._fragment) this._fragment.u(); +} + +var proto = { + destroy: destroy, + get: get, + fire: fire, + observe: observe, + on: on, + set: set, + teardown: destroy, + _recompute: noop, + _set: _set, + _mount: _mount, + _unmount: _unmount, + _differs: _differs +}; + +/* generated by Svelte vX.Y.Z */ +var Nested = window.Nested; + +function create_main_fragment(state, component) { + + var nested = new Nested({ + root: component.root, + data: { foo: "bar" } + }); + + return { + c: function create() { + nested._fragment.c(); + }, + + m: function mount(target, anchor) { + nested._mount(target, anchor); + }, + + p: noop, + + u: function unmount() { + nested._unmount(); + }, + + d: function destroy$$1() { + nested.destroy(false); + } + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this._fragment = create_main_fragment(this._state, this); + + if (options.target) { + this._fragment.c(); + this._fragment.m(options.target, options.anchor || null); + + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } +} + +assign(SvelteComponent.prototype, proto); + +SvelteComponent.prototype._differs = _differsImmutable; + +export default SvelteComponent; diff --git a/test/js/samples/component-static-immutable/expected.js b/test/js/samples/component-static-immutable/expected.js new file mode 100644 index 0000000000..028dc9e64c --- /dev/null +++ b/test/js/samples/component-static-immutable/expected.js @@ -0,0 +1,61 @@ +/* generated by Svelte vX.Y.Z */ +import { _differsImmutable, assign, callAll, init, noop, proto } from "svelte/shared.js"; + +var Nested = window.Nested; + +function create_main_fragment(state, component) { + + var nested = new Nested({ + root: component.root, + data: { foo: "bar" } + }); + + return { + c: function create() { + nested._fragment.c(); + }, + + m: function mount(target, anchor) { + nested._mount(target, anchor); + }, + + p: noop, + + u: function unmount() { + nested._unmount(); + }, + + d: function destroy() { + nested.destroy(false); + } + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this._fragment = create_main_fragment(this._state, this); + + if (options.target) { + this._fragment.c(); + this._fragment.m(options.target, options.anchor || null); + + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } +} + +assign(SvelteComponent.prototype, proto); + +SvelteComponent.prototype._differs = _differsImmutable; +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/component-static-immutable/input.html b/test/js/samples/component-static-immutable/input.html new file mode 100644 index 0000000000..df6c82ea81 --- /dev/null +++ b/test/js/samples/component-static-immutable/input.html @@ -0,0 +1,10 @@ + + + \ No newline at end of file diff --git a/test/js/samples/component-static-immutable2/_config.js b/test/js/samples/component-static-immutable2/_config.js new file mode 100644 index 0000000000..06713941e0 --- /dev/null +++ b/test/js/samples/component-static-immutable2/_config.js @@ -0,0 +1,5 @@ +export default { + options: { + immutable: true + } +}; \ No newline at end of file diff --git a/test/js/samples/component-static-immutable2/expected-bundle.js b/test/js/samples/component-static-immutable2/expected-bundle.js new file mode 100644 index 0000000000..18ca967e92 --- /dev/null +++ b/test/js/samples/component-static-immutable2/expected-bundle.js @@ -0,0 +1,235 @@ +function noop() {} + +function assign(target) { + var k, + source, + i = 1, + len = arguments.length; + for (; i < len; i++) { + source = arguments[i]; + for (k in source) target[k] = source[k]; + } + + return target; +} + +function blankObject() { + return Object.create(null); +} + +function destroy(detach) { + this.destroy = noop; + this.fire('destroy'); + this.set = this.get = noop; + + if (detach !== false) this._fragment.u(); + this._fragment.d(); + this._fragment = this._state = null; +} + +function _differs(a, b) { + return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +} + +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); + } +} + +function get(key) { + return key ? this._state[key] : this._state; +} + +function init(component, options) { + component._observers = { pre: blankObject(), post: blankObject() }; + component._handlers = blankObject(); + component._bind = options._bind; + + component.options = options; + component.root = options.root || component; + component.store = component.root.store || options.store; +} + +function observe(key, callback, options) { + var group = options && options.defer + ? this._observers.post + : this._observers.pre; + + (group[key] || (group[key] = [])).push(callback); + + if (!options || options.init !== false) { + callback.__calling = true; + callback.call(this, this._state[key]); + callback.__calling = false; + } + + return { + cancel: function() { + var index = group[key].indexOf(callback); + if (~index) group[key].splice(index, 1); + } + }; +} + +function on(eventName, handler) { + if (eventName === 'teardown') return this.on('destroy', handler); + + var handlers = this._handlers[eventName] || (this._handlers[eventName] = []); + handlers.push(handler); + + return { + cancel: function() { + var index = handlers.indexOf(handler); + if (~index) handlers.splice(index, 1); + } + }; +} + +function set(newState) { + this._set(assign({}, newState)); + if (this.root._lock) return; + this.root._lock = true; + callAll(this.root._beforecreate); + callAll(this.root._oncreate); + callAll(this.root._aftercreate); + this.root._lock = false; +} + +function _set(newState) { + var oldState = this._state, + changed = {}, + dirty = false; + + for (var key in newState) { + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; + } + if (!dirty) return; + + this._state = assign({}, oldState, newState); + this._recompute(changed, this._state); + if (this._bind) this._bind(changed, this._state); + + if (this._fragment) { + dispatchObservers(this, this._observers.pre, changed, this._state, oldState); + this._fragment.p(changed, this._state); + dispatchObservers(this, this._observers.post, changed, this._state, oldState); + } +} + +function callAll(fns) { + while (fns && fns.length) fns.shift()(); +} + +function _mount(target, anchor) { + this._fragment.m(target, anchor); +} + +function _unmount() { + if (this._fragment) this._fragment.u(); +} + +var proto = { + destroy: destroy, + get: get, + fire: fire, + observe: observe, + on: on, + set: set, + teardown: destroy, + _recompute: noop, + _set: _set, + _mount: _mount, + _unmount: _unmount, + _differs: _differs +}; + +/* generated by Svelte vX.Y.Z */ +var Nested = window.Nested; + +function create_main_fragment(state, component) { + + var nested = new Nested({ + root: component.root, + data: { foo: "bar" } + }); + + return { + c: function create() { + nested._fragment.c(); + }, + + m: function mount(target, anchor) { + nested._mount(target, anchor); + }, + + p: noop, + + u: function unmount() { + nested._unmount(); + }, + + d: function destroy$$1() { + nested.destroy(false); + } + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this._fragment = create_main_fragment(this._state, this); + + if (options.target) { + this._fragment.c(); + this._fragment.m(options.target, options.anchor || null); + + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } +} + +assign(SvelteComponent.prototype, proto); + +SvelteComponent.prototype._differs = _differsImmutable; + +export default SvelteComponent; diff --git a/test/js/samples/component-static-immutable2/expected.js b/test/js/samples/component-static-immutable2/expected.js new file mode 100644 index 0000000000..028dc9e64c --- /dev/null +++ b/test/js/samples/component-static-immutable2/expected.js @@ -0,0 +1,61 @@ +/* generated by Svelte vX.Y.Z */ +import { _differsImmutable, assign, callAll, init, noop, proto } from "svelte/shared.js"; + +var Nested = window.Nested; + +function create_main_fragment(state, component) { + + var nested = new Nested({ + root: component.root, + data: { foo: "bar" } + }); + + return { + c: function create() { + nested._fragment.c(); + }, + + m: function mount(target, anchor) { + nested._mount(target, anchor); + }, + + p: noop, + + u: function unmount() { + nested._unmount(); + }, + + d: function destroy() { + nested.destroy(false); + } + }; +} + +function SvelteComponent(options) { + init(this, options); + this._state = assign({}, options.data); + + if (!options.root) { + this._oncreate = []; + this._beforecreate = []; + this._aftercreate = []; + } + + this._fragment = create_main_fragment(this._state, this); + + if (options.target) { + this._fragment.c(); + this._fragment.m(options.target, options.anchor || null); + + this._lock = true; + callAll(this._beforecreate); + callAll(this._oncreate); + callAll(this._aftercreate); + this._lock = false; + } +} + +assign(SvelteComponent.prototype, proto); + +SvelteComponent.prototype._differs = _differsImmutable; +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/component-static-immutable2/input.html b/test/js/samples/component-static-immutable2/input.html new file mode 100644 index 0000000000..44a2bf2bc1 --- /dev/null +++ b/test/js/samples/component-static-immutable2/input.html @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/test/js/samples/component-static/expected-bundle.js b/test/js/samples/component-static/expected-bundle.js index 5047d48b5e..3db3d982e1 100644 --- a/test/js/samples/component-static/expected-bundle.js +++ b/test/js/samples/component-static/expected-bundle.js @@ -27,8 +27,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -127,7 +127,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -165,7 +165,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index 64b09b4353..97faae3978 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -27,8 +27,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -127,7 +127,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -165,7 +165,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ @@ -209,8 +210,8 @@ assign(SvelteComponent.prototype, proto); SvelteComponent.prototype._recompute = function _recompute(changed, state) { if (changed.x) { - if (differs(state.a, (state.a = a(state.x)))) changed.a = true; - if (differs(state.b, (state.b = b(state.x)))) changed.b = true; + if (this._differs(state.a, (state.a = a(state.x)))) changed.a = true; + if (this._differs(state.b, (state.b = b(state.x)))) changed.b = true; } }; diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index e4a2fe00ef..d87ef16f92 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -1,5 +1,5 @@ /* generated by Svelte vX.Y.Z */ -import { assign, differs, init, noop, proto } from "svelte/shared.js"; +import { assign, init, noop, proto } from "svelte/shared.js"; function a(x) { return x * 2; @@ -41,8 +41,8 @@ assign(SvelteComponent.prototype, proto); SvelteComponent.prototype._recompute = function _recompute(changed, state) { if (changed.x) { - if (differs(state.a, (state.a = a(state.x)))) changed.a = true; - if (differs(state.b, (state.b = b(state.x)))) changed.b = true; + if (this._differs(state.a, (state.a = a(state.x)))) changed.a = true; + if (this._differs(state.b, (state.b = b(state.x)))) changed.b = true; } } export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index 66bb102184..fb4f540eea 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -47,8 +47,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -147,7 +147,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -185,7 +185,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 486c2d1d82..6017c3a0d7 100644 --- a/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js +++ b/test/js/samples/css-shadow-dom-keyframes/expected-bundle.js @@ -39,8 +39,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -139,7 +139,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -177,7 +177,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/deconflict-globals/expected-bundle.js b/test/js/samples/deconflict-globals/expected-bundle.js index 841aaf7111..2d75a472bd 100644 --- a/test/js/samples/deconflict-globals/expected-bundle.js +++ b/test/js/samples/deconflict-globals/expected-bundle.js @@ -27,8 +27,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -127,7 +127,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -165,7 +165,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/do-use-dataset/expected-bundle.js b/test/js/samples/do-use-dataset/expected-bundle.js index b6667c9c91..2482e65cbf 100644 --- a/test/js/samples/do-use-dataset/expected-bundle.js +++ b/test/js/samples/do-use-dataset/expected-bundle.js @@ -43,8 +43,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -143,7 +143,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -181,7 +181,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 643e302968..9efc43320f 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 @@ -47,8 +47,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -147,7 +147,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -185,7 +185,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 5c9ef31f54..2a6a930ea1 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 @@ -47,8 +47,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -147,7 +147,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -185,7 +185,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 cc8a394ac1..626c47980b 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -59,8 +59,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -159,7 +159,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -197,7 +197,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index 252257d9e2..4be9a16f0b 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -39,8 +39,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -139,7 +139,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -177,7 +177,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/head-no-whitespace/expected-bundle.js b/test/js/samples/head-no-whitespace/expected-bundle.js index 60a9e85bf0..a50f2dbef5 100644 --- a/test/js/samples/head-no-whitespace/expected-bundle.js +++ b/test/js/samples/head-no-whitespace/expected-bundle.js @@ -39,8 +39,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -139,7 +139,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -177,7 +177,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 6caf1e5853..2e6975d978 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -43,8 +43,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -143,7 +143,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -181,7 +181,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index 78b31ecb3d..d18967e282 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -43,8 +43,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -143,7 +143,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -181,7 +181,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 b40b46e4c5..987b425333 100644 --- a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -43,8 +43,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -143,7 +143,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -181,7 +181,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 ce83c0473c..79117c831e 100644 --- a/test/js/samples/inline-style-optimized-url/expected-bundle.js +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -43,8 +43,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -143,7 +143,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -181,7 +181,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/inline-style-optimized/expected-bundle.js b/test/js/samples/inline-style-optimized/expected-bundle.js index 60d3574d4f..ff75016f84 100644 --- a/test/js/samples/inline-style-optimized/expected-bundle.js +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -43,8 +43,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -143,7 +143,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -181,7 +181,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/inline-style-unoptimized/expected-bundle.js b/test/js/samples/inline-style-unoptimized/expected-bundle.js index 368d9aeb15..903af075b8 100644 --- a/test/js/samples/inline-style-unoptimized/expected-bundle.js +++ b/test/js/samples/inline-style-unoptimized/expected-bundle.js @@ -43,8 +43,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -143,7 +143,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -181,7 +181,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 dcb3d2e061..a0f57143e2 100644 --- a/test/js/samples/input-without-blowback-guard/expected-bundle.js +++ b/test/js/samples/input-without-blowback-guard/expected-bundle.js @@ -47,8 +47,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -147,7 +147,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -185,7 +185,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/legacy-input-type/expected-bundle.js b/test/js/samples/legacy-input-type/expected-bundle.js index b0174e2105..d08fda0519 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -45,8 +45,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -145,7 +145,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -183,7 +183,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/legacy-quote-class/expected-bundle.js b/test/js/samples/legacy-quote-class/expected-bundle.js index 3956acd874..79a5921c4d 100644 --- a/test/js/samples/legacy-quote-class/expected-bundle.js +++ b/test/js/samples/legacy-quote-class/expected-bundle.js @@ -62,8 +62,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -162,7 +162,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -200,7 +200,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/media-bindings/expected-bundle.js b/test/js/samples/media-bindings/expected-bundle.js index 75e852e835..9427fbe0a4 100644 --- a/test/js/samples/media-bindings/expected-bundle.js +++ b/test/js/samples/media-bindings/expected-bundle.js @@ -55,8 +55,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -155,7 +155,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -193,7 +193,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index 1b85f2f9d9..2026d476ed 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -41,8 +41,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -141,7 +141,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -179,7 +179,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index 9f59cc8a9a..e77cc220bf 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -27,8 +27,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -127,7 +127,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -165,7 +165,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index 988103bc32..530a65fb1e 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -27,8 +27,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -127,7 +127,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -165,7 +165,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/svg-title/expected-bundle.js b/test/js/samples/svg-title/expected-bundle.js index 2ded30910d..2cb1bf8ca9 100644 --- a/test/js/samples/svg-title/expected-bundle.js +++ b/test/js/samples/svg-title/expected-bundle.js @@ -47,8 +47,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -147,7 +147,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -185,7 +185,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/title/expected-bundle.js b/test/js/samples/title/expected-bundle.js index f52f89a71a..36b72b2e93 100644 --- a/test/js/samples/title/expected-bundle.js +++ b/test/js/samples/title/expected-bundle.js @@ -27,8 +27,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -127,7 +127,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -165,7 +165,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ 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 6ece6bffa7..e08d4d3831 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -51,8 +51,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -151,7 +151,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -189,7 +189,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/js/samples/window-binding-scroll/expected-bundle.js b/test/js/samples/window-binding-scroll/expected-bundle.js index b310a6a839..a7c9026cc6 100644 --- a/test/js/samples/window-binding-scroll/expected-bundle.js +++ b/test/js/samples/window-binding-scroll/expected-bundle.js @@ -47,8 +47,8 @@ function destroy(detach) { this._fragment = this._state = null; } -function differs(a, b) { - return a !== b || ((a && typeof a === 'object') || typeof a === 'function'); +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) { @@ -147,7 +147,7 @@ function _set(newState) { dirty = false; for (var key in newState) { - if (differs(newState[key], oldState[key])) changed[key] = dirty = true; + if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; @@ -185,7 +185,8 @@ var proto = { _recompute: noop, _set: _set, _mount: _mount, - _unmount: _unmount + _unmount: _unmount, + _differs: _differs }; /* generated by Svelte vX.Y.Z */ diff --git a/test/runtime/index.js b/test/runtime/index.js index 778f47c0cc..aaafaff21b 100644 --- a/test/runtime/index.js +++ b/test/runtime/index.js @@ -66,6 +66,7 @@ describe("runtime", () => { compileOptions.hydratable = hydrate; compileOptions.dev = config.dev; compileOptions.store = !!config.store; + compileOptions.immutable = config.immutable; // check that no ES2015+ syntax slipped in if (!config.allowES2015) { diff --git a/test/runtime/samples/immutable-mutable/Nested.html b/test/runtime/samples/immutable-mutable/Nested.html new file mode 100644 index 0000000000..02278e385e --- /dev/null +++ b/test/runtime/samples/immutable-mutable/Nested.html @@ -0,0 +1,13 @@ +

Called {{count}} times.

+ + \ No newline at end of file diff --git a/test/runtime/samples/immutable-mutable/_config.js b/test/runtime/samples/immutable-mutable/_config.js new file mode 100644 index 0000000000..5dab0f239b --- /dev/null +++ b/test/runtime/samples/immutable-mutable/_config.js @@ -0,0 +1,16 @@ +export default { + immutable: true, + html: `

Called 0 times.

`, + + test(assert, component, target, window) { + var nested = component.refs.nested; + nested.observe('foo', foo => { + nested.set({ count: nested.get('count') + 1 }); + }); + + assert.htmlEqual(target.innerHTML, `

Called 1 times.

`); + + nested.set({ foo: nested.get('foo') }); + assert.htmlEqual(target.innerHTML, `

Called 2 times.

`); + } +}; diff --git a/test/runtime/samples/immutable-mutable/main.html b/test/runtime/samples/immutable-mutable/main.html new file mode 100644 index 0000000000..4e36143361 --- /dev/null +++ b/test/runtime/samples/immutable-mutable/main.html @@ -0,0 +1,13 @@ +
+ +
+ + diff --git a/test/runtime/samples/immutable-nested/Nested.html b/test/runtime/samples/immutable-nested/Nested.html new file mode 100644 index 0000000000..05eb9f3fdf --- /dev/null +++ b/test/runtime/samples/immutable-nested/Nested.html @@ -0,0 +1,12 @@ +

Called {{count}} times.

+ + \ No newline at end of file diff --git a/test/runtime/samples/immutable-nested/_config.js b/test/runtime/samples/immutable-nested/_config.js new file mode 100644 index 0000000000..541566ce4d --- /dev/null +++ b/test/runtime/samples/immutable-nested/_config.js @@ -0,0 +1,16 @@ +export default { + immutable: true, + html: `

Called 0 times.

`, + + test(assert, component, target, window) { + var nested = component.refs.nested; + nested.observe('foo', foo => { + nested.set({ count: nested.get('count') + 1 }); + }); + + assert.htmlEqual(target.innerHTML, `

Called 1 times.

`); + + nested.set({ foo: nested.get('foo') }); + assert.htmlEqual(target.innerHTML, `

Called 1 times.

`); + } +}; diff --git a/test/runtime/samples/immutable-nested/main.html b/test/runtime/samples/immutable-nested/main.html new file mode 100644 index 0000000000..4e36143361 --- /dev/null +++ b/test/runtime/samples/immutable-nested/main.html @@ -0,0 +1,13 @@ +
+ +
+ + diff --git a/test/runtime/samples/immutable-root/_config.js b/test/runtime/samples/immutable-root/_config.js new file mode 100644 index 0000000000..a862008d7d --- /dev/null +++ b/test/runtime/samples/immutable-root/_config.js @@ -0,0 +1,15 @@ +export default { + immutable: true, + html: `

Called 0 times.

`, + + test(assert, component, target, window) { + component.observe('foo', foo => { + component.set({ count: component.get('count') + 1 }); + }); + + assert.htmlEqual(target.innerHTML, `

Called 1 times.

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

Called 1 times.

`); + } +}; diff --git a/test/runtime/samples/immutable-root/main.html b/test/runtime/samples/immutable-root/main.html new file mode 100644 index 0000000000..087f561caf --- /dev/null +++ b/test/runtime/samples/immutable-root/main.html @@ -0,0 +1,14 @@ +
+

Called {{count}} times.

+
+ + \ No newline at end of file diff --git a/test/store/index.js b/test/store/index.js index d8053367a9..e3c024e4d5 100644 --- a/test/store/index.js +++ b/test/store/index.js @@ -187,4 +187,44 @@ describe('store', () => { }, /Cyclical dependency detected/); }); }); + + describe('immutable', () => { + it('observing state only changes on immutable updates', () => { + let newFoo; + let oldFoo; + let callCount = 0; + let value1 = {}; + let value2 = {}; + + const store = new Store({ + foo: value1 + }, { immutable: true }); + + store.observe('foo', (n, o) => { + callCount++; + newFoo = n; + oldFoo = o; + }); + + assert.equal(callCount, 1); + assert.equal(newFoo, value1); + assert.equal(oldFoo, undefined); + + store.set({ + foo: value1 + }); + + assert.equal(callCount, 1); + assert.equal(newFoo, value1); + assert.equal(oldFoo, undefined); + + store.set({ + foo: value2 + }); + + assert.equal(callCount, 2); + assert.equal(newFoo, value2); + assert.equal(oldFoo, value1); + }); + }); });