@satorijs/core
Advanced tools
Comparing version 4.3.2 to 4.3.3
@@ -44,3 +44,2 @@ import { Context, Service, z, Logger } from 'cordis'; | ||
export class Session<C extends Context = Context> { | ||
static counter: number; | ||
[Service.tracker]: { | ||
@@ -51,2 +50,3 @@ associate: string; | ||
id: number; | ||
sn: number; | ||
bot: Bot<C>; | ||
@@ -92,2 +92,3 @@ app: C['root']; | ||
interface Events<C> { | ||
'satori/meta'(): void; | ||
'internal/session'(session: GetSession<C>): void; | ||
@@ -147,2 +148,11 @@ 'interaction/command'(session: GetSession<C>): void; | ||
export { SatoriContext as Context }; | ||
declare class DisposableSet<T> { | ||
private ctx; | ||
private sn; | ||
private map1; | ||
private map2; | ||
constructor(ctx: Context); | ||
add(...values: T[]): () => void; | ||
[Symbol.iterator](): SetIterator<T>; | ||
} | ||
export class Satori<C extends Context = Context> extends Service<unknown, C> { | ||
@@ -152,4 +162,7 @@ static [Service.provide]: string; | ||
uid: string; | ||
proxyUrls: DisposableSet<string>; | ||
_internalRouter: InternalRouter<C>; | ||
_tempStore: Dict<Response>; | ||
_loginSeq: number; | ||
_sessionSeq: number; | ||
constructor(ctx?: C); | ||
@@ -160,2 +173,6 @@ bots: Bot<C>[] & Dict<Bot<C>>; | ||
handleInternalRoute(method: HTTP.Method, url: URL, headers?: Headers, body?: any): Promise<Response>; | ||
toJSON(meta?: boolean): { | ||
logins: import("@satorijs/protocol").Login[]; | ||
proxyUrls: string[]; | ||
}; | ||
} | ||
@@ -210,2 +227,3 @@ export default Satori; | ||
}; | ||
sn: number; | ||
user: User; | ||
@@ -216,3 +234,2 @@ isBot: boolean; | ||
features: string[]; | ||
proxyUrls: string[]; | ||
adapter?: Adapter<C, this>; | ||
@@ -219,0 +236,0 @@ error?: Error; |
{ | ||
"name": "@satorijs/core", | ||
"description": "Core components of Satorijs", | ||
"version": "4.3.2", | ||
"version": "4.3.3", | ||
"type": "module", | ||
@@ -62,3 +62,3 @@ "main": "lib/index.cjs", | ||
"@satorijs/element": "^3.1.7", | ||
"@satorijs/protocol": "^1.4.2", | ||
"@satorijs/protocol": "^1.5.0", | ||
"cordis": "^3.18.1", | ||
@@ -65,0 +65,0 @@ "cosmokit": "^1.6.3", |
@@ -33,2 +33,3 @@ import { clone, Dict, pick } from 'cosmokit' | ||
public sn: number | ||
public user = {} as User | ||
@@ -39,3 +40,2 @@ public isBot = true | ||
public features: string[] | ||
public proxyUrls: string[] | ||
public adapter?: Adapter<C, this> | ||
@@ -53,2 +53,3 @@ public error?: Error | ||
constructor(public ctx: C, public config: T, platform?: string) { | ||
this.sn = ++ctx.satori._loginSeq | ||
this.internal = null | ||
@@ -64,3 +65,2 @@ this._internalRouter = new InternalRouter(ctx) | ||
this.proxyUrls = [] | ||
this.features = Object.entries(Methods) | ||
@@ -253,3 +253,3 @@ .filter(([, value]) => this[value.name]) | ||
return clone({ | ||
...pick(this, ['platform', 'selfId', 'status', 'hidden', 'features', 'proxyUrls']), | ||
...pick(this, ['sn', 'platform', 'selfId', 'status', 'hidden', 'features']), | ||
// make sure `user.id` is present | ||
@@ -256,0 +256,0 @@ user: this.user.id ? this.user : undefined, |
@@ -42,2 +42,3 @@ import { Context, Logger, Service, z } from 'cordis' | ||
interface Events<C> { | ||
'satori/meta'(): void | ||
'internal/session'(session: GetSession<C>): void | ||
@@ -118,2 +119,46 @@ 'interaction/command'(session: GetSession<C>): void | ||
class DisposableSet<T> { | ||
private sn = 0 | ||
private map1 = new Map<number, T[]>() | ||
private map2 = new Map<T, Set<number>>() | ||
constructor(private ctx: Context) { | ||
defineProperty(this, Service.tracker, { | ||
property: 'ctx', | ||
}) | ||
} | ||
add(...values: T[]) { | ||
const sn = ++this.sn | ||
return this.ctx.effect(() => { | ||
let hasUpdate = false | ||
for (const value of values) { | ||
if (!this.map2.has(value)) { | ||
this.map2.set(value, new Set()) | ||
hasUpdate = true | ||
} | ||
this.map2.get(value)!.add(sn) | ||
} | ||
this.map1.set(sn, values) | ||
if (hasUpdate) this.ctx.emit('satori/meta') | ||
return () => { | ||
let hasUpdate = false | ||
this.map1.delete(sn) | ||
for (const value of values) { | ||
this.map2.get(value)!.delete(sn) | ||
if (this.map2.get(value)!.size === 0) { | ||
this.map2.delete(value) | ||
hasUpdate = true | ||
} | ||
} | ||
if (hasUpdate) this.ctx.emit('satori/meta') | ||
} | ||
}) | ||
} | ||
[Symbol.iterator]() { | ||
return new Set(([] as T[]).concat(...this.map1.values()))[Symbol.iterator]() | ||
} | ||
} | ||
export class Satori<C extends Context = Context> extends Service<unknown, C> { | ||
@@ -124,2 +169,3 @@ static [Service.provide] = 'satori' | ||
public uid = Math.random().toString(36).slice(2) | ||
public proxyUrls: DisposableSet<string> = new DisposableSet(this.ctx) | ||
@@ -129,2 +175,5 @@ public _internalRouter: InternalRouter<C> | ||
public _loginSeq = 0 | ||
public _sessionSeq = 0 | ||
constructor(ctx?: C) { | ||
@@ -203,4 +252,11 @@ super(ctx) | ||
} | ||
toJSON(meta = false) { | ||
return { | ||
logins: meta ? undefined : this.bots.map(bot => bot.toJSON()), | ||
proxyUrls: [...this.proxyUrls], | ||
} | ||
} | ||
} | ||
export default Satori |
@@ -31,4 +31,2 @@ import { Channel, Event, GuildMember, Login, Message, User } from '@satorijs/protocol' | ||
export class Session<C extends Context = Context> { | ||
static counter = 0 | ||
public [Service.tracker] = { | ||
@@ -39,3 +37,4 @@ associate: 'session', | ||
public id: number | ||
public id: number // for backward compatibility | ||
public sn: number | ||
public bot: Bot<C> | ||
@@ -51,3 +50,3 @@ public app: C['root'] | ||
this.event = event as Event | ||
this.id = ++Session.counter | ||
this.sn = this.id = ++bot.ctx.satori._sessionSeq | ||
defineProperty(this, 'bot', bot) | ||
@@ -151,3 +150,4 @@ defineProperty(this, 'app', bot.ctx.root) | ||
...clone(this.event), | ||
id: this.id, | ||
sn: this.sn, | ||
['id' as never]: this.sn, // for backward compatibility | ||
} | ||
@@ -154,0 +154,0 @@ if (event.message?.elements) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
136875
12
2886
Updated@satorijs/protocol@^1.5.0