Skip to content

Commit

Permalink
feat(satori): support experimental service tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 17, 2024
1 parent 58cb423 commit b61c1e0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
44 changes: 23 additions & 21 deletions packages/core/src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clone, Dict, pick, remove } from 'cosmokit'
import { Context, Logger } from 'cordis'
import { clone, Dict, pick } from 'cosmokit'
import { Context, Logger, Service } from 'cordis'
import h from '@satorijs/element'
import { Adapter } from './adapter'
import { MessageEncoder } from './message'
Expand All @@ -22,7 +22,11 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
static reusable = true
static MessageEncoder?: new (bot: Bot, channelId: string, guildId?: string, options?: SendOptions) => MessageEncoder

public self = this
public [Service.tracker] = {
associate: 'bot',
property: 'ctx',
}

public user = {} as User
public isBot = true
public hidden = false
Expand All @@ -33,7 +37,6 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
public error?: Error
public callbacks: Dict<Function> = {}
public logger: Logger
public [Context.current]: C

// Same as `this.ctx`, but with a more specific type.
protected context: Context
Expand All @@ -42,13 +45,11 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
constructor(public ctx: C, public config: T, platform?: string) {
this.internal = null
this.context = ctx
this[Context.current] = ctx
const self = Context.associate(this, 'bot')
ctx.bots.push(self)
self.context.emit('bot-added', self)
ctx.bots.push(this)
this.context.emit('bot-added', this)
if (platform) {
self.logger = ctx.logger(platform)
self.platform = platform
this.logger = ctx.logger(platform)
this.platform = platform
}

this.proxyUrls = [`upload://temp/${ctx.satori.uid}/`]
Expand All @@ -58,18 +59,16 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login

ctx.on('ready', async () => {
await Promise.resolve()
self.dispatchLoginEvent('login-added')
return self.start()
this.dispatchLoginEvent('login-added')
return this.start()
})

ctx.on('dispose', () => self.dispose())
ctx.on('dispose', () => this.dispose())

ctx.on('interaction/button', (session) => {
const cb = this.callbacks[session.event.button.id]
if (cb) cb(session)
})

return self
}

registerUpload(path: string, callback: (path: string) => Promise<Response>) {
Expand All @@ -85,9 +84,12 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
}

dispose() {
remove(this.ctx.bots, this)
this.context.emit('bot-removed', this)
this.dispatchLoginEvent('login-removed')
const index = this.ctx.bots.findIndex(bot => bot.sid === this.sid)
if (index >= 0) {
this.ctx.bots.splice(index, 1)
this.context.emit('bot-removed', this)
this.dispatchLoginEvent('login-removed')
}
return this.stop()
}

Expand All @@ -105,7 +107,7 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
set status(value) {
if (value === this._status) return
this._status = value
if (this.ctx.bots?.includes(this)) {
if (this.ctx.bots?.some(bot => bot.sid === this.sid)) {
this.context.emit('bot-status-updated', this)
this.dispatchLoginEvent('login-updated')
}
Expand Down Expand Up @@ -143,7 +145,7 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
await this.context.parallel('bot-disconnect', this)
await this.adapter?.disconnect(this)
} catch (error) {
this.context.emit('internal/error', error)
this.context.emit(this.ctx, 'internal/error', error)
} finally {
this.offline()
}
Expand Down Expand Up @@ -216,7 +218,7 @@ export abstract class Bot<C extends Context = Context, T = any> implements Login
delete this.ctx.satori._tempStore[id]
}
}
const _dispose = this[Context.current].on('dispose', dispose)
const _dispose = this.ctx.on('dispose', dispose)
return ids.map(id => `upload://temp/${this.ctx.satori.uid}/${id}`)
}

Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, Logger, Service, z } from 'cordis'
import { Awaitable, Dict, makeArray, remove } from 'cosmokit'
import { Awaitable, defineProperty, Dict, makeArray, remove } from 'cosmokit'
import { Bot } from './bot'
import { Session } from './session'
import { HTTP } from '@cordisjs/plugin-http'
Expand Down Expand Up @@ -135,6 +135,8 @@ export class Satori<C extends Context = Context> extends Service<unknown, C> {
return this._tempStore[id] ?? { status: 404 }
})

defineProperty(this.bots, Service.tracker, {})

const self = this
;(ctx as Context).on('http/file', async function (url, options) {
if (!url.startsWith('upload://')) return
Expand Down Expand Up @@ -180,7 +182,7 @@ export class Satori<C extends Context = Context> extends Service<unknown, C> {
}

upload(path: UploadRoute['path'], callback: UploadRoute['callback'], proxyUrls: UploadRoute['path'][] = []) {
return this[Context.current].effect(() => {
return this.ctx.effect(() => {
const route: UploadRoute = { path, callback }
this._uploadRoutes.push(route)
proxyUrls.push(path)
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Channel, Event, GuildMember, Message, User } from '@satorijs/protocol'
import { defineProperty, isNullable } from 'cosmokit'
import { Context } from 'cordis'
import { Context, Service } from 'cordis'
import { Bot } from './bot'
import h from '@satorijs/element'

Expand Down Expand Up @@ -30,6 +30,11 @@ export interface Session {
export class Session<C extends Context = Context> {
static counter = 0

public [Service.tracker] = {
associate: 'session',
property: 'ctx',
}

public id: number
public bot: Bot<C>
public app: C['root']
Expand Down

0 comments on commit b61c1e0

Please sign in to comment.