|
|
|
@ -28,14 +28,13 @@ function differs(a, b) {
|
|
|
|
|
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dispatchObservers(component, group, newState, oldState) {
|
|
|
|
|
function dispatchObservers(component, group, changed, newState, oldState) {
|
|
|
|
|
for (var key in group) {
|
|
|
|
|
if (!(key in newState)) continue;
|
|
|
|
|
if (!(key in changed)) continue;
|
|
|
|
|
|
|
|
|
|
var newValue = newState[key];
|
|
|
|
|
var oldValue = oldState[key];
|
|
|
|
|
|
|
|
|
|
if (differs(newValue, oldValue)) {
|
|
|
|
|
var callbacks = group[key];
|
|
|
|
|
if (!callbacks) continue;
|
|
|
|
|
|
|
|
|
@ -48,7 +47,6 @@ function dispatchObservers(component, group, newState, oldState) {
|
|
|
|
|
callback.__calling = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get(key) {
|
|
|
|
@ -110,6 +108,23 @@ function set(newState) {
|
|
|
|
|
this._root._lock = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _set(newState) {
|
|
|
|
|
var oldState = this._state,
|
|
|
|
|
changed = {},
|
|
|
|
|
dirty = false;
|
|
|
|
|
|
|
|
|
|
for (var key in newState) {
|
|
|
|
|
if (differs(newState[key], oldState[key])) changed[key] = dirty = true;
|
|
|
|
|
}
|
|
|
|
|
if (!dirty) return;
|
|
|
|
|
|
|
|
|
|
this._state = assign({}, oldState, newState);
|
|
|
|
|
this._recompute(changed, this._state, oldState, false);
|
|
|
|
|
dispatchObservers(this, this._observers.pre, changed, this._state, oldState);
|
|
|
|
|
this._fragment.update(changed, this._state);
|
|
|
|
|
dispatchObservers(this, this._observers.post, changed, this._state, oldState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function callAll(fns) {
|
|
|
|
|
while (fns && fns.length) fns.pop()();
|
|
|
|
|
}
|
|
|
|
@ -121,16 +136,11 @@ var proto = {
|
|
|
|
|
observe: observe,
|
|
|
|
|
on: on,
|
|
|
|
|
set: set,
|
|
|
|
|
teardown: destroy
|
|
|
|
|
teardown: destroy,
|
|
|
|
|
_recompute: noop,
|
|
|
|
|
_set: _set
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function recompute ( state, newState, oldState, isInitial ) {
|
|
|
|
|
if ( isInitial || ( 'x' in newState && differs( state.x, oldState.x ) ) ) {
|
|
|
|
|
state.a = newState.a = template.computed.a( state.x );
|
|
|
|
|
state.b = newState.b = template.computed.b( state.x );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var template = (function () {
|
|
|
|
|
return {
|
|
|
|
|
computed: {
|
|
|
|
@ -147,6 +157,8 @@ function create_main_fragment ( state, component ) {
|
|
|
|
|
|
|
|
|
|
mount: noop,
|
|
|
|
|
|
|
|
|
|
update: noop,
|
|
|
|
|
|
|
|
|
|
unmount: noop,
|
|
|
|
|
|
|
|
|
|
destroy: noop
|
|
|
|
@ -156,7 +168,7 @@ function create_main_fragment ( state, component ) {
|
|
|
|
|
function SvelteComponent ( options ) {
|
|
|
|
|
options = options || {};
|
|
|
|
|
this._state = options.data || {};
|
|
|
|
|
recompute( this._state, this._state, {}, true );
|
|
|
|
|
this._recompute( {}, this._state, {}, true );
|
|
|
|
|
|
|
|
|
|
this._observers = {
|
|
|
|
|
pre: Object.create( null ),
|
|
|
|
@ -178,12 +190,11 @@ function SvelteComponent ( options ) {
|
|
|
|
|
|
|
|
|
|
assign( SvelteComponent.prototype, proto );
|
|
|
|
|
|
|
|
|
|
SvelteComponent.prototype._set = function _set ( newState ) {
|
|
|
|
|
var oldState = this._state;
|
|
|
|
|
this._state = assign( {}, oldState, newState );
|
|
|
|
|
recompute( this._state, newState, oldState, false );
|
|
|
|
|
dispatchObservers( this, this._observers.pre, newState, oldState );
|
|
|
|
|
dispatchObservers( this, this._observers.post, newState, oldState );
|
|
|
|
|
SvelteComponent.prototype._recompute = function _recompute ( changed, state, oldState, isInitial ) {
|
|
|
|
|
if ( isInitial || ( 'x' in changed ) ) {
|
|
|
|
|
if ( differs( ( state.a = template.computed.a( state.x ) ), oldState.a ) ) changed.a = true;
|
|
|
|
|
if ( differs( ( state.b = template.computed.b( state.x ) ), oldState.b ) ) changed.b = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SvelteComponent;
|
|
|
|
|