mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-19 09:34:02 +00:00
* use DeepFrozenStore in one end to end test Follow up to #320 * add DeepFrozenStore to some Core unit tests
44 lines
884 B
JavaScript
44 lines
884 B
JavaScript
var deepFreeze = require('deep-freeze')
|
|
|
|
/**
|
|
* 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 = Object.assign({}, this.state)
|
|
const nextState = deepFreeze(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 DeepFrozenStore()
|
|
}
|