-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
391 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export interface H5File { | ||
export interface RemoteFile { | ||
filename: string; | ||
buffer: ArrayBuffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as H5WasmProvider } from './H5WasmProvider'; | ||
export { default as H5WasmLocalFileProvider } from './local/H5WasmLocalFileProvider'; | ||
export { Plugin } from './utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { DataProvider } from '@h5web/app'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { useMemo, useState } from 'react'; | ||
|
||
import { H5WasmLocalFileApi } from './h5wasm-local-file-api'; | ||
|
||
interface Props { | ||
file: File; | ||
} | ||
|
||
function H5WasmLocalFileProvider(props: PropsWithChildren<Props>) { | ||
const { file, children } = props; | ||
|
||
const api = useMemo(() => new H5WasmLocalFileApi(file), [file]); | ||
|
||
const [prevApi, setPrevApi] = useState(api); | ||
if (prevApi !== api) { | ||
setPrevApi(api); | ||
void prevApi.cleanUp(); // https://github.com/silx-kit/h5web/pull/1568 | ||
} | ||
|
||
return <DataProvider api={api}>{children}</DataProvider>; | ||
} | ||
|
||
export default H5WasmLocalFileProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { ValuesStoreParams } from '@h5web/app'; | ||
import { DataProviderApi } from '@h5web/app'; | ||
import type { | ||
AttributeValues, | ||
Entity, | ||
ProvidedEntity, | ||
} from '@h5web/shared/hdf5-models'; | ||
import type { Remote } from 'comlink'; | ||
|
||
import { getEnhancedError, hasBigInts, sanitizeBigInts } from '../utils'; | ||
import { getH5WasmRemote } from './remote'; | ||
import type { H5WasmWorkerAPI } from './worker'; | ||
|
||
export class H5WasmLocalFileApi extends DataProviderApi { | ||
private readonly remote: Remote<H5WasmWorkerAPI>; | ||
private readonly fileId: Promise<bigint>; | ||
|
||
public constructor(file: File) { | ||
super(file.name); | ||
|
||
this.remote = getH5WasmRemote(); | ||
this.fileId = this.remote.openFile(file); | ||
} | ||
|
||
public override async getEntity(path: string): Promise<ProvidedEntity> { | ||
return this.remote.getEntity(await this.fileId, path); | ||
} | ||
|
||
public override async getValue(params: ValuesStoreParams): Promise<unknown> { | ||
const { dataset, selection } = params; | ||
const fileId = await this.fileId; | ||
|
||
try { | ||
const value = await this.remote.getValue(fileId, dataset.path, selection); | ||
return hasBigInts(dataset.type) ? sanitizeBigInts(value) : value; | ||
} catch (error) { | ||
throw getEnhancedError(error); | ||
} | ||
} | ||
|
||
public override async getAttrValues( | ||
entity: Entity, | ||
): Promise<AttributeValues> { | ||
const fileId = await this.fileId; | ||
|
||
return Object.fromEntries( | ||
await Promise.all( | ||
entity.attributes.map<Promise<[string, unknown]>>(async ({ name }) => [ | ||
name, | ||
await this.remote.getAttrValue(fileId, entity.path, name), | ||
]), | ||
), | ||
); | ||
} | ||
|
||
public async cleanUp(): Promise<number> { | ||
return this.remote.closeFile(await this.fileId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { Remote } from 'comlink'; | ||
import { wrap } from 'comlink'; | ||
|
||
import type { H5WasmWorkerAPI } from './worker'; | ||
|
||
let remote: Remote<H5WasmWorkerAPI>; | ||
|
||
export function getH5WasmRemote() { | ||
if (remote) { | ||
return remote; | ||
} | ||
|
||
const worker = new Worker(new URL('worker.ts', import.meta.url), { | ||
type: 'module', | ||
}); | ||
|
||
remote = wrap<H5WasmWorkerAPI>(worker); | ||
return remote; | ||
} |
Oops, something went wrong.