-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat: Jupyter comms server #145
Open
manzt
wants to merge
3
commits into
main
Choose a base branch
from
manzt/jupyter-comms
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,41 +1,104 @@ | ||
import hglib from "https://esm.sh/[email protected]?deps=react@17,react-dom@17,pixi.js@6"; | ||
// import hglib from "https://esm.sh/[email protected]?deps=react@17,react-dom@17,pixi.js@6"; | ||
import * as hglib from "http://localhost:5173/app/scripts/hglib.jsx"; | ||
import { v4 } from "https://esm.sh/@lukeed/uuid@2"; | ||
|
||
// Make sure plugins are registered and enabled | ||
window.higlassDataFetchersByType = window.higlassDataFetchersByType || {}; | ||
|
||
/** | ||
* @param {{ | ||
* xDomain: [number, number], | ||
* yDomain: [number, number], | ||
* }} location | ||
* @template T | ||
* @param {import("npm:@anyiwdget/types").AnyModel} model | ||
* @param {unknown} payload | ||
* @param {{ timeout?: number }} [options] | ||
* @returns {Promise<{ data: T, buffers: DataView[] }>} | ||
*/ | ||
function toPts({ xDomain, yDomain }) { | ||
let [x, xe] = xDomain; | ||
let [y, ye] = yDomain; | ||
return [x, xe, y, ye]; | ||
function send(model, payload, { timeout = 3000 } = {}) { | ||
let uuid = globalThis.crypto.randomUUID(); | ||
return new Promise((resolve, reject) => { | ||
let timer = setTimeout(() => { | ||
reject(new Error(`Promise timed out after ${timeout} ms`)); | ||
model.off("msg:custom", handler); | ||
}, timeout); | ||
/** | ||
* @param {{ uuid: string, payload: T }} msg | ||
* @param {DataView[]} buffers | ||
*/ | ||
function handler(msg, buffers) { | ||
if (!(msg.uuid === uuid)) return; | ||
clearTimeout(timer); | ||
resolve({ data: msg.payload, buffers }); | ||
model.off("msg:custom", handler); | ||
} | ||
model.on("msg:custom", handler); | ||
model.send({ payload, uuid }); | ||
}); | ||
} | ||
|
||
export async function render({ model, el }) { | ||
let viewconf = model.get("_viewconf"); | ||
let options = model.get("_options") ?? {}; | ||
let api = await hglib.viewer(el, viewconf, options); | ||
/** | ||
* Detects server { server: 'jupyter' }, and creates a custom data entry for it. | ||
* @example | ||
* resolveJupyterServers({ views: [{ tracks: { top: [{ server: 'jupyter', tilesetUid: 'abc' }] } }] }, 'jupyter-123') | ||
* // { views: [{ tracks: { top: [{ tilesetUid: 'abc', data: { type: 'jupyter-123', tilesetUid: 'abc' } }] } }] } | ||
*/ | ||
function resolveJupyterServers(viewConfig, dataFetcherId) { | ||
let copy = JSON.parse(JSON.stringify(viewConfig)); | ||
for (let view of copy.views) { | ||
for (let track of Object.values(view.tracks).flat()) { | ||
if (track?.server === "jupyter") { | ||
delete track.server; | ||
track.data = track.data || {}; | ||
track.data.type = dataFetcherId; | ||
track.data.tilesetUid = track.tilesetUid; | ||
} | ||
} | ||
} | ||
return copy; | ||
} | ||
|
||
model.on("msg:custom", (msg) => { | ||
msg = JSON.parse(msg); | ||
let [fn, ...args] = msg; | ||
api[fn](...args); | ||
}); | ||
function assert(condition, message) { | ||
if (!condition) throw new Error(message); | ||
} | ||
|
||
if (viewconf.views.length === 1) { | ||
api.on("location", (loc) => { | ||
model.set("location", toPts(loc)); | ||
model.save_changes(); | ||
}, viewconf.views[0].uid); | ||
} else { | ||
viewconf.views.forEach((view, idx) => { | ||
api.on("location", (loc) => { | ||
let copy = model.get("location").slice(); | ||
copy[idx] = toPts(loc); | ||
model.set("location", copy); | ||
model.save_changes(); | ||
}, view.uid); | ||
function createDataFetcherForModel(model) { | ||
return function createDataFetcher(hgc, dataConfig, pubSub) { | ||
let config = { ...dataConfig, server: "jupyter" }; | ||
return new hgc.dataFetchers.DataFetcher(config, pubSub, { | ||
async fetchTiles({ id, server, tileIds }) { | ||
let { data } = await send(model, { type: "tiles", tileIds }); | ||
let result = hgc.services.tileResponseToData(data, "jupyter", tileIds); | ||
return result; | ||
}, | ||
async fetchTilesetInfo({ server, tilesetUid }) { | ||
assert(server === "jupyter", "must be a jupyter server"); | ||
let url = `${server}-${tilesetUid}`; | ||
let { data } = await send(model, { type: "tileset_info", tilesetUid }); | ||
return data; | ||
}, | ||
registerTileset() { | ||
throw new Error("Not implemented"); | ||
}, | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
export default () => { | ||
let id = `jupyter-${v4().split("-")[0]}`; | ||
return { | ||
async initialize({ model }) { | ||
let tsId = model.get("_ts"); | ||
let tsModel = await model.widget_manager.get_model( | ||
tsId.slice("IPY_MODEL_".length) | ||
); | ||
window.higlassDataFetchersByType[tsId] = { | ||
name: id, | ||
dataFetcher: createDataFetcherForModel(tsModel), | ||
}; | ||
}, | ||
async render({ model, el }) { | ||
let viewconf = model.get("_viewconf"); | ||
let options = model.get("_options") ?? {}; | ||
let resolved = resolveJupyterServers(viewconf, model.get("_ts")); | ||
let api = await hglib.viewer(el, resolved, options); | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing with Vite locally and PR from higlass