Skip to content

Commit 62a3792

Browse files
committed
wip
1 parent 77ea23e commit 62a3792

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useRef, 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 prevSmelter = useRef<Smelter>();
8+
79
useEffect(() => {
810
const smelter = new Smelter(options);
11+
const smelterToTerminate = prevSmelter.current;
12+
prevSmelter.current = smelter;
913

1014
let cancel = false;
1115
(async () => {
16+
if (smelterToTerminate) {
17+
console.log("Terminate");
18+
}
19+
await smelterToTerminate?.terminate().catch(() => { });
20+
console.log("Init");
1221
await smelter.init();
1322
await smelter.start();
1423
if (!cancel) {
@@ -18,9 +27,18 @@ export function useSmelter(options: SmelterInstanceOptions): Smelter | undefined
1827

1928
return () => {
2029
cancel = true;
21-
void smelter.terminate()
2230
};
2331
}, [options?.url]);
2432

33+
useEffect(() => {
34+
return () => {
35+
if (prevSmelter.current) {
36+
console.log("Terminate unmount");
37+
}
38+
// It runs only during unmount
39+
void prevSmelter.current?.terminate().catch(() => { });
40+
}
41+
}, [])
42+
2543
return smelter;
2644
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
import { useEffect, useRef, useState } from 'react';
1+
import { useEffect, 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);
7+
88
useEffect(() => {
99
const smelter = new Smelter(options);
1010

11-
let id = count.current;
12-
count.current++;
1311
let cancel = false;
1412
(async () => {
15-
console.log('Init', id);
1613
await smelter.init();
1714
await smelter.start();
1815
if (!cancel) {
@@ -21,7 +18,6 @@ export function useSmelter(options?: SmelterOptions): Smelter | undefined {
2118
})();
2219

2320
return () => {
24-
console.log('Cancel', id);
2521
cancel = true;
2622
void smelter.terminate();
2723
};

0 commit comments

Comments
 (0)