forked from FooSoft/yomichan
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add API map type descriptions * Remove unused ApiMapInitLax * Add createApiMap function * Add extendApiMap * Support promises * Update Offscreen to use API map * Add ApiNames<> template * Add getApiMapHandler * Use getApiMapHandler in offscreen
- Loading branch information
1 parent
b83ca2f
commit ab847b1
Showing
5 changed files
with
191 additions
and
68 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (C) 2023 Yomitan Authors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
/** | ||
* @template {import('api-map').ApiSurface} [TApiSurface=never] | ||
* @param {import('api-map').ApiMapInit<TApiSurface>} init | ||
* @returns {import('api-map').ApiMap<TApiSurface>} | ||
*/ | ||
export function createApiMap(init) { | ||
return new Map(init); | ||
} | ||
|
||
/** | ||
* @template {import('api-map').ApiSurface} [TApiSurface=never] | ||
* @param {import('api-map').ApiMap<TApiSurface>} map | ||
* @param {import('api-map').ApiMapInit<TApiSurface>} init | ||
* @throws {Error} | ||
*/ | ||
export function extendApiMap(map, init) { | ||
for (const [key, value] of init) { | ||
if (map.has(key)) { throw new Error(`The handler for ${String(key)} has already been registered`); } | ||
map.set(key, value); | ||
} | ||
} | ||
|
||
/** | ||
* @template {import('api-map').ApiSurface} [TApiSurface=never] | ||
* @param {import('api-map').ApiMap<TApiSurface>} map | ||
* @param {string} name | ||
* @returns {import('api-map').ApiHandlerAny<TApiSurface>|undefined} | ||
*/ | ||
export function getApiMapHandler(map, name) { | ||
return map.get(/** @type {import('api-map').ApiNames<TApiSurface>} */ (name)); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2023 Yomitan Authors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
type ApiSurface = { | ||
[name: string]: ApiItem; | ||
}; | ||
|
||
type ApiItem = { | ||
params: void | {[name: string]: unknown}; | ||
return: unknown; | ||
}; | ||
|
||
export type ApiHandler<TApiItem extends ApiItem> = (params: TApiItem['params']) => TApiItem['return'] | Promise<TApiItem['return']>; | ||
|
||
type ApiHandlerSurface<TApiSurface extends ApiSurface> = {[name in ApiNames<TApiSurface>]: ApiHandler<TApiSurface[name]>}; | ||
|
||
export type ApiHandlerAny<TApiSurface extends ApiSurface> = ApiHandlerSurface<TApiSurface>[ApiNames<TApiSurface>]; | ||
|
||
export type ApiNames<TApiSurface extends ApiSurface> = keyof TApiSurface; | ||
|
||
export type ApiParams<TApiSurface extends ApiSurface, TName extends ApiNames<TApiSurface>> = TApiSurface[TName]['params']; | ||
|
||
export type ApiReturn<TApiSurface extends ApiSurface, TName extends ApiNames<TApiSurface>> = TApiSurface[TName]['return']; | ||
|
||
export type ApiMap<TApiSurface extends ApiSurface> = Map<ApiNames<TApiSurface>, ApiHandlerAny<TApiSurface>>; | ||
|
||
export type ApiMapInit<TApiSurface extends ApiSurface> = ApiMapInitItemAny<TApiSurface>[]; | ||
|
||
export type ApiMapInitLax<TApiSurface extends ApiSurface> = ApiMapInitLaxItem<TApiSurface>[]; | ||
|
||
export type ApiMapInitLaxItem<TApiSurface extends ApiSurface> = [ | ||
name: ApiNames<TApiSurface>, | ||
handler: ApiHandlerAny<TApiSurface>, | ||
]; | ||
|
||
type ApiMapInitItem<TApiSurface extends ApiSurface, TName extends ApiNames<TApiSurface>> = [ | ||
name: TName, | ||
handler: ApiHandler<TApiSurface[TName]>, | ||
]; | ||
|
||
type ApiMapInitItemAny<TApiSurface extends ApiSurface> = {[key in ApiNames<TApiSurface>]: ApiMapInitItem<TApiSurface, key>}[ApiNames<TApiSurface>]; |
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