on 07faa93 help sign in
bridge/src/store/pointCache.jsx
54 lines 1.6 kB history raw
1import { Just, Nothing } from 'folktale/maybe';
2import Result from 'folktale/result';
3import React, { createContext, forwardRef, useContext } from 'react';
4import { noop } from 'lodash';
6import useDeepEqualReference from 'lib/useDeepEqualReference';
7import usePointStore from './lib/usePointStore';
9/**
10 * Avoid race condition by setting an initial dummy state. Previously, on
11 * occasion the application would render before the points queries finished
12 * loading data asynchronously from the roller and Infura. This would cause
13 * a crash in Points.tsx when attempting to destructure the data.
14*/
15const initialContext = {
16 details: Nothing(),
17 rekeyDates: Nothing(),
18 controlledPoints: Nothing(),
19 ecliptic: Nothing(),
20 residents: Nothing(),
21 pointCursor: Nothing(),
22 syncDetails: noop,
23 syncEclipticOwner:noop,
24 syncRekeyDate: noop,
25 syncControlledPoints: noop,
26 syncDates: noop,
27 syncResidents: noop,
28 syncExtras: noop,
31export const PointCacheContext = createContext(initialContext);
33export function PointCacheProvider({ children }) {
34 const cache = usePointStore();
36 return (
37 <PointCacheContext.Provider value={useDeepEqualReference(cache)}>
38 {children}
39 </PointCacheContext.Provider>
40 );
43// Hook version
44export function usePointCache() {
45 return useContext(PointCacheContext);
48// HOC version
49export const withPointCache = Component =>
50 forwardRef((props, ref) => (
51 <PointCacheContext.Consumer>
52 {cache => <Component ref={ref} {...cache} {...props} />}
53 </PointCacheContext.Consumer>
54 ));