on a2a5eed help sign in
bridge/src/lib/useSyncPoints.js
39 lines 1.4 kB history raw
1import { useEffect } from 'react';
2import { isEqual } from 'lodash';
4import { usePointCache } from 'store/pointCache';
5import usePreviousValue from 'lib/usePreviousValue';
7// sync a known (i.e. mine) point to the cache (i.e display values)
8export function useSyncKnownPoints(points = []) {
9 // TBD: right now we don't need anything more than the point itself
10 // but soon we'll need to know information around "key type"
13// sync a foreign (not the user's) point to the cache
14export function useSyncForeignPoints(points = []) {
15 const { syncForeignPoint } = usePointCache();
16 const prevPoints = usePreviousValue(points);
18 useEffect(() => {
19 if (!isEqual(prevPoints, points)) {
20 // if the actual set of points changes, sync the whole array
21 // TODO: we should get clever about caching
22 points.forEach(point => syncForeignPoint(point));
23 }
24 }, [syncForeignPoint, prevPoints, points]);
27// sync an owned point to the cache
28export function useSyncOwnedPoints(points = []) {
29 const { syncOwnedPoint } = usePointCache();
30 const prevPoints = usePreviousValue(points);
32 useEffect(() => {
33 if (!isEqual(prevPoints, points)) {
34 // if the actual set of points changes, sync the whole array
35 // TODO: we should get clever about caching
36 points.forEach(point => syncOwnedPoint(point));
37 }
38 }, [syncOwnedPoint, prevPoints, points]);