on master help sign in
fix: resolve pointsCache race condition
50637actomholford
src/store/pointCache.jsx+23−1
@@ -1,9 +1,31 @@
1+import { Just, Nothing } from 'folktale/maybe';
2+import Result from 'folktale/result';
1import React, { createContext, forwardRef, useContext } from 'react';
3import React, { createContext, forwardRef, useContext } from 'react';
2
4
3import useDeepEqualReference from 'lib/useDeepEqualReference';
5import useDeepEqualReference from 'lib/useDeepEqualReference';
4import usePointStore from './lib/usePointStore';
6import usePointStore from './lib/usePointStore';
5
7
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+
6−export const PointCacheContext = createContext(null);
28+export const PointCacheContext = createContext(initialContext);
7
29
8export function PointCacheProvider({ children }) {
30export function PointCacheProvider({ children }) {
9 const cache = usePointStore();
31 const cache = usePointStore();
src/views/Points.tsx−7
@@ -67,13 +67,6 @@ export default function Points() {
67 const { wallet }: any = useWallet();
67 const { wallet }: any = useWallet();
68 const { pop, push, names }: any = useHistory();
68 const { pop, push, names }: any = useHistory();
69 const { setPointCursor }: any = usePointCursor();
69 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− */
77 const { controlledPoints, getDetails }: any = usePointCache();
70 const { controlledPoints, getDetails }: any = usePointCache();
78 const { contracts }: any = useNetwork();
71 const { contracts }: any = useNetwork();
79 const isEclipticOwner = useIsEclipticOwner();
72 const isEclipticOwner = useIsEclipticOwner();