Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

Commit

Permalink
Remove dependency on immer
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed Nov 16, 2023
1 parent c9cb1e5 commit fdb29f7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
10 changes: 0 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"typescript": "^5.0.2"
},
"dependencies": {
"immer": "^10.0.3",
"preact": "^10.19.2",
"ts-adt": "^2.1.2",
"wouter-preact": "^2.12.1"
Expand Down
9 changes: 2 additions & 7 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { h } from "preact";
import { useReducer } from "preact/hooks";
import { Link, Route, Router } from "wouter-preact";
import { produce } from "immer";

import WorldMap from "./WorldMap";
import NotFound from "./NotFound";
import Homepage from "./Homepage";
import Level from "./Level";
import Home from "../icons/home";
import LevelsIcon from "../icons/levels";
import { AppContext, initialState, updateState } from "../logic/state";
import { AppContext, useStateReducer } from "../logic/state";

import "../styles/app.css";

export function App() {
const context: AppContext = useReducer(
(state, action) => produce(state, (draft) => updateState(draft, action)),
initialState,
);
const context = useStateReducer();

return (
<AppContext.Provider value={context}>
Expand Down
45 changes: 30 additions & 15 deletions src/logic/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext } from "preact/hooks";
import { useContext, useRef, useState } from "preact/hooks";
import { createContext } from "preact";
import { ADT } from "ts-adt";

Expand Down Expand Up @@ -26,10 +26,21 @@ export const AppContext = createContext<AppContext>([

export const useAppState = () => useContext(AppContext);

export const updateState = (
state: AppState,
action: AppAction,
): AppState | void => {
const saveState = (state: AppState) => {
localStorage.setItem("state", JSON.stringify(state));
};

function loadState(): AppState {
const savedState = localStorage.getItem("state");
const parsedSavedState = savedState === null ? {} : JSON.parse(savedState);

return {
completed: 0,
...parsedSavedState,
};
}

const updateState = (state: AppState, action: AppAction): AppState => {
switch (action._type) {
case "loadState":
return loadState();
Expand All @@ -39,18 +50,22 @@ export const updateState = (
}

saveState(state);
};

const saveState = (state: AppState) => {
localStorage.setItem("state", JSON.stringify(state));
return state;
};

function loadState(): AppState {
const savedState = localStorage.getItem("state");
const parsedSavedState = savedState === null ? {} : JSON.parse(savedState);
// Similar to using `useReducer` directly, but lets our `updateState`
// function mutate the state directly instead of having to copy each time.
export const useStateReducer = (): AppContext => {
const state = useRef(initialState);

return {
completed: 0,
...parsedSavedState,
// A simple way for us to manually trigger updates
const [gen, setGen] = useState(0);

const sendAction = (action: AppAction) => {
state.current = updateState(state.current, action);
setGen(gen + 1);
};
}

return [state.current, sendAction];
};

0 comments on commit fdb29f7

Please sign in to comment.