map_concurrent major cleanup

This commit is contained in:
Jörn-Michael Miehe 2023-12-30 16:01:23 +00:00
parent ffb5a3b497
commit 1f3d6cdda6
4 changed files with 15 additions and 26 deletions

View file

@ -4,8 +4,8 @@
"author": "Jörn-Michael Miehe <joern-michael.miehe@lenaisten.de>", "author": "Jörn-Michael Miehe <joern-michael.miehe@lenaisten.de>",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"build": "webpack --mode=development", "build-debug": "webpack --mode=development",
"build-release": "webpack --mode=production", "build": "webpack --mode=production",
"lint-ext": "web-ext lint --source-dir=dist", "lint-ext": "web-ext lint --source-dir=dist",
"build-ext": "web-ext build --source-dir=dist --artifacts-dir=dist --overwrite-dest" "build-ext": "web-ext build --source-dir=dist --artifacts-dir=dist --overwrite-dest"
}, },

View file

@ -13,12 +13,9 @@ type BTTV_Emote = {
* Query BTTV API for an emote graphic * Query BTTV API for an emote graphic
* *
* @param code string associated with the emote graphic (often called "emote" itself) * @param code string associated with the emote graphic (often called "emote" itself)
* @returns `null` iff no emote found or error, * @returns `null` iff no emote found or error, else URL to a BTTV emote graphic
* else `[code, url]` tuple where `url` is the URL to a BTTV emote graphic
*/ */
export async function bttv_get_url( export async function bttv_get_url(code: string): Promise<string | null> {
code: string,
): Promise<[string, string] | null> {
// search for emotes // search for emotes
const res = await fetch( const res = await fetch(
`https://api.betterttv.net/3/emotes/shared/search?query=${code}&offset=0&limit=50`, `https://api.betterttv.net/3/emotes/shared/search?query=${code}&offset=0&limit=50`,
@ -37,8 +34,5 @@ export async function bttv_get_url(
if (matches.length <= 0) return null; if (matches.length <= 0) return null;
// return first emote's URL // return first emote's URL
return [ return `//cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`;
code,
`//cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`,
];
} }

View file

@ -1,8 +1,3 @@
/**
* Async function mapping some `input` to an `[input, output]` tuple or a `null` if no output exists
*/
type MapFunction<In, Out> = (input: In) => Promise<[In, Out] | null>;
/** /**
* Concurrently create some mapping of input to output elements. * Concurrently create some mapping of input to output elements.
* *
@ -12,13 +7,13 @@ type MapFunction<In, Out> = (input: In) => Promise<[In, Out] | null>;
*/ */
export async function map_concurrent<In, Out>( export async function map_concurrent<In, Out>(
inputs: Iterable<In>, inputs: Iterable<In>,
map_fn: MapFunction<In, Out>, map_fn: (input: In) => Promise<Out | null>,
): Promise<Map<In, Out>> { ): Promise<Map<In, Out | null>> {
// apply map_fn to inputs // apply map_fn to inputs
const queue = [...inputs].map((input) => map_fn(input)); const outputs = await Promise.all([...inputs].map((input) => map_fn(input)));
// wait for calls to finish // create map object
return new Map<In, Out>( return new Map<In, Out | null>(
(await Promise.all(queue)).filter((job): job is [In, Out] => job !== null), [...inputs].map((input, index) => [input, outputs[index]]),
); );
} }

View file

@ -24,13 +24,13 @@ async function process_text(text: string): Promise<string> {
for (const emote of matches.reverse()) { for (const emote of matches.reverse()) {
const [match, code] = emote; const [match, code] = emote;
if (!emotes.has(code)) continue; // ensure emote exists
// emote exists
const url = emotes.get(code); const url = emotes.get(code);
if (url == null) continue;
if (emote.index == undefined) continue; // ensure position in input string is known
// position in input string is known
const index = emote.index; const index = emote.index;
if (index == undefined) continue;
text = text =
text.substring(0, index) + text.substring(0, index) +