From d349e8cc104d4ccbfed7e160b6e0b18f3380bc4c Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Sun, 14 Jan 2024 08:22:37 -0600 Subject: [PATCH] feat(gui): batch status requests (#238) --- gui/package.json | 1 + gui/src/client/api.ts | 18 ++++++++++++++++-- gui/yarn.lock | 12 ++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/gui/package.json b/gui/package.json index a02e18e7..21e71e7d 100644 --- a/gui/package.json +++ b/gui/package.json @@ -16,6 +16,7 @@ "@tanstack/react-query": "^4.0.5", "@types/lodash": "^4.14.192", "@types/node": "^18.15.11", + "@yornaath/batshit": "^0.9.0", "allotment": "^1.19.5", "browser-bunyan": "^1.8.0", "exifreader": "^4.13.0", diff --git a/gui/src/client/api.ts b/gui/src/client/api.ts index ccd1a11a..62975d07 100644 --- a/gui/src/client/api.ts +++ b/gui/src/client/api.ts @@ -1,8 +1,9 @@ /* eslint-disable max-lines */ import { doesExist, InvalidArgumentError, Maybe } from '@apextoaster/js-utils'; +import { create as batcher, windowScheduler, keyResolver } from '@yornaath/batshit'; import { ServerParams } from '../config.js'; -import { FIXED_FLOAT, FIXED_INTEGER, STATUS_SUCCESS } from '../constants.js'; +import { FIXED_FLOAT, FIXED_INTEGER, POLL_TIME, STATUS_SUCCESS } from '../constants.js'; import { JobResponse, JobResponseWithRetry, SuccessJobResponse } from '../types/api-v2.js'; import { FilterResponse, @@ -129,7 +130,7 @@ export function makeClient(root: string, token: Maybe = undefined, f = f return f(url, options).then((res) => parseJobResponse(root, res)); } - return { + const client = { async extras(): Promise { const path = makeApiUrl(root, 'extras'); @@ -500,6 +501,19 @@ export function makeClient(root: string, token: Maybe = undefined, f = f return new URL(joinPath('output', image.outputs[index]), root).toString(); }, }; + + const batchStatus = batcher({ + fetcher: async (jobs: Array) => client.status(jobs), + resolver: keyResolver('name'), + scheduler: windowScheduler(POLL_TIME), + }); + + return { + ...client, + async status(keys): Promise> { + return Promise.all(keys.map((key) => batchStatus.fetch(key))); + }, + }; } /** diff --git a/gui/yarn.lock b/gui/yarn.lock index d36e3edc..1aa99af6 100644 --- a/gui/yarn.lock +++ b/gui/yarn.lock @@ -791,6 +791,18 @@ resolved "https://registry.yarnpkg.com/@xobotyi/scrollbar-width/-/scrollbar-width-1.9.5.tgz#80224a6919272f405b87913ca13b92929bdf3c4d" integrity sha512-N8tkAACJx2ww8vFMneJmaAgmjAG1tnVBZJRLRcx061tmsLRZHSEZSLuGWnwPtunsSLvSqXQ2wfp7Mgqg1I+2dQ== +"@yornaath/batshit-devtools@^1.6.0": + version "1.6.0" + resolved "https://registry.yarnpkg.com/@yornaath/batshit-devtools/-/batshit-devtools-1.6.0.tgz#a40de72569d9571e4fcf3be9ea623f54203e7c99" + integrity sha512-ckKxrdfuFSRFz54tYU+VkD1eZOOaxa92U4eKOPtlxc1itB12Cqb1Dmk8Rqv6mARxp8YvocVGKbccrXoCwH2Lew== + +"@yornaath/batshit@^0.9.0": + version "0.9.0" + resolved "https://registry.yarnpkg.com/@yornaath/batshit/-/batshit-0.9.0.tgz#4d078e6ed3cadb5d8c4ae762363f40b6f61ad7d0" + integrity sha512-QdI1hQUPlpFT+o1iKLi9fQEKCli8c3UG7q4Ih8MP4jn+pyAjbfC4xaK5PwqSsSZgT0YeAZ0GWbbO9+1xAtWXlQ== + dependencies: + "@yornaath/batshit-devtools" "^1.6.0" + acorn-jsx@^5.3.2: version "5.3.2" resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937"