refactor(shell): api: do not use sym

This commit is contained in:
Il Harper 2024-03-10 01:25:21 +08:00
parent fd6ca9440a
commit d4a91b3d9a
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC

View File

@ -2,12 +2,10 @@ import type { Methods } from '../types'
import { bgMagenta, cyan, magenta, white } from '../utils/colors' import { bgMagenta, cyan, magenta, white } from '../utils/colors'
import { l } from './logger' import { l } from './logger'
const notimplSym = Symbol('chronocat.internal.notimpl')
export type ApiImpl<M extends keyof Methods> = (( export type ApiImpl<M extends keyof Methods> = ((
...args: Methods[M][0] ...args: Methods[M][0]
) => Promise<Methods[M][1]>) & { ) => Promise<Methods[M][1]>) & {
[notimplSym]: boolean notimpl: boolean
engine: string engine: string
priority: number priority: number
@ -16,8 +14,6 @@ export type ApiImpl<M extends keyof Methods> = ((
export type Api = { export type Api = {
[M in keyof Methods]: ApiImpl<M> [M in keyof Methods]: ApiImpl<M>
} & { } & {
notimpl: typeof notimplSym
register: ( register: (
engine: string, engine: string,
priority?: number, priority?: number,
@ -41,9 +37,9 @@ const buildNotimpl = (name: string) => {
;( ;(
fn as unknown as { fn as unknown as {
[notimplSym]: boolean notimpl: boolean
} }
)[notimplSym] = true ).notimpl = true
return fn return fn
} }
@ -66,7 +62,7 @@ api.register =
priority: number = -1, priority: number = -1,
) => { ) => {
const newPriority = priority === -1 ? defaultPriority : priority const newPriority = priority === -1 ? defaultPriority : priority
if (!api[method][notimplSym]) { if (!api[method].notimpl) {
l.warn( l.warn(
`${cyan(engine)}(${newPriority}) 与 ${cyan(api[method].engine)}(${ `${cyan(engine)}(${newPriority}) 与 ${cyan(api[method].engine)}(${
api[method].priority api[method].priority