import async from 'async' var Promise = require('bluebird') module.exports = BaseStore function BaseStore(options) { this._listeners = {} this._plugin_teardowns = [] if (options.actions) { for (var name in options.actions) { this.actions[name] = options.actions[name] } } } BaseStore.prototype = { actions: {}, init(plugins, pluginConfig, done) { this.allPlugins = plugins || [] if (!plugins) return done() const tasks = plugins // .map(plugin => plugin.store) // .filter(plugin => !!plugin) .map(plugin => plugin.store && this.addPlugin.bind( this, plugin.store, pluginConfig && pluginConfig[plugin.id] )) .filter(fn => !!fn) async.parallel(tasks, done) }, teardown: function () { this._plugin_teardowns.forEach(fn => fn(this)) }, addPlugin: function (plugin, config, done) { if (plugin.teardown) { this._plugin_teardowns.push(plugin.teardown) } var name , actions if (plugin.actions) { if ('function' === typeof plugin.actions) { actions = plugin.actions(this.allPlugins) } else { actions = plugin.actions } for (name in actions) { this.actions[name] = actions[name] } } if (plugin.extend) { for (name in plugin.extend) { this[name] = plugin.extend[name] } } if (plugin.init) { plugin.init(this, config) // TODO async? } else if (plugin.asyncInit) { let timeout = setTimeout(() => { if (timeout !== null) { done(new Error('Plugin failed to initialize within 5 seconds')) timeout = null } }, 10000) return plugin.asyncInit(this, config, (err) => { if (timeout === null) { return } clearTimeout(timeout) timeout = null done(err) }) } done() }, on: function (changes, listener) { if ('string' === typeof changes) changes = [changes] for (var i=0; i { if (window.DEBUG_CHANGES) { console.log('emitting', this._changed) } var changes = this._changed this._changed = null this.emitChanged(changes) }, 0) } if (window.DEBUG_CHANGES) { console.log('changed', what) } }, emitChanged: function (what) { var called = [] var promises = [] for (var i=0; i