Skip to content

Commit

Permalink
Eliminate possibility of state desync.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswilddev committed Oct 27, 2021
1 parent dd7b60c commit 02d5f30
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions components/createSessionStoreManagerComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from "react";
import { useRefresh } from "../../hooks/useRefresh";
import { useEventRefresh } from "../../hooks/useEventRefresh";
import type { SessionStore } from "../../services/SessionStore";
import type { Json } from "../../types/Json";
Expand Down Expand Up @@ -26,7 +27,8 @@ export const createSessionStoreManagerComponent = <T extends Json>(
) => React.ReactElement<any, any> | null;
}> => {
return ({ loading, ready }) => {
const [loaded, setLoaded] = React.useState(false);
const loaded = React.useRef(false);
const refresh = useRefresh();
useEventRefresh(sessionStore, `set`);

React.useEffect(() => {
Expand All @@ -38,7 +40,8 @@ export const createSessionStoreManagerComponent = <T extends Json>(
switch (state as `loading` | `aborting`) {
case `loading`:
state = `loaded`;
setLoaded(true);
loaded.current = true;
refresh();
break;

case `aborting`:
Expand All @@ -61,7 +64,7 @@ export const createSessionStoreManagerComponent = <T extends Json>(
};
}, []);

if (loaded) {
if (loaded.current) {
return ready(sessionStore.get(), (to: T) => {
sessionStore.set(to);
});
Expand Down

0 comments on commit 02d5f30

Please sign in to comment.