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

31 lines
554 B
JavaScript
Raw Normal View History

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