Skip to content

Commit

Permalink
Merge pull request #116 from Kitware/fix-data-available-triggers
Browse files Browse the repository at this point in the history
Fix onDataAvailable triggers
  • Loading branch information
floryst authored Jul 11, 2023
2 parents ae5e18a + 353b78a commit 1d86e33
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/core/Geometry2DRepresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { IDownstream, IRepresentation } from '../types';
import { compareShallowObject } from '../utils/comparators';
import useBooleanAccumulator from '../utils/useBooleanAccumulator';
import useComparableEffect from '../utils/useComparableEffect';
import useLatest from '../utils/useLatest';
import {
DownstreamContext,
RepresentationContext,
Expand Down Expand Up @@ -153,13 +154,15 @@ export default forwardRef(function Geometry2DRepresentation(

// --- events --- //

const { onDataAvailable } = props;
const onDataAvailable = useLatest(props.onDataAvailable);
useEffect(() => {
if (dataAvailable) {
// trigger onDataAvailable after making updates to the actor and mapper
onDataAvailable?.();
onDataAvailable.current?.();
}
}, [dataAvailable, onDataAvailable]);
// onDataAvailable is a ref
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dataAvailable]);

// --- //

Expand Down
9 changes: 6 additions & 3 deletions src/core/GeometryRepresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { IDownstream, IRepresentation } from '../types';
import { compareShallowObject } from '../utils/comparators';
import useBooleanAccumulator from '../utils/useBooleanAccumulator';
import useComparableEffect from '../utils/useComparableEffect';
import useLatest from '../utils/useLatest';
import {
DownstreamContext,
RepresentationContext,
Expand Down Expand Up @@ -166,13 +167,15 @@ export default forwardRef(function GeometryRepresentation(

// --- events --- //

const { onDataAvailable } = props;
const onDataAvailable = useLatest(props.onDataAvailable);
useEffect(() => {
if (dataAvailable) {
// trigger onDataAvailable after making updates to the actor and mapper
onDataAvailable?.();
onDataAvailable.current?.();
}
}, [dataAvailable, onDataAvailable]);
// onDataAvailable is a ref
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dataAvailable]);

// --- //

Expand Down
9 changes: 6 additions & 3 deletions src/core/SliceRepresentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IDownstream, IRepresentation } from '../types';
import { compareShallowObject } from '../utils/comparators';
import useBooleanAccumulator from '../utils/useBooleanAccumulator';
import useComparableEffect from '../utils/useComparableEffect';
import useLatest from '../utils/useLatest';
import {
DownstreamContext,
RepresentationContext,
Expand Down Expand Up @@ -253,13 +254,15 @@ export default forwardRef(function SliceRepresentation(

// --- events --- //

const { onDataAvailable } = props;
const onDataAvailable = useLatest(props.onDataAvailable);
useEffect(() => {
if (dataAvailable) {
// trigger onDataAvailable after making updates to the actor and mapper
onDataAvailable?.();
onDataAvailable.current?.();
}
}, [dataAvailable, onDataAvailable]);
// onDataAvailable is a ref
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dataAvailable]);

// --- //

Expand Down
7 changes: 7 additions & 0 deletions src/utils/useLatest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useRef } from 'react';

export default function useLatest<T>(val: T) {
const ref = useRef(val);
ref.current = val;
return ref;
}

0 comments on commit 1d86e33

Please sign in to comment.