|
| 1 | +import * as vizarr from "https://hms-dbmi.github.io/vizarr/index.js"; |
| 2 | +import debounce from "https://esm.sh/just-debounce-it@3"; |
| 3 | + |
| 4 | +/** |
| 5 | + * @template T |
| 6 | + * @param {import("npm:@anywidget/types").AnyModel} model |
| 7 | + * @param {any} payload |
| 8 | + * @param {{ timeout?: number }} [options] |
| 9 | + * @returns {Promise<{ data: T, buffers: DataView[] }>} |
| 10 | + */ |
| 11 | +function send(model, payload, { timeout = 3000 } = {}) { |
| 12 | + let uuid = globalThis.crypto.randomUUID(); |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + let timer = setTimeout(() => { |
| 15 | + reject(new Error(`Promise timed out after ${timeout} ms`)); |
| 16 | + model.off("msg:custom", handler); |
| 17 | + }, timeout); |
| 18 | + /** |
| 19 | + * @param {{ uuid: string, payload: T }} msg |
| 20 | + * @param {DataView[]} buffers |
| 21 | + */ |
| 22 | + function handler(msg, buffers) { |
| 23 | + if (!(msg.uuid === uuid)) return; |
| 24 | + clearTimeout(timer); |
| 25 | + resolve({ data: msg.payload, buffers }); |
| 26 | + model.off("msg:custom", handler); |
| 27 | + } |
| 28 | + model.on("msg:custom", handler); |
| 29 | + model.send({ payload, uuid }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @param {import("npm:@anywidget/types").AnyModel} model |
| 35 | + * @param {string | { id: string }} source |
| 36 | + */ |
| 37 | +function get_source(model, source) { |
| 38 | + if (typeof source === "string") { |
| 39 | + return source; |
| 40 | + } |
| 41 | + // create a python |
| 42 | + return { |
| 43 | + /** |
| 44 | + * @param {string} key |
| 45 | + * @return {Promise<ArrayBuffer>} |
| 46 | + */ |
| 47 | + async getItem(key) { |
| 48 | + const { data, buffers } = await send(model, { |
| 49 | + type: "get", |
| 50 | + source_id: source.id, |
| 51 | + key, |
| 52 | + }); |
| 53 | + if (!data.success) { |
| 54 | + throw { __zarr__: "KeyError" }; |
| 55 | + } |
| 56 | + return buffers[0].buffer; |
| 57 | + }, |
| 58 | + /** |
| 59 | + * @param {string} key |
| 60 | + * @return {Promise<boolean>} |
| 61 | + */ |
| 62 | + async containsItem(key) { |
| 63 | + const { data } = await send(model, { |
| 64 | + type: "has", |
| 65 | + source_id: source.id, |
| 66 | + key, |
| 67 | + }); |
| 68 | + return data; |
| 69 | + }, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * @typedef Model |
| 75 | + * @property {string} height |
| 76 | + * @property {ViewState=} view_state |
| 77 | + * @property {{ source: string | { id: string }}[]} _configs |
| 78 | + */ |
| 79 | + |
| 80 | +/** |
| 81 | + * @typedef ViewState |
| 82 | + * @property {number} zoom |
| 83 | + * @property {[x: number, y: number]} target |
| 84 | + */ |
| 85 | + |
| 86 | +/** @type {import("npm:@anywidget/types").Render<Model>} */ |
| 87 | +export function render({ model, el }) { |
| 88 | + let div = document.createElement("div"); |
| 89 | + { |
| 90 | + div.style.height = model.get("height"); |
| 91 | + div.style.backgroundColor = "black"; |
| 92 | + model.on("change:height", () => { |
| 93 | + div.style.height = model.get("height"); |
| 94 | + }); |
| 95 | + } |
| 96 | + let viewer = vizarr.createViewer(div); |
| 97 | + { |
| 98 | + model.on("change:view_state", () => { |
| 99 | + viewer.setViewState(model.get("view_state")); |
| 100 | + }); |
| 101 | + viewer.on( |
| 102 | + "viewStateChange", |
| 103 | + debounce((/** @type {ViewState} */ update) => { |
| 104 | + model.set("view_state", update); |
| 105 | + model.save_changes(); |
| 106 | + }, 200), |
| 107 | + ); |
| 108 | + } |
| 109 | + { |
| 110 | + // sources are append-only now |
| 111 | + for (const config of model.get("_configs")) { |
| 112 | + const source = get_source(model, config.source); |
| 113 | + viewer.addImage({ ...config, source }); |
| 114 | + } |
| 115 | + model.on("change:_configs", () => { |
| 116 | + const last = model.get("_configs").at(-1); |
| 117 | + if (!last) return; |
| 118 | + const source = get_source(model, last.source); |
| 119 | + viewer.addImage({ ...last, source }); |
| 120 | + }); |
| 121 | + } |
| 122 | + el.appendChild(div); |
| 123 | +} |
0 commit comments