From 313ebcde430b769bb6c38248fb748d72d159faef Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Mon, 24 Jun 2024 18:26:03 +0900 Subject: [PATCH] =?UTF-8?q?misskey-js=E5=81=B4=E3=81=ABinterface=E3=82=92?= =?UTF-8?q?=E7=BD=AE=E3=81=84=E3=81=A6=E5=9E=8B=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E3=82=92=E8=A7=A3=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/frontend/src/scripts/stream-mock.ts | 16 ++++++---- packages/frontend/src/stream.ts | 8 ++--- packages/misskey-js/src/index.ts | 4 ++- packages/misskey-js/src/streaming.ts | 31 ++++++++++++++++++-- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/packages/frontend/src/scripts/stream-mock.ts b/packages/frontend/src/scripts/stream-mock.ts index 75b4a2c823..cb0e607fcb 100644 --- a/packages/frontend/src/scripts/stream-mock.ts +++ b/packages/frontend/src/scripts/stream-mock.ts @@ -5,7 +5,7 @@ import { EventEmitter } from 'eventemitter3'; import * as Misskey from 'misskey-js'; -import type { Channels, StreamEvents } from 'misskey-js'; +import type { Channels, StreamEvents, IStream, IChannelConnection } from 'misskey-js'; type AnyOf> = T[keyof T]; type OmitFirst = T extends [any, ...infer R] ? R : never; @@ -13,7 +13,7 @@ type OmitFirst = T extends [any, ...infer R] ? R : never; /** * Websocket無効化時に使うStreamのモック(なにもしない) */ -export class StreamMock extends EventEmitter { +export class StreamMock extends EventEmitter implements IStream { public readonly state = 'initializing'; constructor(...args: ConstructorParameters) { @@ -57,12 +57,18 @@ export class StreamMock extends EventEmitter { } } -class ChannelConnectionMock = any> extends EventEmitter { +class ChannelConnectionMock = any> extends EventEmitter implements IChannelConnection { public id = ''; + public name?: string; // for debug + public inCount = 0; // for debug + public outCount = 0; // for debug + public channel: string; - constructor(stream: StreamMock, ...args: OmitFirst>>) { + constructor(stream: IStream, ...args: OmitFirst>>) { super(); - // do nothing + + this.channel = args[0]; + this.name = args[1]; } public send(type: T, body: Channel['receives'][T]): void { diff --git a/packages/frontend/src/stream.ts b/packages/frontend/src/stream.ts index e1986c8598..2723857e38 100644 --- a/packages/frontend/src/stream.ts +++ b/packages/frontend/src/stream.ts @@ -13,16 +13,16 @@ import { isEmbedPage } from '@/scripts/embed-page.js'; // heart beat interval in ms const HEART_BEAT_INTERVAL = 1000 * 60; -let stream: Misskey.Stream | null = null; -let timeoutHeartBeat: ReturnType | null = null; +let stream: Misskey.IStream | null = null; +let timeoutHeartBeat: number | null = null; let lastHeartbeatCall = 0; -export function useStream(): Misskey.Stream { +export function useStream(): Misskey.IStream { if (stream) return stream; // TODO: No Websocketモードもここで判定 if (isEmbedPage()) { - stream = markRaw(new StreamMock(wsOrigin, null) as unknown as Misskey.Stream); + stream = markRaw(new StreamMock(wsOrigin, null)); return stream; } else { stream = markRaw(new Misskey.Stream(wsOrigin, $i ? { diff --git a/packages/misskey-js/src/index.ts b/packages/misskey-js/src/index.ts index a34d86bf95..9b5828fcab 100644 --- a/packages/misskey-js/src/index.ts +++ b/packages/misskey-js/src/index.ts @@ -1,6 +1,6 @@ import { type Endpoints } from './api.types.js'; import Stream, { Connection } from './streaming.js'; -import { type StreamEvents } from './streaming.js'; +import type { StreamEvents, IStream, IChannelConnection } from './streaming.js'; import { type Channels } from './streaming.types.js'; import { type Acct } from './acct.js'; import * as consts from './consts.js'; @@ -10,6 +10,8 @@ export type { Channels, Acct, StreamEvents, + IStream, + IChannelConnection, }; export { diff --git a/packages/misskey-js/src/streaming.ts b/packages/misskey-js/src/streaming.ts index f5528e333c..fdf71daebc 100644 --- a/packages/misskey-js/src/streaming.ts +++ b/packages/misskey-js/src/streaming.ts @@ -22,10 +22,26 @@ export type StreamEvents = { _disconnected_: void; } & BroadcastEvents; +export interface IStream extends EventEmitter { + state: 'initializing' | 'reconnecting' | 'connected'; + + useChannel(channel: C, params?: Channels[C]['params'], name?: string): IChannelConnection; + removeSharedConnection(connection: SharedConnection): void; + removeSharedConnectionPool(pool: Pool): void; + disconnectToChannel(connection: NonSharedConnection): void; + send(typeOrPayload: string): void; + send(typeOrPayload: string, payload: any): void; + send(typeOrPayload: Record | any[]): void; + send(typeOrPayload: string | Record | any[], payload?: any): void; + ping(): void; + heartbeat(): void; + close(): void; +}; + /** * Misskey stream connection */ -export default class Stream extends EventEmitter { +export default class Stream extends EventEmitter implements IStream { private stream: _ReconnectingWebsocket.default; public state: 'initializing' | 'reconnecting' | 'connected' = 'initializing'; private sharedConnectionPools: Pool[] = []; @@ -275,7 +291,18 @@ class Pool { } } -export abstract class Connection = any> extends EventEmitter { +export interface IChannelConnection = any> extends EventEmitter { + id: string; + name?: string; + inCount: number; + outCount: number; + channel: string; + + send(type: T, body: Channel['receives'][T]): void; + dispose(): void; +} + +export abstract class Connection = any> extends EventEmitter implements IChannelConnection { public channel: string; protected stream: Stream; public abstract id: string;