Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trying to close file twice in strict mode #1615

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
loichuder marked this conversation as resolved.
Show resolved Hide resolved
return h5wasm.ccall('H5Fclose', 'number', ['bigint'], [fileId]);
}

Expand Down