From b95775041ce92d3884c15d4d0822dab7270b92d0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 27 Jun 2021 21:17:38 +0900 Subject: [PATCH] Improve type --- src/streaming.ts | 19 ++++++++++--------- src/streaming.types.ts | 13 +++++++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/streaming.ts b/src/streaming.ts index c1a4d2ba89..565cea1b64 100644 --- a/src/streaming.ts +++ b/src/streaming.ts @@ -3,7 +3,7 @@ import { EventEmitter } from 'eventemitter3'; import ReconnectingWebsocket from 'reconnecting-websocket'; import { stringify } from 'querystring'; import { markRaw } from '@vue/reactivity'; -import { BroadcasrEvents, ChannelDef } from './streaming.types'; +import { BroadcasrEvents, Channels } from './streaming.types'; function urlQuery(obj: {}): string { return stringify(Object.entries(obj) @@ -49,7 +49,7 @@ export default class Stream extends EventEmitter { } @autobind - public useChannel(channel: C, params?: any, name?: string): Connection { + public useChannel(channel: C, params?: Channels[C]['params'], name?: string): Connection { if (params) { return this.connectToChannel(channel, params); } else { @@ -58,7 +58,7 @@ export default class Stream extends EventEmitter { } @autobind - public useSharedConnection(channel: C, name?: string): SharedConnection { + private useSharedConnection(channel: C, name?: string): SharedConnection { let pool = this.sharedConnectionPools.find(p => p.channel === channel); if (pool == null) { @@ -82,7 +82,7 @@ export default class Stream extends EventEmitter { } @autobind - public connectToChannel(channel: C, params?: any): NonSharedConnection { + private connectToChannel(channel: C, params: Channels[C]['params']): NonSharedConnection { const connection = markRaw(new NonSharedConnection(this, channel, params)); this.nonSharedConnections.push(connection); return connection; @@ -177,6 +177,7 @@ export default class Stream extends EventEmitter { let idCounter = 0; +// TODO: これらのクラスを Stream クラスの内部クラスにすれば余計なメンバをpublicにしないで済むかも? class Pool { public channel: string; public id: string; @@ -246,7 +247,7 @@ class Pool { } } -abstract class Connection = any> extends EventEmitter { +abstract class Connection extends EventEmitter { public channel: string; protected stream: Stream; public abstract id: string; @@ -280,7 +281,7 @@ abstract class Connection = any> extends Even public abstract dispose(): void; } -class SharedConnection extends Connection { +class SharedConnection extends Connection { private pool: Pool; public get id(): string { @@ -307,11 +308,11 @@ class SharedConnection extends Connection { } } -class NonSharedConnection extends Connection { +class NonSharedConnection extends Connection { public id: string; - protected params: any; + protected params: Channel['params']; - constructor(stream: Stream, channel: string, params?: any) { + constructor(stream: Stream, channel: string, params: Channel['params']) { super(stream, channel); this.params = params; diff --git a/src/streaming.types.ts b/src/streaming.types.ts index a45aadb89d..b346be3bd4 100644 --- a/src/streaming.types.ts +++ b/src/streaming.types.ts @@ -1,7 +1,8 @@ -import { CustomEmoji, DriveFile, MeDetailed, MessagingMessage, Note, Notification, PageEvent, User } from './entities'; +import { CustomEmoji, DriveFile, MeDetailed, MessagingMessage, Note, Notification, PageEvent, User, UserGroup } from './entities'; -export type ChannelDef = { +export type Channels = { main: { + params: null; events: { notification: (payload: Notification) => void; mention: (payload: Note) => void; @@ -30,26 +31,34 @@ export type ChannelDef = { }; }; homeTimeline: { + params: null; events: { note: (payload: Note) => void; }; }; localTimeline: { + params: null; events: { note: (payload: Note) => void; }; }; hybridTimeline: { + params: null; events: { note: (payload: Note) => void; }; }; globalTimeline: { + params: null; events: { note: (payload: Note) => void; }; }; messaging: { + params: { + otherparty?: User['id'] | null; + group?: UserGroup['id'] | null; + }; events: { message: (payload: MessagingMessage) => void; deleted: (payload: MessagingMessage['id']) => void;