nav2/lib/store.ts

17 lines
420 B
TypeScript
Raw Permalink Normal View History

2024-11-12 20:18:36 +03:00
export const createStore = <StateType>(initialState: StateType) => {
let state: StateType = initialState
2024-11-12 20:06:45 +03:00
const listeners = new Set<() => void>()
return {
getState: () => state,
subscribe: (listener) => {
listeners.add(listener)
return () => {listeners.delete(listener)}
},
setState: (newState) => {
state = newState
listeners.forEach((listener) => listener())
},
}
}