You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
554 B
31 lines
554 B
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,
|
|
};
|
|
}
|