You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/store.js

174 lines
3.6 KiB

7 years ago
import {
assign,
blankObject,
differs,
dispatchObservers,
get,
observe
} from './shared.js';
function Store(state) {
this._observers = { pre: blankObject(), post: blankObject() };
this._changeHandlers = [];
this._dependents = [];
this._computed = blankObject();
this._sortedComputedProperties = [];
this._state = assign({}, state);
7 years ago
}
assign(Store.prototype, {
_add: function(component, props) {
this._dependents.push({
component: component,
props: props
});
},
7 years ago
_init: function(props) {
var state = {};
for (var i = 0; i < props.length; i += 1) {
7 years ago
var prop = props[i];
state['$' + prop] = this._state[prop];
}
return state;
},
7 years ago
_remove: function(component) {
var i = this._dependents.length;
7 years ago
while (i--) {
if (this._dependents[i].component === component) {
this._dependents.splice(i, 1);
return;
}
}
},
_sortComputedProperties: function() {
var computed = this._computed;
var sorted = this._sortedComputedProperties = [];
var visited = blankObject();
function visit(key) {
if (visited[key]) return;
var c = computed[key];
if (c) {
c.deps.forEach(visit);
sorted.push(c);
}
}
for (var key in this._computed) visit(key);
},
compute: function(key, deps, fn) {
var store = this;
var value;
var c = {
deps: deps,
update: function(state, changed, dirty) {
var values = deps.map(function(dep) {
if (dep in changed) dirty = true;
return state[dep];
});
if (dirty) {
var newValue = fn.apply(null, values);
if (differs(newValue, value)) {
value = newValue;
changed[key] = true;
state[key] = value;
}
}
}
};
c.update(this._state, {}, true);
this._computed[key] = c;
this._sortComputedProperties();
},
get: get,
observe: observe,
7 years ago
onchange: function(callback) {
this._changeHandlers.push(callback);
return {
cancel: function() {
var index = this._changeHandlers.indexOf(callback);
if (~index) this._changeHandlers.splice(index, 1);
}
};
},
set: function(newState) {
var oldState = this._state,
changed = this._changed = {},
7 years ago
dirty = false;
for (var key in newState) {
if (this._computed[key]) throw new Error("'" + key + "' is a read-only property");
7 years ago
if (differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
if (!dirty) return;
this._state = assign({}, oldState, newState);
for (var i = 0; i < this._sortedComputedProperties.length; i += 1) {
this._sortedComputedProperties[i].update(this._state, changed);
}
7 years ago
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);
var dependents = this._dependents.slice(); // guard against mutations
for (var i = 0; i < dependents.length; i += 1) {
var dependent = dependents[i];
var componentState = {};
dirty = false;
for (var j = 0; j < dependent.props.length; j += 1) {
var prop = dependent.props[j];
if (prop in changed) {
componentState['$' + prop] = this._state[prop];
dirty = true;
}
}
if (dirty) dependent.component.set(componentState);
}
dispatchObservers(this, this._observers.post, changed, this._state, oldState);
}
});
function combineStores(children, store) {
if (!store) store = new Store();
var updates = {};
function addChild(key) {
var child = children[key];
updates[key] = child.get();
child.onchange(function(state) {
var update = {};
update[key] = state;
store.set(update);
});
}
for (var key in children) addChild(key);
store.set(updates);
return store;
}
export { Store, combineStores };