import { assign, blankObject, _differs, _differsImmutable, get, observe, on, fire } from './shared.js'; function Store(state, options) { this._observers = { pre: blankObject(), post: blankObject() }; this._handlers = {}; this._dependents = []; this._computed = blankObject(); this._sortedComputedProperties = []; this._state = assign({}, state); this._differs = options && options.immutable ? _differsImmutable : _differs; } assign(Store.prototype, { _add: function(component, props) { this._dependents.push({ component: component, props: props }); }, _init: function(props) { var state = {}; for (var i = 0; i < props.length; i += 1) { var prop = props[i]; state['$' + prop] = this._state[prop]; } return state; }, _remove: function(component) { var i = this._dependents.length; 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 cycles; var visited = blankObject(); function visit(key) { if (cycles[key]) { throw new Error('Cyclical dependency detected'); } if (visited[key]) return; visited[key] = true; var c = computed[key]; if (c) { cycles[key] = true; c.deps.forEach(visit); sorted.push(c); } } for (var key in this._computed) { cycles = blankObject(); 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 (store._differs(newValue, value)) { value = newValue; changed[key] = true; state[key] = value; } } } }; c.update(this._state, {}, true); this._computed[key] = c; this._sortComputedProperties(); }, fire: fire, get: get, // TODO remove this method observe: observe, on: on, 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); }); }, set: function(newState) { var oldState = this._state, changed = this._changed = {}, dirty = false; for (var key in newState) { if (this._computed[key]) throw new Error("'" + key + "' is a read-only property"); if (this._differs(newState[key], oldState[key])) changed[key] = dirty = true; } if (!dirty) return; this._state = assign(assign({}, oldState), newState); for (var i = 0; i < this._sortedComputedProperties.length; i += 1) { this._sortedComputedProperties[i].update(this._state, changed); } 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) { 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); } this.fire('update', { changed: changed, current: this._state, previous: oldState }); } }); export { Store };