mirror of
https://github.com/misskey-dev/misskey.git
synced 2025-01-09 22:09:40 +01:00
misskey-js側にinterfaceを置いて型エラーを解消
This commit is contained in:
parent
584e80dedd
commit
313ebcde43
4 changed files with 47 additions and 12 deletions
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
import { EventEmitter } from 'eventemitter3';
|
import { EventEmitter } from 'eventemitter3';
|
||||||
import * as Misskey from 'misskey-js';
|
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 AnyOf<T extends Record<any, any>> = T[keyof T];
|
||||||
type OmitFirst<T extends any[]> = T extends [any, ...infer R] ? R : never;
|
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のモック(なにもしない)
|
* Websocket無効化時に使うStreamのモック(なにもしない)
|
||||||
*/
|
*/
|
||||||
export class StreamMock extends EventEmitter<StreamEvents> {
|
export class StreamMock extends EventEmitter<StreamEvents> implements IStream {
|
||||||
public readonly state = 'initializing';
|
public readonly state = 'initializing';
|
||||||
|
|
||||||
constructor(...args: ConstructorParameters<typeof Misskey.Stream>) {
|
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 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();
|
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 {
|
public send<T extends keyof Channel['receives']>(type: T, body: Channel['receives'][T]): void {
|
||||||
|
|
|
@ -13,16 +13,16 @@ import { isEmbedPage } from '@/scripts/embed-page.js';
|
||||||
// heart beat interval in ms
|
// heart beat interval in ms
|
||||||
const HEART_BEAT_INTERVAL = 1000 * 60;
|
const HEART_BEAT_INTERVAL = 1000 * 60;
|
||||||
|
|
||||||
let stream: Misskey.Stream | null = null;
|
let stream: Misskey.IStream | null = null;
|
||||||
let timeoutHeartBeat: ReturnType<typeof setTimeout> | null = null;
|
let timeoutHeartBeat: number | null = null;
|
||||||
let lastHeartbeatCall = 0;
|
let lastHeartbeatCall = 0;
|
||||||
|
|
||||||
export function useStream(): Misskey.Stream {
|
export function useStream(): Misskey.IStream {
|
||||||
if (stream) return stream;
|
if (stream) return stream;
|
||||||
|
|
||||||
// TODO: No Websocketモードもここで判定
|
// TODO: No Websocketモードもここで判定
|
||||||
if (isEmbedPage()) {
|
if (isEmbedPage()) {
|
||||||
stream = markRaw(new StreamMock(wsOrigin, null) as unknown as Misskey.Stream);
|
stream = markRaw(new StreamMock(wsOrigin, null));
|
||||||
return stream;
|
return stream;
|
||||||
} else {
|
} else {
|
||||||
stream = markRaw(new Misskey.Stream(wsOrigin, $i ? {
|
stream = markRaw(new Misskey.Stream(wsOrigin, $i ? {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { type Endpoints } from './api.types.js';
|
import { type Endpoints } from './api.types.js';
|
||||||
import Stream, { Connection } from './streaming.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 Channels } from './streaming.types.js';
|
||||||
import { type Acct } from './acct.js';
|
import { type Acct } from './acct.js';
|
||||||
import * as consts from './consts.js';
|
import * as consts from './consts.js';
|
||||||
|
@ -10,6 +10,8 @@ export type {
|
||||||
Channels,
|
Channels,
|
||||||
Acct,
|
Acct,
|
||||||
StreamEvents,
|
StreamEvents,
|
||||||
|
IStream,
|
||||||
|
IChannelConnection,
|
||||||
};
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
|
|
@ -22,10 +22,26 @@ export type StreamEvents = {
|
||||||
_disconnected_: void;
|
_disconnected_: void;
|
||||||
} & BroadcastEvents;
|
} & 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
|
* Misskey stream connection
|
||||||
*/
|
*/
|
||||||
export default class Stream extends EventEmitter<StreamEvents> {
|
export default class Stream extends EventEmitter<StreamEvents> implements IStream {
|
||||||
private stream: _ReconnectingWebsocket.default;
|
private stream: _ReconnectingWebsocket.default;
|
||||||
public state: 'initializing' | 'reconnecting' | 'connected' = 'initializing';
|
public state: 'initializing' | 'reconnecting' | 'connected' = 'initializing';
|
||||||
private sharedConnectionPools: Pool[] = [];
|
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;
|
public channel: string;
|
||||||
protected stream: Stream;
|
protected stream: Stream;
|
||||||
public abstract id: string;
|
public abstract id: string;
|
||||||
|
|
Loading…
Reference in a new issue