Improve type

This commit is contained in:
syuilo 2021-06-27 21:17:38 +09:00
parent 36bfaeba12
commit b95775041c
2 changed files with 21 additions and 11 deletions

View file

@ -3,7 +3,7 @@ import { EventEmitter } from 'eventemitter3';
import ReconnectingWebsocket from 'reconnecting-websocket'; import ReconnectingWebsocket from 'reconnecting-websocket';
import { stringify } from 'querystring'; import { stringify } from 'querystring';
import { markRaw } from '@vue/reactivity'; import { markRaw } from '@vue/reactivity';
import { BroadcasrEvents, ChannelDef } from './streaming.types'; import { BroadcasrEvents, Channels } from './streaming.types';
function urlQuery(obj: {}): string { function urlQuery(obj: {}): string {
return stringify(Object.entries(obj) return stringify(Object.entries(obj)
@ -49,7 +49,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
} }
@autobind @autobind
public useChannel<C extends keyof ChannelDef>(channel: C, params?: any, name?: string): Connection<ChannelDef[C]['events']> { public useChannel<C extends keyof Channels>(channel: C, params?: Channels[C]['params'], name?: string): Connection<Channels[C]> {
if (params) { if (params) {
return this.connectToChannel(channel, params); return this.connectToChannel(channel, params);
} else { } else {
@ -58,7 +58,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
} }
@autobind @autobind
public useSharedConnection<C extends keyof ChannelDef>(channel: C, name?: string): SharedConnection<ChannelDef[C]['events']> { private useSharedConnection<C extends keyof Channels>(channel: C, name?: string): SharedConnection<Channels[C]> {
let pool = this.sharedConnectionPools.find(p => p.channel === channel); let pool = this.sharedConnectionPools.find(p => p.channel === channel);
if (pool == null) { if (pool == null) {
@ -82,7 +82,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
} }
@autobind @autobind
public connectToChannel<C extends keyof ChannelDef>(channel: C, params?: any): NonSharedConnection<ChannelDef[C]['events']> { private connectToChannel<C extends keyof Channels>(channel: C, params: Channels[C]['params']): NonSharedConnection<Channels[C]> {
const connection = markRaw(new NonSharedConnection(this, channel, params)); const connection = markRaw(new NonSharedConnection(this, channel, params));
this.nonSharedConnections.push(connection); this.nonSharedConnections.push(connection);
return connection; return connection;
@ -177,6 +177,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
let idCounter = 0; let idCounter = 0;
// TODO: これらのクラスを Stream クラスの内部クラスにすれば余計なメンバをpublicにしないで済むかも
class Pool { class Pool {
public channel: string; public channel: string;
public id: string; public id: string;
@ -246,7 +247,7 @@ class Pool {
} }
} }
abstract class Connection<Events extends Record<string, any> = any> extends EventEmitter<Events> { abstract class Connection<Channel extends Channels[keyof Channels] = any> extends EventEmitter<Channel['events']> {
public channel: string; public channel: string;
protected stream: Stream; protected stream: Stream;
public abstract id: string; public abstract id: string;
@ -280,7 +281,7 @@ abstract class Connection<Events extends Record<string, any> = any> extends Even
public abstract dispose(): void; public abstract dispose(): void;
} }
class SharedConnection<Events = any> extends Connection<Events> { class SharedConnection<Channel extends Channels[keyof Channels] = any> extends Connection<Channel> {
private pool: Pool; private pool: Pool;
public get id(): string { public get id(): string {
@ -307,11 +308,11 @@ class SharedConnection<Events = any> extends Connection<Events> {
} }
} }
class NonSharedConnection<Events = any> extends Connection<Events> { class NonSharedConnection<Channel extends Channels[keyof Channels] = any> extends Connection<Channel> {
public id: string; 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); super(stream, channel);
this.params = params; this.params = params;

View file

@ -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: { main: {
params: null;
events: { events: {
notification: (payload: Notification) => void; notification: (payload: Notification) => void;
mention: (payload: Note) => void; mention: (payload: Note) => void;
@ -30,26 +31,34 @@ export type ChannelDef = {
}; };
}; };
homeTimeline: { homeTimeline: {
params: null;
events: { events: {
note: (payload: Note) => void; note: (payload: Note) => void;
}; };
}; };
localTimeline: { localTimeline: {
params: null;
events: { events: {
note: (payload: Note) => void; note: (payload: Note) => void;
}; };
}; };
hybridTimeline: { hybridTimeline: {
params: null;
events: { events: {
note: (payload: Note) => void; note: (payload: Note) => void;
}; };
}; };
globalTimeline: { globalTimeline: {
params: null;
events: { events: {
note: (payload: Note) => void; note: (payload: Note) => void;
}; };
}; };
messaging: { messaging: {
params: {
otherparty?: User['id'] | null;
group?: UserGroup['id'] | null;
};
events: { events: {
message: (payload: MessagingMessage) => void; message: (payload: MessagingMessage) => void;
deleted: (payload: MessagingMessage['id']) => void; deleted: (payload: MessagingMessage['id']) => void;