diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bcb3c65 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "editor.defaultFormatter": "esbenp.prettier-vscode", + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + }, + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" + } +} diff --git a/packages/shell/src/services/api.ts b/packages/shell/src/services/api.ts index 9e56e4b..37242fe 100644 --- a/packages/shell/src/services/api.ts +++ b/packages/shell/src/services/api.ts @@ -29,16 +29,21 @@ export type Api = { const buildNotimpl = (name: string) => { const fn = () => - l.error(new Error(`没有引擎提供 ${bgMagenta(white(name))}。可能没有加载所需的引擎?`), { - code: 2159, - throw: true, - }) + l.error( + new Error( + `没有引擎提供 ${bgMagenta(white(name))}。可能没有加载所需的引擎?`, + ), + { + code: 2159, + throw: true, + }, + ) - ; ( - fn as unknown as { - [notimplSym]: boolean - } - )[notimplSym] = true + ;( + fn as unknown as { + [notimplSym]: boolean + } + )[notimplSym] = true return fn } @@ -46,7 +51,7 @@ const buildNotimpl = (name: string) => { const handler: ProxyHandler = { get: (target, name) => typeof name === 'symbol' || - Object.prototype.hasOwnProperty.call(target, name) + Object.prototype.hasOwnProperty.call(target, name) ? target[name as keyof Methods] : buildNotimpl(name), } @@ -55,20 +60,23 @@ export const api = new Proxy({} as Api, handler) api.register = (engine: string, defaultPriority: number = 0) => - ( - method: M, - impl: (...args: Methods[M][0]) => Promise, - priority: number = -1, - ) => { - const newPriority = priority === -1 ? defaultPriority : priority - if (api[method]) { - l.warn(`${cyan(engine)}(${newPriority}) 与 ${cyan(api[method].engine)}(${api[method].priority - }) 重复注册了方法 ${bgMagenta(white(method))},将采用 ${newPriority > api[method].priority ? engine : api[method].engine} 的版本。`) + ( + method: M, + impl: (...args: Methods[M][0]) => Promise, + priority: number = -1, + ) => { + const newPriority = priority === -1 ? defaultPriority : priority + if (api[method]) { + l.warn( + `${cyan(engine)}(${newPriority}) 与 ${cyan(api[method].engine)}(${ + api[method].priority + }) 重复注册了方法 ${bgMagenta(white(method))},将采用 ${newPriority > api[method].priority ? engine : api[method].engine} 的版本。`, + ) - if (newPriority < api[method].priority) return - } - // FIXME: Do not use type assertion - api[method] = impl /* ApiImpl */ as Api[M] - api[method].engine = engine - api[method].priority = newPriority + if (newPriority < api[method].priority) return } + // FIXME: Do not use type assertion + api[method] = impl /* ApiImpl */ as Api[M] + api[method].engine = engine + api[method].priority = newPriority + }