ant-design-vue/components/_util/store/create.js

31 lines
549 B
JavaScript
Raw Normal View History

2018-03-25 10:07:04 +00:00
export default function create (initialState) {
let state = initialState
const listeners = []
function setState (partial) {
state = { ...state, ...partial }
for (let i = 0; i < listeners.length; i++) {
listeners[i]()
}
}
function getState () {
return state
}
function subscribe (listener) {
listeners.push(listener)
return function unsubscribe () {
const index = listeners.indexOf(listener)
listeners.splice(index, 1)
}
}
return {
setState,
getState,
subscribe,
}
}