mirror of
https://gitee.com/ant-design-vue/ant-design-vue.git
synced 2024-12-15 17:31:43 +08:00
31 lines
549 B
JavaScript
31 lines
549 B
JavaScript
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,
|
|
}
|
|
}
|