map_concurrent major cleanup
This commit is contained in:
		
							parent
							
								
									ffb5a3b497
								
							
						
					
					
						commit
						1f3d6cdda6
					
				
					 4 changed files with 15 additions and 26 deletions
				
			
		|  | @ -4,8 +4,8 @@ | |||
|   "author": "Jörn-Michael Miehe <joern-michael.miehe@lenaisten.de>", | ||||
|   "license": "MIT", | ||||
|   "scripts": { | ||||
|     "build": "webpack --mode=development", | ||||
|     "build-release": "webpack --mode=production", | ||||
|     "build-debug": "webpack --mode=development", | ||||
|     "build": "webpack --mode=production", | ||||
|     "lint-ext": "web-ext lint --source-dir=dist", | ||||
|     "build-ext": "web-ext build --source-dir=dist --artifacts-dir=dist --overwrite-dest" | ||||
|   }, | ||||
|  |  | |||
|  | @ -13,12 +13,9 @@ type BTTV_Emote = { | |||
|  * Query BTTV API for an emote graphic | ||||
|  * | ||||
|  * @param code string associated with the emote graphic (often called "emote" itself) | ||||
|  * @returns `null` iff no emote found or error, | ||||
|  * else `[code, url]` tuple where `url` is the URL to a BTTV emote graphic | ||||
|  * @returns `null` iff no emote found or error, else URL to a BTTV emote graphic | ||||
|  */ | ||||
| export async function bttv_get_url( | ||||
|   code: string, | ||||
| ): Promise<[string, string] | null> { | ||||
| export async function bttv_get_url(code: string): Promise<string | null> { | ||||
|   // search for emotes
 | ||||
|   const res = await fetch( | ||||
|     `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; | ||||
| 
 | ||||
|   // return first emote's URL
 | ||||
|   return [ | ||||
|     code, | ||||
|     `//cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`, | ||||
|   ]; | ||||
|   return `//cdn.betterttv.net/emote/${matches[0].id}/1x.${matches[0].imageType}`; | ||||
| } | ||||
|  |  | |||
|  | @ -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. | ||||
|  * | ||||
|  | @ -12,13 +7,13 @@ type MapFunction<In, Out> = (input: In) => Promise<[In, Out] | null>; | |||
|  */ | ||||
| export async function map_concurrent<In, Out>( | ||||
|   inputs: Iterable<In>, | ||||
|   map_fn: MapFunction<In, Out>, | ||||
| ): Promise<Map<In, Out>> { | ||||
|   map_fn: (input: In) => Promise<Out | null>, | ||||
| ): Promise<Map<In, Out | null>> { | ||||
|   // 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
 | ||||
|   return new Map<In, Out>( | ||||
|     (await Promise.all(queue)).filter((job): job is [In, Out] => job !== null), | ||||
|   // create map object
 | ||||
|   return new Map<In, Out | null>( | ||||
|     [...inputs].map((input, index) => [input, outputs[index]]), | ||||
|   ); | ||||
| } | ||||
|  |  | |||
|  | @ -24,13 +24,13 @@ async function process_text(text: string): Promise<string> { | |||
|   for (const emote of matches.reverse()) { | ||||
|     const [match, code] = emote; | ||||
| 
 | ||||
|     if (!emotes.has(code)) continue; | ||||
|     // emote exists
 | ||||
|     // ensure emote exists
 | ||||
|     const url = emotes.get(code); | ||||
|     if (url == null) continue; | ||||
| 
 | ||||
|     if (emote.index == undefined) continue; | ||||
|     // position in input string is known
 | ||||
|     // ensure position in input string is known
 | ||||
|     const index = emote.index; | ||||
|     if (index == undefined) continue; | ||||
| 
 | ||||
|     text = | ||||
|       text.substring(0, index) + | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue