Skip to content

Commit

Permalink
Fix h5wasm file clean-up in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Jun 10, 2022
1 parent 3673162 commit 26f69d5
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/h5wasm/src/H5WasmProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import { DataProvider } from '@h5web/app';
import type { ReactNode } from 'react';
import { useEffect, useMemo } from 'react';
import type { PropsWithChildren } from 'react';
import { useState, useEffect } from 'react';

import { H5WasmApi } from './h5wasm-api';

interface Props {
filename: string;
buffer: ArrayBuffer;
children: ReactNode;
}

function H5WasmProvider(props: Props) {
function H5WasmProvider(props: PropsWithChildren<Props>) {
const { filename, buffer, children } = props;

const api = useMemo(
() => new H5WasmApi(filename, buffer),
[filename, buffer]
);
const [api, setApi] = useState<H5WasmApi>();

useEffect(() => {
return () => void api.cleanUp();
}, [api]);
const h5wasmApi = new H5WasmApi(filename, buffer);
setApi(h5wasmApi);

return () => void h5wasmApi.cleanUp();
}, [filename, buffer]);

if (!api) {
return null;
}

return <DataProvider api={api}>{children}</DataProvider>;
}
Expand Down

0 comments on commit 26f69d5

Please sign in to comment.