Skip to content

Commit 77ea23e

Browse files
committed
[ts-sdk] Add useSmelter hook to web client
Also updated wasm `useSmelter` hook
1 parent 7076e2f commit 77ea23e

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import { useEffect, useRef, useState } from 'react';
1+
import { useEffect, useState } from 'react';
22
import Smelter from '../smelter/live';
33
import type { SmelterInstanceOptions } from '../manager';
44

55
export function useSmelter(options: SmelterInstanceOptions): Smelter | undefined {
66
const [smelter, setSmelter] = useState<Smelter>();
7-
const cleanUpPromise = useRef<Promise<void>>();
8-
97
useEffect(() => {
108
const smelter = new Smelter(options);
119

1210
let cancel = false;
13-
// TODO(noituri): Add smelter.restart()
14-
const promise = (async () => {
15-
await cleanUpPromise.current;
16-
11+
(async () => {
1712
await smelter.init();
1813
await smelter.start();
1914
if (!cancel) {
@@ -23,12 +18,9 @@ export function useSmelter(options: SmelterInstanceOptions): Smelter | undefined
2318

2419
return () => {
2520
cancel = true;
26-
cleanUpPromise.current = (async () => {
27-
await promise.catch(() => {});
28-
await smelter.terminate();
29-
})();
21+
void smelter.terminate()
3022
};
31-
}, [options]);
23+
}, [options?.url]);
3224

3325
return smelter;
3426
}

ts/smelter-web-wasm/src/hooks/useSmelter.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useRef, useState } from 'react';
22
import type { SmelterOptions } from '../compositor/compositor';
33
import Smelter from '../compositor/compositor';
44

55
export function useSmelter(options?: SmelterOptions): Smelter | undefined {
66
const [smelter, setSmelter] = useState<Smelter>();
7+
const count = useRef(1);
78
useEffect(() => {
89
const smelter = new Smelter(options);
910

11+
let id = count.current;
12+
count.current++;
1013
let cancel = false;
11-
const promise = (async () => {
14+
(async () => {
15+
console.log('Init', id);
1216
await smelter.init();
1317
await smelter.start();
1418
if (!cancel) {
@@ -17,11 +21,9 @@ export function useSmelter(options?: SmelterOptions): Smelter | undefined {
1721
})();
1822

1923
return () => {
24+
console.log('Cancel', id);
2025
cancel = true;
21-
void (async () => {
22-
await promise.catch(() => {});
23-
await smelter.terminate();
24-
})();
26+
void smelter.terminate();
2527
};
2628
}, [options?.framerate, (options?.framerate as any)?.num, (options?.framerate as any)?.den]);
2729
return smelter;

0 commit comments

Comments
 (0)