|
|
|
@ -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 = [];
|
|
|
|
|