on master help sign in
fix: resolve pointsCache race condition

Avoid race condition by setting an initial dummy state. Previously, on occasion the application would render before the points queries finished loading data asynchronously from the roller and Infura. This would cause a crash in Points.tsx when attempting to destructure the data.

commit50637acd944e7aa2f0dd6aff25842b6495dd4cd6 authortomholford <tomholford@users.noreply.github.com> dateSun 16 Oct 2022, 20:30 (4 years ago) parentb9ad86d
2 files changed+23−8
src/store/pointCache.jsx+23−1 src/views/Points.tsx−7
src/store/pointCache.jsx+23−1
@@ -1,9 +1,31 @@
1+import { Just, Nothing } from 'folktale/maybe';
2+import Result from 'folktale/result';
13import React, { createContext, forwardRef, useContext } from 'react';
24
35import useDeepEqualReference from 'lib/useDeepEqualReference';
46import usePointStore from './lib/usePointStore';
57
6−export const PointCacheContext = createContext(null);
8+/**
9+ * Avoid race condition by setting an initial dummy state. Previously, on
10+ * occasion the application would render before the points queries finished
11+ * loading data asynchronously from the roller and Infura. This would cause
12+ * a crash in Points.tsx when attempting to destructure the data.
13+*/
14+const initialContext = {
15+ details: Nothing(),
16+ rekeyDates: Nothing(),
17+ controlledPoints: Just(Result.Error('not yet loaded')),
18+ ecliptic: Nothing(),
19+ residents: Nothing(),
20+ syncDetails: () => [],
21+ syncRekeyDate: () => [],
22+ syncControlledPoints: () => [],
23+ syncDates: () => [],
24+ syncResidents: () => [],
25+ syncExtras: () => [],
26+}
27+
28+export const PointCacheContext = createContext(initialContext);
729
830export function PointCacheProvider({ children }) {
931 const cache = usePointStore();
src/views/Points.tsx−7
@@ -67,13 +67,6 @@ export default function Points() {
6767 const { wallet }: any = useWallet();
6868 const { pop, push, names }: any = useHistory();
6969 const { setPointCursor }: any = usePointCursor();
70− /**
71− * TODO: there is a race condition here; sometimes the pointCache is not
72− * loaded by the time this view renders, which then causes a crash when
73− * attempting to destructure.
74− *
75− * Same is also true for isEclipticOwner.
76− */
7770 const { controlledPoints, getDetails }: any = usePointCache();
7871 const { contracts }: any = useNetwork();
7972 const isEclipticOwner = useIsEclipticOwner();