Skip to content

Commit 51e4d66

Browse files
committed
Cleanup
1 parent 03ad84a commit 51e4d66

4 files changed

Lines changed: 16 additions & 18 deletions

File tree

apps/example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"react-native": "0.79.2",
3030
"react-native-gesture-handler": "^2.17.1",
3131
"react-native-macos": "^0.78.3",
32-
"react-native-reanimated": "^3.17.5",
32+
"react-native-reanimated": "^3.12.1",
3333
"react-native-safe-area-context": "^5.4.0",
3434
"react-native-wgpu": "*",
3535
"teapot": "^1.0.0",

apps/example/src/Triangle/HelloTriangle.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ import { Canvas, useCanvasEffect } from "react-native-wgpu";
44

55
import { redFragWGSL, triangleVertWGSL } from "./triangle";
66

7-
const onAbort = (signal: AbortSignal, cb: () => unknown) => {
8-
signal.addEventListener('abort', cb);
9-
}
10-
117
export function HelloTriangle() {
128
const ref = useCanvasEffect(useCallback(async (signal) => {
139
const adapter = await navigator.gpu.requestAdapter();
1410
if (!adapter) {
1511
throw new Error("No adapter");
1612
}
1713
const device = await adapter.requestDevice();
18-
onAbort(signal, () => device.destroy());
1914

2015
if (signal.aborted) {
16+
device.destroy();
2117
return;
2218
}
2319

@@ -29,6 +25,7 @@ export function HelloTriangle() {
2925
canvas.height = canvas.clientHeight * PixelRatio.get();
3026

3127
if (!context) {
28+
device.destroy();
3229
throw new Error("No context");
3330
}
3431

@@ -85,6 +82,7 @@ export function HelloTriangle() {
8582
device.queue.submit([commandEncoder.finish()]);
8683

8784
context.present();
85+
device.destroy();
8886
}, []));
8987

9088
return (

packages/webgpu/src/Canvas.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import type { RefObject } from "react";
1212

1313
import WebGPUNativeView from "./WebGPUViewNativeComponent";
14-
import { useSignal } from "./useSignal";
14+
import { useImmediate } from "./useImmediate";
1515

1616
let CONTEXT_COUNTER = 1;
1717
function generateContextId() {
@@ -61,7 +61,7 @@ interface Size {
6161
}
6262

6363
const useSizeFabric = (ref: RefObject<View>, onSizeChange: (v: Size) => void) => {
64-
const [sizeSignal] = useSignal<null | Size>(null);
64+
const [sizeSignal] = useImmediate<null | Size>(null);
6565
useLayoutEffect(() => {
6666
if (!ref.current) {
6767
throw new Error("Canvas ref is null");
@@ -76,7 +76,7 @@ const useSizeFabric = (ref: RefObject<View>, onSizeChange: (v: Size) => void) =>
7676
};
7777

7878
const useSizePaper = (_ref: RefObject<View>, onSizeChange: (v: Size) => void) => {
79-
const [sizeSignal] = useSignal<null | Size>(null);
79+
const [sizeSignal] = useImmediate<null | Size>(null);
8080
const onLayout = useCallback<(event: LayoutChangeEvent) => void>(
8181
({
8282
nativeEvent: {
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useReducer, useState } from "react";
22

3-
export interface Signal<T> {
3+
export interface Immediate<T> {
44
/** Get latest value */
55
get(): T;
66
set(value: T): void;
@@ -10,12 +10,12 @@ function emptyReducer<T>(value: T): T {
1010
return value;
1111
}
1212

13-
interface CreateSignalOptions<T> {
14-
initialValue: T;
13+
interface CreateImmediateOptions<T> {
14+
readonly initialValue: T;
1515
setValue(v: T): void;
1616
}
1717

18-
function createSignal<T>(options: CreateSignalOptions<T>): Signal<T> {
18+
function createImmediate<T>(options: CreateImmediateOptions<T>): Immediate<T> {
1919
let value = options.initialValue;
2020

2121
return {
@@ -30,17 +30,17 @@ function createSignal<T>(options: CreateSignalOptions<T>): Signal<T> {
3030
}
3131

3232
/**
33-
* A replacement for `useState` that returns a "Signal", which in this case is an object
34-
* that allows to read the latest value, and to write a new value. It's object identity
33+
* A replacement for `useState` that returns an "Immediate", which in this case is an object
34+
* that allows to read the latest value, and to write a new value. Its object identity
3535
* is stable, and does not change when writing a new value. That also means that it's
3636
* not reactive (you can't depend on it in useMemo or useEffect). For that, use the
3737
* second value from the returned tuple.
3838
* @param initialValue
3939
* @returns
4040
*/
41-
export function useSignal<T>(initialValue: T): [Signal<T>, T] {
41+
export function useImmediate<T>(initialValue: T): [Immediate<T>, T] {
4242
const [value, setValue] = useState(initialValue);
43-
const [signal] = useReducer(emptyReducer<Signal<T>>, { initialValue, setValue }, createSignal<T>);
43+
const [immediate] = useReducer(emptyReducer<Immediate<T>>, { initialValue, setValue }, createImmediate<T>);
4444

45-
return [signal, value] as const;
45+
return [immediate, value] as const;
4646
}

0 commit comments

Comments
 (0)