|
1 | 1 | import diff from "microdiff";
|
2 | 2 | import p5 from "p5";
|
3 |
| -import React, { createRef, FC, memo, useState } from "react"; |
| 3 | +import React, { createRef, FC, memo, MutableRefObject } from "react"; |
| 4 | +import { useRef } from "react"; |
4 | 5 | import { useIsomorphicEffect } from "rooks";
|
5 | 6 |
|
6 | 7 | type Wrapper = HTMLDivElement;
|
7 |
| - |
8 |
| -export interface SketchProps { |
| 8 | +export type Sketch = (instance: P5CanvasInstance) => void; |
| 9 | +export type SketchProps = { |
9 | 10 | [key: string]: any;
|
10 |
| -} |
11 |
| - |
12 |
| -export interface Sketch { |
13 |
| - (instance: P5Instance): void; |
14 |
| -} |
15 |
| - |
16 |
| -export interface P5WrapperProps extends SketchProps { |
| 11 | +}; |
| 12 | +export type P5WrapperProps = SketchProps & { |
17 | 13 | sketch: Sketch;
|
18 |
| -} |
19 |
| - |
20 |
| -export interface P5Instance extends p5 { |
| 14 | +}; |
| 15 | +export type P5CanvasInstance = p5 & { |
21 | 16 | updateWithProps?: (props: SketchProps) => void;
|
22 |
| -} |
| 17 | +}; |
| 18 | + |
| 19 | +// @TODO: remove in next major version, keep for compatibility reasons for now. |
| 20 | +export type P5Instance = P5CanvasInstance; |
23 | 21 |
|
24 |
| -function createCanvas(sketch: Sketch, wrapper: Wrapper): P5Instance { |
| 22 | +function createCanvasInstance( |
| 23 | + sketch: Sketch, |
| 24 | + wrapper: Wrapper |
| 25 | +): P5CanvasInstance { |
25 | 26 | return new p5(sketch, wrapper);
|
26 | 27 | }
|
27 | 28 |
|
| 29 | +function removeCanvasInstance( |
| 30 | + canvasInstanceRef: MutableRefObject<P5CanvasInstance | undefined> |
| 31 | +) { |
| 32 | + canvasInstanceRef.current?.remove(); |
| 33 | + canvasInstanceRef.current = undefined; |
| 34 | +} |
| 35 | + |
28 | 36 | const ReactP5WrapperComponent: FC<P5WrapperProps> = ({
|
29 | 37 | sketch,
|
30 | 38 | children,
|
31 | 39 | ...props
|
32 | 40 | }) => {
|
33 | 41 | const wrapperRef = createRef<Wrapper>();
|
34 |
| - const [instance, setInstance] = useState<P5Instance>(); |
| 42 | + const instanceRef = useRef<P5CanvasInstance>(); |
35 | 43 |
|
36 | 44 | useIsomorphicEffect(() => {
|
37 | 45 | if (wrapperRef.current === null) {
|
38 | 46 | return;
|
39 | 47 | }
|
40 | 48 |
|
41 |
| - instance?.remove(); |
42 |
| - const canvas = createCanvas(sketch, wrapperRef.current); |
43 |
| - setInstance(canvas); |
| 49 | + removeCanvasInstance(instanceRef); |
| 50 | + instanceRef.current = createCanvasInstance(sketch, wrapperRef.current); |
44 | 51 | }, [sketch]);
|
45 | 52 |
|
46 |
| - useIsomorphicEffect(() => { |
47 |
| - instance?.updateWithProps?.(props); |
48 |
| - }, [props, instance]); |
| 53 | + useIsomorphicEffect( |
| 54 | + () => instanceRef.current?.updateWithProps?.(props), |
| 55 | + [props] |
| 56 | + ); |
49 | 57 |
|
50 |
| - useIsomorphicEffect(() => () => instance?.remove(), []); |
| 58 | + useIsomorphicEffect(() => () => removeCanvasInstance(instanceRef), []); |
51 | 59 |
|
52 | 60 | return <div ref={wrapperRef}>{children}</div>;
|
53 | 61 | };
|
|
0 commit comments