Skip to content

Commit

Permalink
Merge branch 'main' into pwa
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt authored Jul 7, 2021
2 parents 87d21d5 + 41ce62d commit 4964b40
Showing 4 changed files with 58 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
"imjoy-rpc": "^0.2.23",
"jotai": "^1.0.0",
"p-map": "^4.0.0",
"quick-lru": "^6.0.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"reference-spec-reader": "^0.1.1",
36 changes: 36 additions & 0 deletions src/lru-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { AsyncStore } from 'zarr/types/storage/types';
import QuickLRU from 'quick-lru';

export class LRUCacheStore<S extends AsyncStore<ArrayBuffer>> {
private cache: QuickLRU<string, ArrayBuffer>;

constructor(public store: S, public maxSize: number = 100) {
this.cache = new QuickLRU({ maxSize });
}

async getItem(...args: Parameters<S['getItem']>) {
const [key, opts] = args;
if (this.cache.has(key)) {
return this.cache.get(key)!;
}
const value = await this.store.getItem(key, opts);
this.cache.set(key, value);
return value;
}

async containsItem(key: string) {
return this.cache.has(key) || this.store.containsItem(key);
}

async keys() {
return [];
}

deleteItem(key: string): never {
throw new Error('deleteItem not implemented');
}

setItem(key: string, value: ArrayBuffer): never {
throw new Error('setItem not implemented');
}
}
23 changes: 18 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ContainsArrayError, HTTPStore, openArray, openGroup, ZarrArray } from 'zarr';
import type { Group as ZarrGroup } from 'zarr';
import type { AsyncStore, Store } from 'zarr/types/storage/types';
import { Matrix4 } from '@math.gl/core/dist/esm';
import { LRUCacheStore } from './lru-store';

export const MAX_CHANNELS = 6;

@@ -17,19 +19,30 @@ export const MAGENTA_GREEN = [COLORS.magenta, COLORS.green];
export const RGB = [COLORS.red, COLORS.green, COLORS.blue];
export const CYMRGB = Object.values(COLORS).slice(0, -2);

async function normalizeStore(source: string | ZarrArray['store']) {
async function normalizeStore(source: string | Store) {
if (typeof source === 'string') {
let store: AsyncStore<ArrayBuffer>;

if (source.endsWith('.json')) {
// import custom store implementation
const { ReferenceStore } = await import('reference-spec-reader');
return ReferenceStore.fromJSON(await fetch(source).then((res) => res.json()));
const [{ ReferenceStore }, json] = await Promise.all([
import('reference-spec-reader'),
fetch(source).then((res) => res.json()),
]);

store = new ReferenceStore(json);
} else {
store = new HTTPStore(source);
}
return new HTTPStore(source);

// Wrap remote stores in a cache
return new LRUCacheStore(store);
}

return source;
}

export async function open(source: string | ZarrArray['store']) {
export async function open(source: string | Store) {
const store = await normalizeStore(source);
return openGroup(store).catch((err) => {
if (err instanceof ContainsArrayError) {

0 comments on commit 4964b40

Please sign in to comment.