Skip to content

Commit d95b4f8

Browse files
committed
replace class with proxy creator; use in loader
1 parent 9ef4d06 commit d95b4f8

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

src/loaders/OmeZarrLoader.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import type {
4444
NumericZarrArray,
4545
} from "./zarr_utils/types.js";
4646
import { VolumeLoadError, VolumeLoadErrorType, wrapVolumeLoadError } from "./VolumeLoadError.js";
47+
import cachingArray from "./zarr_utils/CachingArray.js";
4748
import { validateOMEZarrMetadata } from "./zarr_utils/validation.js";
4849

4950
const CHUNK_REQUEST_CANCEL_REASON = "chunk request cancelled";
@@ -169,7 +170,7 @@ class OMEZarrLoader extends ThreadableVolumeLoader {
169170

170171
// Create one `ZarrSource` per URL
171172
const sourceProms = urlsArr.map(async (url, i) => {
172-
const store = new WrappedStore<RequestInit>(new FetchStore(url), cache, queue);
173+
const store = new WrappedStore<RequestInit>(new FetchStore(url), queue);
173174
const root = zarr.root(store);
174175

175176
const group = await zarr
@@ -191,6 +192,7 @@ class OMEZarrLoader extends ThreadableVolumeLoader {
191192
const lvlProms = multiscaleMetadata.datasets.map(({ path }) =>
192193
zarr
193194
.open(root.resolve(path), { kind: "array" })
195+
.then((array) => (cache ? cachingArray(array, cache) : array))
194196
.catch(
195197
wrapVolumeLoadError(
196198
`Failed to open scale level ${path} of OME-Zarr data at ${url}`,
+19-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
import { Array, ArrayMetadata, Chunk, DataType } from "@zarrita/core";
1+
import { Array as ZarrArray, Chunk, DataType } from "@zarrita/core";
22
import VolumeCache, { CacheData } from "../../VolumeCache";
3-
import { AbsolutePath, Readable } from "@zarrita/storage";
3+
import { Readable } from "@zarrita/storage";
44
import { pathIsToMetadata } from "./utils";
55

6-
const ZARR_EXTS = [".zarray", ".zgroup", ".zattrs", "zarr.json"];
7-
86
const isChunk = (data: CacheData): data is Chunk<DataType> => (data as Chunk<DataType>).data !== undefined;
97

10-
export default class CachingArray<T extends DataType, Store extends Readable = Readable> extends Array<T, Store> {
11-
constructor(store: Store, path: AbsolutePath, metadata: ArrayMetadata<T>, private cache?: VolumeCache) {
12-
super(store, path, metadata);
13-
}
14-
15-
async getChunk(coords: number[], opts?: Parameters<Store["get"]>[1]): Promise<Chunk<T>> {
16-
if (!this.cache || pathIsToMetadata(this.path)) {
17-
return super.getChunk(coords, opts);
8+
export default function cachingArray<T extends DataType, Store extends Readable = Readable>(
9+
array: ZarrArray<T, Store>,
10+
cache: VolumeCache
11+
): ZarrArray<T, Store> {
12+
const getChunk = async (coords: number[], opts?: Parameters<Store["get"]>[1]): Promise<Chunk<T>> => {
13+
if (pathIsToMetadata(array.path)) {
14+
return array.getChunk(coords, opts);
1815
}
1916

20-
const trailingSlash = this.path.endsWith("/") ? "" : "/";
21-
const fullKey = this.path + trailingSlash + coords.join(",");
22-
const cacheResult = this.cache.get(fullKey);
17+
const trailingSlash = array.path.endsWith("/") ? "" : "/";
18+
const fullKey = array.path + trailingSlash + coords.join(",");
19+
const cacheResult = cache.get(fullKey);
2320
if (cacheResult && isChunk(cacheResult)) {
2421
return cacheResult;
2522
}
2623

27-
const result = await super.getChunk(coords, opts);
28-
this.cache.insert(fullKey, result);
24+
const result = await array.getChunk(coords, opts);
25+
cache.insert(fullKey, result);
2926
return result;
30-
}
27+
};
28+
29+
return new Proxy(array, {
30+
get: (target, prop, reciever) => (prop === "getChunk" ? getChunk : Reflect.get(target, prop, reciever)),
31+
});
3132
}

0 commit comments

Comments
 (0)