From 238a5fd907fa8c1ee7c8ecdfe347cd0d7ebc3985 Mon Sep 17 00:00:00 2001 From: Shigma Date: Mon, 19 Feb 2024 02:50:58 +0800 Subject: [PATCH] feat(core): enhance service method typings --- packages/cordis/src/index.ts | 11 ++++++++- packages/core/src/service.ts | 44 ++++++++++++++++-------------------- packages/core/src/utils.ts | 1 - packages/hmr/src/index.ts | 6 ++--- 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/packages/cordis/src/index.ts b/packages/cordis/src/index.ts index 8e4d1dc..03453da 100644 --- a/packages/cordis/src/index.ts +++ b/packages/cordis/src/index.ts @@ -32,7 +32,16 @@ export class Context extends core.Context { } export abstract class Service extends core.Service { - public logger!: Logger + /** @deprecated use `this.ctx.logger` instead */ + public logger: Logger + + constructor(config: T) + constructor(ctx: C, config: T) + constructor(ctx: C, name: string, immediate?: boolean) + constructor(...args: any[]) { + super(args[0], args[1], args[2]) + this.logger = this.ctx.logger(this.name) + } [core.Service.setup]() { this.ctx = new Context() as C diff --git a/packages/core/src/service.ts b/packages/core/src/service.ts index 1112b45..a89baae 100644 --- a/packages/core/src/service.ts +++ b/packages/core/src/service.ts @@ -1,9 +1,8 @@ import { Awaitable, defineProperty } from 'cosmokit' import { Context } from './context.ts' -import { createCallable, joinPrototype, symbols } from './index.ts' +import { createCallable, joinPrototype, symbols } from './utils.ts' export abstract class Service { - static readonly init: unique symbol = symbols.init as any static readonly setup: unique symbol = symbols.setup as any static readonly invoke: unique symbol = symbols.invoke as any static readonly extend: unique symbol = symbols.extend as any @@ -15,14 +14,14 @@ export abstract class Service { protected fork?(ctx: C, config: any): void protected ctx!: C - protected [Context.trace]!: C + protected [symbols.trace]!: C public name!: string public config!: T constructor(config: T) - constructor(ctx: C | undefined, config: T) - constructor(ctx: C | undefined, name: string, immediate?: boolean) + constructor(ctx: C, config: T) + constructor(ctx: C, name: string, immediate?: boolean) constructor(...args: any[]) { let _ctx: C | undefined, name: string | undefined, immediate: boolean | undefined, config: any if (Context.is(args[0])) { @@ -51,12 +50,11 @@ export abstract class Service { self.name = name self.config = config defineProperty(self, symbols.trace, self.ctx) - self[symbols.init]() self.ctx.provide(name) self.ctx.runtime.name = name if (immediate) { - if (_ctx) self[Context.expose] = name + if (_ctx) self[symbols.expose] = name else self.ctx[name] = self } @@ -71,34 +69,32 @@ export abstract class Service { return Context.associate(self, name) } - [Context.filter](ctx: Context) { - return ctx[Context.shadow][this.name] === this.ctx[Context.shadow][this.name] + protected [symbols.filter](ctx: Context) { + return ctx[symbols.shadow][this.name] === this.ctx[symbols.shadow][this.name] } - static [Symbol.hasInstance](instance: any) { - let constructor = instance.constructor - while (constructor) { - if (constructor === this) return true - constructor = Object.getPrototypeOf(constructor) - } - return false - } - - [symbols.setup]() { + protected [symbols.setup]() { this.ctx = new Context() as C } - [symbols.init]() {} - - [symbols.extend](props?: any) { - const caller = this[Context.trace] + protected [symbols.extend](props?: any) { + const caller = this[symbols.trace] let self: any if (this[Service.invoke]) { self = createCallable(this.name, this) } else { self = Object.create(this) } - defineProperty(self, Context.trace, caller) + defineProperty(self, symbols.trace, caller) return Context.associate(Object.assign(self, props), this.name) } + + static [Symbol.hasInstance](instance: any) { + let constructor = instance.constructor + while (constructor) { + if (constructor === this) return true + constructor = Object.getPrototypeOf(constructor) + } + return false + } } diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 8b27a3e..fb0bff8 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -13,7 +13,6 @@ export const symbols = { intercept: Symbol.for('cordis.intercept') as typeof Context.intercept, // service symbols - init: Symbol.for('cordis.init') as typeof Service.init, setup: Symbol.for('cordis.setup') as typeof Service.setup, invoke: Symbol.for('cordis.invoke') as typeof Service.invoke, extend: Symbol.for('cordis.extend') as typeof Service.extend, diff --git a/packages/hmr/src/index.ts b/packages/hmr/src/index.ts index 2647d8d..07312f5 100644 --- a/packages/hmr/src/index.ts +++ b/packages/hmr/src/index.ts @@ -37,11 +37,11 @@ interface Reload { } class Watcher extends Service { - static name = 'hmr' static inject = ['loader'] private base: string private watcher!: FSWatcher + private initialURL!: string /** * changes from externals E will always trigger a full reload @@ -69,9 +69,7 @@ class Watcher extends Service { /** stashed changes */ private stashed = new Set() - private initialURL!: string - - constructor(ctx: Context, private config: Watcher.Config) { + constructor(ctx: Context, public config: Watcher.Config) { super(ctx, 'hmr') this.base = resolve(ctx.baseDir, config.base || '') this.initialURL = pathToFileURL(ctx.loader.filename).href