A Metamask fork with Infura removed and default networks editable
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.
ciphermask/app/scripts/lib/remote-store.js

98 lines
2.0 KiB

const Dnode = require('dnode')
const inherits = require('util').inherits
module.exports = {
HostStore: HostStore,
RemoteStore: RemoteStore,
}
9 years ago
function BaseStore (initState) {
this._state = initState || {}
this._subs = []
}
9 years ago
BaseStore.prototype.set = function (key, value) {
throw Error('Not implemented.')
}
9 years ago
BaseStore.prototype.get = function (key) {
return this._state[key]
}
9 years ago
BaseStore.prototype.subscribe = function (fn) {
this._subs.push(fn)
var unsubscribe = this.unsubscribe.bind(this, fn)
return unsubscribe
}
9 years ago
BaseStore.prototype.unsubscribe = function (fn) {
var index = this._subs.indexOf(fn)
if (index !== -1) this._subs.splice(index, 1)
}
9 years ago
BaseStore.prototype._emitUpdates = function (state) {
this._subs.forEach(function (handler) {
handler(state)
})
}
//
// host
//
inherits(HostStore, BaseStore)
9 years ago
function HostStore (initState, opts) {
BaseStore.call(this, initState)
}
9 years ago
HostStore.prototype.set = function (key, value) {
this._state[key] = value
process.nextTick(this._emitUpdates.bind(this, this._state))
}
9 years ago
HostStore.prototype.createStream = function () {
var dnode = Dnode({
// update: this._didUpdate.bind(this),
})
dnode.on('remote', this._didConnect.bind(this))
return dnode
}
9 years ago
HostStore.prototype._didConnect = function (remote) {
this.subscribe(function (state) {
remote.update(state)
})
remote.update(this._state)
}
//
// remote
//
inherits(RemoteStore, BaseStore)
9 years ago
function RemoteStore (initState, opts) {
BaseStore.call(this, initState)
this._remote = null
}
9 years ago
RemoteStore.prototype.set = function (key, value) {
this._remote.set(key, value)
}
9 years ago
RemoteStore.prototype.createStream = function () {
var dnode = Dnode({
update: this._didUpdate.bind(this),
})
dnode.once('remote', this._didConnect.bind(this))
return dnode
}
9 years ago
RemoteStore.prototype._didConnect = function (remote) {
this._remote = remote
}
9 years ago
RemoteStore.prototype._didUpdate = function (state) {
this._state = state
this._emitUpdates(state)
}