From 26f69d58c14d8f5b103f64f54049aab43006aad5 Mon Sep 17 00:00:00 2001 From: Axel Bocciarelli Date: Fri, 10 Jun 2022 14:49:29 +0200 Subject: [PATCH] Fix h5wasm file clean-up in strict mode --- packages/h5wasm/src/H5WasmProvider.tsx | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/h5wasm/src/H5WasmProvider.tsx b/packages/h5wasm/src/H5WasmProvider.tsx index a9eff8e90..43edd7a58 100644 --- a/packages/h5wasm/src/H5WasmProvider.tsx +++ b/packages/h5wasm/src/H5WasmProvider.tsx @@ -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) { const { filename, buffer, children } = props; - const api = useMemo( - () => new H5WasmApi(filename, buffer), - [filename, buffer] - ); + const [api, setApi] = useState(); 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 {children}; }