Skip to content

Instantly share code, notes, and snippets.

@whitelizard
Created November 18, 2019 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whitelizard/5e45deedb55c6aaad75422d586bfa141 to your computer and use it in GitHub Desktop.
Save whitelizard/5e45deedb55c6aaad75422d586bfa141 to your computer and use it in GitHub Desktop.
Functional style Immutable Store tests
const initialState = {
1: circle([2.5, 0], 1.5),
2: circle([-2, 0], 2),
};
const { getState, dispatch } = createStore(initialState, reducer);
area(getState(1).r); // -> 7.0685834705770345;
area(getState(2).r); // -> 12.566370614359172;
const [hit] = intersect(getState(1), getState(2));
hit; // -> false
if (hit) dispatch({ type: 'hit', id1: 1, id2: 2 });
getState(1).hits; // -> 0
dispatch({ type: 'move', payload: [1, 0], id: 1 });
const [hit2] = intersect(getState(1), getState(2));
hit2; // -> true
if (hit2) dispatch({ type: 'hit', id1: 1, id2: 2 });
getState(1).hits; // -> 1
getState(2).hits; // -> 1
initialState[1].hits = 4; // -> THROWS!
getState(1).hits = 4; // -> THROWS!
getState(1).hits; // -> 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment