mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-18 00:55:35 +00:00
* enforce some eslint rules * enforce accessibility linter rules * harden lint rules with only 1 or 2 warnings * fix remaining rules with less than 3 warnings * fix e2e tests * fix remaining rules with less than 4 warnings * fix remaining rules with less than 6 warnings * fix `shuffleTaglines` * fix companion build
46 lines
908 B
JavaScript
46 lines
908 B
JavaScript
const deepFreeze = require('deep-freeze')
|
|
|
|
/* eslint-disable no-underscore-dangle */
|
|
|
|
/**
|
|
* Default store + deepFreeze on setState to make sure nothing is mutated accidentally
|
|
*/
|
|
class DeepFrozenStore {
|
|
constructor () {
|
|
this.state = {}
|
|
this.callbacks = []
|
|
}
|
|
|
|
getState () {
|
|
return this.state
|
|
}
|
|
|
|
setState (patch) {
|
|
const prevState = { ...this.state }
|
|
const nextState = deepFreeze({ ...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 DeepFrozenStore()
|
|
}
|