on 50637ac help sign in
bridge/src/store/pointCache.jsx
51 lines 1.5 kB history raw
1import { Just, Nothing } from 'folktale/maybe';
2import Result from 'folktale/result';
3import React, { createContext, forwardRef, useContext } from 'react';
5import useDeepEqualReference from 'lib/useDeepEqualReference';
6import usePointStore from './lib/usePointStore';
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*/
14const 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: () => [],
28export const PointCacheContext = createContext(initialContext);
30export function PointCacheProvider({ children }) {
31 const cache = usePointStore();
33 return (
34 <PointCacheContext.Provider value={useDeepEqualReference(cache)}>
35 {children}
36 </PointCacheContext.Provider>
37 );
40// Hook version
41export function usePointCache() {
42 return useContext(PointCacheContext);
45// HOC version
46export const withPointCache = Component =>
47 forwardRef((props, ref) => (
48 <PointCacheContext.Consumer>
49 {cache => <Component ref={ref} {...cache} {...props} />}
50 </PointCacheContext.Consumer>
51 ));