uppy/test/resources/DeepFrozenStore.js
Kevin van Zonneveld bfe659820e
Upgrade linting to 2.0.0-0 (#3280)
* Upgrade linting to 2.0.0-0

* Adjust so we can pass without error

* Fix linting

* Update header-blacklist.js

* Update index.js

* Update Uppy.js

* Update Components.js

* Update StatusBar.js

* Update Assembly.js

* Upgrade to linting 2.0.0 (final release)
2021-11-09 11:19:05 +00:00

46 lines
909 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()
}