Skip to content

Commit

Permalink
Merge pull request #1615 from silx-kit/fix-clean-up
Browse files Browse the repository at this point in the history
Fix trying to close file twice in strict mode
  • Loading branch information
axelboc authored Apr 18, 2024
2 parents 2e6db09 + e537b39 commit fb8c6c1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
20 changes: 10 additions & 10 deletions packages/h5wasm/src/H5WasmProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DataProviderApi } from '@h5web/app';
import { DataProvider } from '@h5web/app';
import type { PropsWithChildren } from 'react';
import { useMemo, useState } from 'react';
import { useMemo, useRef } from 'react';

import { H5WasmApi } from './h5wasm-api';
import type { Plugin } from './models';
Expand All @@ -16,16 +16,16 @@ interface Props {
function H5WasmProvider(props: PropsWithChildren<Props>) {
const { filename, buffer, getExportURL, getPlugin, children } = props;

const api = useMemo(
() => new H5WasmApi(filename, buffer, getExportURL, getPlugin),
[buffer, filename, getExportURL, getPlugin],
);
const prevApiRef = useRef<H5WasmApi>();

const [prevApi, setPrevApi] = useState(api);
if (prevApi !== api) {
setPrevApi(api);
void prevApi.cleanUp(); // https://github.com/silx-kit/h5web/pull/1568
}
const api = useMemo(() => {
const newApi = new H5WasmApi(filename, buffer, getExportURL, getPlugin);

void prevApiRef.current?.cleanUp();
prevApiRef.current = newApi;

return newApi;
}, [buffer, filename, getExportURL, getPlugin]);

return <DataProvider api={api}>{children}</DataProvider>;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/h5wasm/src/h5wasm-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ export class H5WasmApi extends DataProviderApi {
}

public async cleanUp(): Promise<void> {
const module = await h5wasmReady;
const file = await this.file;
file.close();
// `file.close()` flushes the file, which is not needed
module.ccall('H5Fclose', 'number', ['bigint'], [file.file_id]);
}

private async initH5Wasm(): Promise<typeof Module> {
Expand Down
20 changes: 10 additions & 10 deletions packages/h5wasm/src/local/H5WasmLocalFileProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DataProviderApi } from '@h5web/app';
import { DataProvider } from '@h5web/app';
import type { PropsWithChildren } from 'react';
import { useMemo, useState } from 'react';
import { useMemo, useRef } from 'react';

import type { Plugin } from '../models';
import { H5WasmLocalFileApi } from './h5wasm-local-file-api';
Expand All @@ -15,16 +15,16 @@ interface Props {
function H5WasmLocalFileProvider(props: PropsWithChildren<Props>) {
const { file, getExportURL, getPlugin, children } = props;

const api = useMemo(
() => new H5WasmLocalFileApi(file, getExportURL, getPlugin),
[file, getExportURL, getPlugin],
);
const prevApiRef = useRef<H5WasmLocalFileApi>();

const [prevApi, setPrevApi] = useState(api);
if (prevApi !== api) {
setPrevApi(api);
void prevApi.cleanUp(); // https://github.com/silx-kit/h5web/pull/1568
}
const api = useMemo(() => {
const newApi = new H5WasmLocalFileApi(file, getExportURL, getPlugin);

void prevApiRef.current?.cleanUp();
prevApiRef.current = newApi;

return newApi;
}, [file, getExportURL, getPlugin]);

return <DataProvider api={api}>{children}</DataProvider>;
}
Expand Down
1 change: 0 additions & 1 deletion packages/h5wasm/src/local/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ async function openFile(file: File): Promise<bigint> {

async function closeFile(fileId: bigint): Promise<number> {
const h5wasm = await h5wasmReady;
h5wasm.flush(fileId);
return h5wasm.ccall('H5Fclose', 'number', ['bigint'], [fileId]);
}

Expand Down

0 comments on commit fb8c6c1

Please sign in to comment.