Skip to content

Commit

Permalink
feat(core): enhance service method typings
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 18, 2024
1 parent 7e9e378 commit 238a5fd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
11 changes: 10 additions & 1 deletion packages/cordis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ export class Context extends core.Context {
}

export abstract class Service<T = unknown, C extends Context = Context> extends core.Service<T, C> {
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
Expand Down
44 changes: 20 additions & 24 deletions packages/core/src/service.ts
Original file line number Diff line number Diff line change
@@ -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<T = unknown, C extends Context = Context> {
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
Expand All @@ -15,14 +14,14 @@ export abstract class Service<T = unknown, C extends Context = Context> {
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<C>(args[0])) {
Expand Down Expand Up @@ -51,12 +50,11 @@ export abstract class Service<T = unknown, C extends Context = Context> {
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
}

Expand All @@ -71,34 +69,32 @@ export abstract class Service<T = unknown, C extends Context = Context> {
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
}
}
1 change: 0 additions & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions packages/hmr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,9 +69,7 @@ class Watcher extends Service {
/** stashed changes */
private stashed = new Set<string>()

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
Expand Down

0 comments on commit 238a5fd

Please sign in to comment.