import type { GridState } from './types' import type { Actions } from './actions' import { INITIAL_STATE } from '.' import reducer from './reducer' import type { StoreSelectors } from './selectors' import { createSelectors } from './selectors' export type Store = { subscribe: (listener: () => void) => () => void dispatch: (action: Actions) => void getState: () => GridState selectors: StoreSelectors } export function createStore(): Store { let state = { ...INITIAL_STATE } const listeners = new Set<() => void>() const subscribe = (listener: () => void) => { listeners.add(listener) return () => { listeners.delete(listener) } } const notify = () => { listeners.forEach((l) => l()) } const dispatch = (action: Actions) => { const currentState = state state = reducer(state, action) if (currentState !== state) { notify() } } const getState = () => state const selectors = createSelectors() return { subscribe, dispatch, getState, selectors } }