Problem, as an interviewer would state it:
"Implement a minimal createStore(reducer, initialState) — like Redux — with getState(), dispatch(action), and subscribe(listener)."
Examples:
const store = createStore(counterReducer, { count: 0 });
store.subscribe(() => console.log(store.getState()));
store.dispatch({ type: "INCREMENT" }); // logs { count: 1 }
Clarifying questions expected:
- Should
subscribereturn an unsubscribe function, and must calling it correctly stop future notifications for JUST that listener? - Should the reducer receive the CURRENT state and an action, returning a brand-new state (never mutating), matching the real, standard Redux convention?
- Should
dispatchitself return anything meaningful, like the action that was dispatched?
Code / implementation expected: Yes — real, direct proof of a real dispatch sequence, confirming correct state updates, subscriber notification on every dispatch, and a correctly-working unsubscribe.