Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/higlass/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type HGC = {
tileResponseToData<T>(
inputData: Record<string, T>,
server: string,
theseTileIds: Array<string>,
tileIds: Array<string>,
): Record<string, unknown>;
};
};
Expand Down
72 changes: 61 additions & 11 deletions src/higlass/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ async function registerJupyterHiGlassDataFetcher(model) {
/** @type {(...args: ConstructorParameters<PluginDataFetcherConstructor>) => DataFetcher} */
function DataFetcher(hgc, dataConfig, pubSub) {
let config = { ...dataConfig, server: NAME };

return new hgc.dataFetchers.DataFetcher(config, pubSub, {
async fetchTilesetInfo({ server, tilesetUid }) {
assert(server === NAME, "must be a jupyter server");
Expand All @@ -202,17 +203,29 @@ async function registerJupyterHiGlassDataFetcher(model) {
});
return response.payload;
},
async fetchTiles({ tileIds }) {
let response = await sendCustomMessage(tModel, {
payload: { type: "tiles", tileIds },
});
let result = hgc.services.tileResponseToData(
response.payload,
NAME,
tileIds,
);
return result;
},
fetchTiles: consolidator(
/** @param {Array<WithResolvers<{ tileIds: Array<string> }, Record<string, any>>>} requests */
async (requests) => {
let tileIds = [...new Set(requests.flatMap((r) => r.data.tileIds))];
let response = await sendCustomMessage(tModel, {
payload: { type: "tiles", tileIds },
});
let tiles = hgc.services.tileResponseToData(
response.payload,
NAME,
tileIds,
);
for (let request of requests) {
/** @type {Record<string, unknown>} */
const requestData = {};
for (let id of request.data.tileIds) {
let tileData = tiles[id];
if (tileData) requestData[id] = tileData;
}
request.resolve(requestData);
}
},
),
registerTileset() {
throw new Error("Not implemented");
},
Expand Down Expand Up @@ -302,3 +315,40 @@ export default {
};
},
};

/**
* @template T
* @template U
* @typedef {{ data: T, resolve: (success: U) => void, reject: (err: unknown) => void }} WithResolvers
*/

/**
* Collects multiple calls within the same frame to processes them as a batch.
*
* The provided `processBatch` function receives an array of items, each with
* associated resolvers for fulfilling the original request.
*
* @template T
* @template U
* @param {(batch: Array<WithResolvers<T, U>>) => void} processBatch - A function to process each batch.
* @returns {(item: T) => Promise<U>} - A function to enqueue items.
*/
function consolidator(processBatch) {
/** @type {Array<WithResolvers<T, U>>} */
let pending = [];
/** @type {number} */
let id = 0;

function run() {
processBatch(pending);
pending = [];
id = 0;
}

return function enqueue(data) {
id = id || requestAnimationFrame(() => run());
let { promise, resolve, reject } = Promise.withResolvers();
pending.push({ data, resolve, reject });
return promise;
};
}