1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-12 21:59:02 +09:00

misskey-js側にinterfaceを置いて型エラーを解消

This commit is contained in:
kakkokari-gtyih 2024-06-24 18:26:03 +09:00
parent 584e80dedd
commit 313ebcde43
4 changed files with 47 additions and 12 deletions

View File

@ -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 extends Record<any, any>> = T[keyof T];
type OmitFirst<T extends any[]> = T extends [any, ...infer R] ? R : never;
@ -13,7 +13,7 @@ type OmitFirst<T extends any[]> = T extends [any, ...infer R] ? R : never;
/**
* Websocket無効化時に使うStreamのモック
*/
export class StreamMock extends EventEmitter<StreamEvents> {
export class StreamMock extends EventEmitter<StreamEvents> implements IStream {
public readonly state = 'initializing';
constructor(...args: ConstructorParameters<typeof Misskey.Stream>) {
@ -57,12 +57,18 @@ export class StreamMock extends EventEmitter<StreamEvents> {
}
}
class ChannelConnectionMock<Channel extends AnyOf<Channels> = any> extends EventEmitter<Channel['events']> {
class ChannelConnectionMock<Channel extends AnyOf<Channels> = any> extends EventEmitter<Channel['events']> implements IChannelConnection<Channel> {
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<ConstructorParameters<typeof Misskey.ChannelConnection<Channel>>>) {
constructor(stream: IStream, ...args: OmitFirst<ConstructorParameters<typeof Misskey.ChannelConnection<Channel>>>) {
super();
// do nothing
this.channel = args[0];
this.name = args[1];
}
public send<T extends keyof Channel['receives']>(type: T, body: Channel['receives'][T]): void {

View File

@ -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<typeof setTimeout> | 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 ? {

View File

@ -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 {

View File

@ -22,10 +22,26 @@ export type StreamEvents = {
_disconnected_: void;
} & BroadcastEvents;
export interface IStream extends EventEmitter<StreamEvents> {
state: 'initializing' | 'reconnecting' | 'connected';
useChannel<C extends keyof Channels>(channel: C, params?: Channels[C]['params'], name?: string): IChannelConnection<Channels[C]>;
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<string, any> | any[]): void;
send(typeOrPayload: string | Record<string, any> | any[], payload?: any): void;
ping(): void;
heartbeat(): void;
close(): void;
};
/**
* Misskey stream connection
*/
export default class Stream extends EventEmitter<StreamEvents> {
export default class Stream extends EventEmitter<StreamEvents> 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<Channel extends AnyOf<Channels> = any> extends EventEmitter<Channel['events']> {
export interface IChannelConnection<Channel extends AnyOf<Channels> = any> extends EventEmitter<Channel['events']> {
id: string;
name?: string;
inCount: number;
outCount: number;
channel: string;
send<T extends keyof Channel['receives']>(type: T, body: Channel['receives'][T]): void;
dispose(): void;
}
export abstract class Connection<Channel extends AnyOf<Channels> = any> extends EventEmitter<Channel['events']> implements IChannelConnection<Channel> {
public channel: string;
protected stream: Stream;
public abstract id: string;