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.
47 lines
956 B
47 lines
956 B
|
2 years ago
|
import packageJson from '../package.json'
|
||
|
|
/**
|
||
|
|
* Default store that keeps state in a simple object.
|
||
|
|
*/
|
||
|
|
class DefaultStore {
|
||
|
|
static VERSION = packageJson.version
|
||
|
|
|
||
|
|
constructor () {
|
||
|
|
this.state = {}
|
||
|
|
this.callbacks = [] // TODO: use a Set instead, make it a private prop
|
||
|
|
}
|
||
|
|
|
||
|
|
getState () {
|
||
|
|
return this.state
|
||
|
|
}
|
||
|
|
|
||
|
|
setState (patch) {
|
||
|
|
const prevState = { ...this.state }
|
||
|
|
const nextState = { ...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)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TODO: export the class instead in the next major.
|
||
|
|
export default function defaultStore () {
|
||
|
|
return new DefaultStore()
|
||
|
|
}
|