uppy/packages/@uppy/store-default/src/index.js
Renée Kooi 980210f76c
Add VERSION properties to all plugins.
And remove the proposal-object-rest-spread plugin because it is built
into preset-env now.
2019-05-27 16:53:47 +02:00

44 lines
847 B
JavaScript

/**
* Default store that keeps state in a simple object.
*/
class DefaultStore {
static VERSION = require('../package.json').version
constructor () {
this.state = {}
this.callbacks = []
}
getState () {
return this.state
}
setState (patch) {
const prevState = Object.assign({}, this.state)
const nextState = Object.assign({}, this.state, patch)
this.state = nextState
this._publish(prevState, nextState, patch)
}
subscribe (listener) {
this.callbacks.push(listener)
return () => {
// Remove the listener.
this.callbacks.splice(
this.callbacks.indexOf(listener),
1
)
}
}
_publish (...args) {
this.callbacks.forEach((listener) => {
listener(...args)
})
}
}
module.exports = function defaultStore () {
return new DefaultStore()
}