2016-04-15 21:12:04 +02:00
|
|
|
const Dnode = require('dnode')
|
|
|
|
const inherits = require('util').inherits
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
HostStore: HostStore,
|
|
|
|
RemoteStore: RemoteStore,
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
function BaseStore (initState) {
|
2016-04-15 21:12:04 +02:00
|
|
|
this._state = initState || {}
|
|
|
|
this._subs = []
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
BaseStore.prototype.set = function (key, value) {
|
2016-04-15 21:12:04 +02:00
|
|
|
throw Error('Not implemented.')
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
BaseStore.prototype.get = function (key) {
|
2016-04-15 21:12:04 +02:00
|
|
|
return this._state[key]
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
BaseStore.prototype.subscribe = function (fn) {
|
2016-04-15 21:12:04 +02:00
|
|
|
this._subs.push(fn)
|
|
|
|
var unsubscribe = this.unsubscribe.bind(this, fn)
|
|
|
|
return unsubscribe
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
BaseStore.prototype.unsubscribe = function (fn) {
|
2016-04-15 21:12:04 +02:00
|
|
|
var index = this._subs.indexOf(fn)
|
|
|
|
if (index !== -1) this._subs.splice(index, 1)
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
BaseStore.prototype._emitUpdates = function (state) {
|
|
|
|
this._subs.forEach(function (handler) {
|
2016-04-15 21:12:04 +02:00
|
|
|
handler(state)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// host
|
|
|
|
//
|
|
|
|
|
|
|
|
inherits(HostStore, BaseStore)
|
2016-06-21 22:18:32 +02:00
|
|
|
function HostStore (initState, opts) {
|
2016-04-15 21:12:04 +02:00
|
|
|
BaseStore.call(this, initState)
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
HostStore.prototype.set = function (key, value) {
|
2016-04-15 21:12:04 +02:00
|
|
|
this._state[key] = value
|
|
|
|
process.nextTick(this._emitUpdates.bind(this, this._state))
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
HostStore.prototype.createStream = function () {
|
2016-04-15 21:12:04 +02:00
|
|
|
var dnode = Dnode({
|
2016-07-28 20:53:33 +02:00
|
|
|
// update: this._didUpdate.bind(this),
|
2016-04-15 21:12:04 +02:00
|
|
|
})
|
|
|
|
dnode.on('remote', this._didConnect.bind(this))
|
|
|
|
return dnode
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
HostStore.prototype._didConnect = function (remote) {
|
|
|
|
this.subscribe(function (state) {
|
2016-04-15 21:12:04 +02:00
|
|
|
remote.update(state)
|
|
|
|
})
|
|
|
|
remote.update(this._state)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// remote
|
|
|
|
//
|
|
|
|
|
|
|
|
inherits(RemoteStore, BaseStore)
|
2016-06-21 22:18:32 +02:00
|
|
|
function RemoteStore (initState, opts) {
|
2016-04-15 21:12:04 +02:00
|
|
|
BaseStore.call(this, initState)
|
|
|
|
this._remote = null
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
RemoteStore.prototype.set = function (key, value) {
|
2016-04-15 21:12:04 +02:00
|
|
|
this._remote.set(key, value)
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
RemoteStore.prototype.createStream = function () {
|
2016-04-15 21:12:04 +02:00
|
|
|
var dnode = Dnode({
|
|
|
|
update: this._didUpdate.bind(this),
|
|
|
|
})
|
|
|
|
dnode.once('remote', this._didConnect.bind(this))
|
|
|
|
return dnode
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
RemoteStore.prototype._didConnect = function (remote) {
|
2016-04-15 21:12:04 +02:00
|
|
|
this._remote = remote
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
RemoteStore.prototype._didUpdate = function (state) {
|
2016-04-15 21:12:04 +02:00
|
|
|
this._state = state
|
|
|
|
this._emitUpdates(state)
|
|
|
|
}
|