mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-05 05:28:20 +08:00
39 lines
744 B
TypeScript
39 lines
744 B
TypeScript
|
import assign from 'object-assign';
|
||
|
|
||
|
export interface Store {
|
||
|
setState: (Object) => void;
|
||
|
getState: () => any;
|
||
|
subscribe: (listener: () => void) => () => void;
|
||
|
}
|
||
|
|
||
|
export default function createStore(initialState): Store {
|
||
|
let state = initialState;
|
||
|
const listeners: any[] = [];
|
||
|
|
||
|
function setState(partial) {
|
||
|
state = assign({}, 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,
|
||
|
};
|
||
|
}
|