送信側に型エラーがないことを3回確認した

This commit is contained in:
tamaina 2021-09-06 23:18:16 +09:00
parent f6853b2254
commit 4793d1cc56
2 changed files with 58 additions and 47 deletions

View File

@ -1,27 +1,27 @@
import { User } from '@/models/entities/user';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import Emitter from 'strict-event-emitter-types'; import Emitter from 'strict-event-emitter-types';
import { Channel } from '@/models/entities/channel'; import { Channel } from '@/models/entities/channel';
import { User } from '@/models/entities/user';
import { UserProfile } from '@/models/entities/user-profile'; import { UserProfile } from '@/models/entities/user-profile';
import { PackedUser } from '@/models/repositories/user'; import { PackedUser } from '@/models/repositories/user';
import { PackedNotification } from '@/models/repositories/notification'; import { PackedNotification } from '@/models/repositories/notification';
import { Note } from '@/models/entities/note';
import { PackedNote } from '@/models/repositories/note'; import { PackedNote } from '@/models/repositories/note';
import { Antenna } from '@/models/entities/antenna'; import { Antenna } from '@/models/entities/antenna';
import { DriveFile } from '@/models/entities/drive-file'; import { DriveFile } from '@/models/entities/drive-file';
import { PackedDriveFile } from '@/models/repositories/drive-file'; import { PackedDriveFile } from '@/models/repositories/drive-file';
import { PackedDriveFolder } from '@/models/repositories/drive-folder';
import { DriveFolder } from '@/models/entities/drive-folder'; import { DriveFolder } from '@/models/entities/drive-folder';
import { Note } from '@/models/entities/note'; import { PackedDriveFolder } from '@/models/repositories/drive-folder';
import { Emoji } from '@/models/entities/emoji'; import { Emoji } from '@/models/entities/emoji';
import { PackedEmoji } from '@/models/repositories/emoji';
import { UserList } from '@/models/entities/user-list'; import { UserList } from '@/models/entities/user-list';
import { MessagingMessage } from '@/models/entities/messaging-message'; import { MessagingMessage } from '@/models/entities/messaging-message';
import { PackedMessagingMessage } from '@/models/repositories/messaging-message'; import { PackedMessagingMessage } from '@/models/repositories/messaging-message';
import { UserGroup } from '@/models/entities/user-group'; import { UserGroup } from '@/models/entities/user-group';
import { ReversiGame } from '@/models/entities/games/reversi/game';
import { PackedReversiGame } from '@/models/repositories/games/reversi/game'; import { PackedReversiGame } from '@/models/repositories/games/reversi/game';
import { PackedReversiMatching } from '@/models/repositories/games/reversi/matching'; import { PackedReversiMatching } from '@/models/repositories/games/reversi/matching';
import { ReversiGame } from '@/models/entities/games/reversi/game';
import { AbuseUserReport } from '@/models/entities/abuse-user-report'; import { AbuseUserReport } from '@/models/entities/abuse-user-report';
import { PackedEmoji } from '@/models/repositories/emoji';
import { PackedSignin } from '@/models/repositories/signin'; import { PackedSignin } from '@/models/repositories/signin';
import { Page } from '@/models/entities/page'; import { Page } from '@/models/entities/page';
@ -32,9 +32,6 @@ type EventUnionFromDictionary<
U = { [K in keyof T]: { type: K; body: T[K]} } U = { [K in keyof T]: { type: K; body: T[K]} }
> = U[keyof U]; > = U[keyof U];
// (payload: P) => void からPを取り出す
type Payload<T extends (payload: any) => void> = T extends (payload: infer P) => void ? P : never;
//#region Stream type-body definitions //#region Stream type-body definitions
export interface InternalStreamTypes { export interface InternalStreamTypes {
antennaCreated: Antenna; antennaCreated: Antenna;
@ -43,7 +40,9 @@ export interface InternalStreamTypes {
} }
export interface BroadcastTypes { export interface BroadcastTypes {
emojiAdded: PackedEmoji; emojiAdded: {
emoji: PackedEmoji;
};
} }
export interface UserStreamTypes { export interface UserStreamTypes {
@ -116,34 +115,28 @@ export interface DriveStreamTypes {
export interface NoteStreamTypes { export interface NoteStreamTypes {
pollVoted: { pollVoted: {
id: Note['id']; choice: number;
body: { userId: User['id'];
choice: number;
userId: User['id'];
};
}; };
deleted: { deleted: {
id: Note['id']; deletedAt: Date;
body: {
deletedAt: Date;
};
}; };
reacted: { reacted: {
id: Note['id']; reaction: string;
body: { emoji?: Emoji;
reaction: string; userId: User['id'];
emoji?: Emoji;
userId: User['id'];
};
}; };
unreacted: { unreacted: {
id: Note['id']; reaction: string;
body: { userId: User['id'];
reaction: string;
userId: User['id'];
}
}; };
} }
type NoteStreamEventTypes = {
[key in keyof NoteStreamTypes]: {
id: Note['id'];
body: NoteStreamTypes[key];
};
};
export interface ChannelStreamTypes { export interface ChannelStreamTypes {
typing: User['id']; typing: User['id'];
@ -188,7 +181,7 @@ export interface ReversiStreamTypes {
export interface ReversiGameStreamTypes { export interface ReversiGameStreamTypes {
started: PackedReversiGame; started: PackedReversiGame;
ended: { ended: {
winnerId: User['id'], winnerId?: User['id'] | null,
game: PackedReversiGame; game: PackedReversiGame;
}; };
updateSettings: { updateSettings: {
@ -255,7 +248,7 @@ interface StreamMessages {
}; };
note: { note: {
name: `noteStream:${Note['id']}`; name: `noteStream:${Note['id']}`;
spec: EventUnionFromDictionary<NoteStreamTypes>; spec: EventUnionFromDictionary<NoteStreamEventTypes>;
}; };
channel: { channel: {
name: `channelStream:${Channel['id']}`; name: `channelStream:${Channel['id']}`;
@ -302,6 +295,6 @@ type EventsGenerater<K extends keyof StreamMessages> = { [key in StreamMessages[
type NotesStreamEvent = { notesStream: (e: PackedNote) => void }; type NotesStreamEvent = { notesStream: (e: PackedNote) => void };
export type StreamEventEmitter = Emitter<EventEmitter, EventsGenerater<keyof StreamMessages> & NotesStreamEvent>; export type StreamEventEmitter = Emitter<EventEmitter, EventsGenerater<keyof StreamMessages> & NotesStreamEvent>;
// Channel Union // provide stream channels union
type ChannelsUnionGenerater<K extends keyof StreamMessages> = StreamMessages[K]['name']; type ChannelsUnionGenerater<K extends keyof StreamMessages> = StreamMessages[K]['name'];
export type Channels = ChannelsUnionGenerater<keyof StreamMessages> | 'notesStream'; export type StreamChannels = ChannelsUnionGenerater<keyof StreamMessages> | 'notesStream';

View File

@ -7,10 +7,28 @@ import { UserGroup } from '@/models/entities/user-group';
import config from '@/config/index'; import config from '@/config/index';
import { Antenna } from '@/models/entities/antenna'; import { Antenna } from '@/models/entities/antenna';
import { Channel } from '@/models/entities/channel'; import { Channel } from '@/models/entities/channel';
import { BroadcastTypes, Channels, InternalStreamTypes, MainStreamTypes, UserStreamTypes } from '@/server/api/stream/types'; import {
StreamChannels,
AdminStreamTypes,
AntennaStreamTypes,
BroadcastTypes,
ChannelStreamTypes,
DriveStreamTypes,
GroupMessagingStreamTypes,
InternalStreamTypes,
MainStreamTypes,
MessagingIndexStreamTypes,
MessagingStreamTypes,
NoteStreamTypes,
ReversiGameStreamTypes,
ReversiStreamTypes,
UserListStreamTypes,
UserStreamTypes
} from '@/server/api/stream/types';
import { PackedNote } from '@/models/repositories/note';
class Publisher { class Publisher {
private publish = (channel: Channels, type: string | null, value?: any): void => { private publish = (channel: StreamChannels, type: string | null, value?: any): void => {
const message = type == null ? value : value == null ? const message = type == null ? value : value == null ?
{ type: type, body: null } : { type: type, body: null } :
{ type: type, body: value }; { type: type, body: value };
@ -37,54 +55,54 @@ class Publisher {
this.publish(`mainStream:${userId}`, type, typeof value === 'undefined' ? null : value); this.publish(`mainStream:${userId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishDriveStream = (userId: User['id'], type: string, value?: any): void => { public publishDriveStream = <K extends keyof DriveStreamTypes>(userId: User['id'], type: K, value?: DriveStreamTypes[K]): void => {
this.publish(`driveStream:${userId}`, type, typeof value === 'undefined' ? null : value); this.publish(`driveStream:${userId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishNoteStream = (noteId: Note['id'], type: string, value: any): void => { public publishNoteStream = <K extends keyof NoteStreamTypes>(noteId: Note['id'], type: K, value: NoteStreamTypes[K]): void => {
this.publish(`noteStream:${noteId}`, type, { this.publish(`noteStream:${noteId}`, type, {
id: noteId, id: noteId,
body: value body: value
}); });
} }
public publishChannelStream = (channelId: Channel['id'], type: string, value?: any): void => { public publishChannelStream = <K extends keyof ChannelStreamTypes>(channelId: Channel['id'], type: K, value?: ChannelStreamTypes[K]): void => {
this.publish(`channelStream:${channelId}`, type, typeof value === 'undefined' ? null : value); this.publish(`channelStream:${channelId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishUserListStream = (listId: UserList['id'], type: string, value?: any): void => { public publishUserListStream = <K extends keyof UserListStreamTypes>(listId: UserList['id'], type: K, value?: UserListStreamTypes[K]): void => {
this.publish(`userListStream:${listId}`, type, typeof value === 'undefined' ? null : value); this.publish(`userListStream:${listId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishAntennaStream = (antennaId: Antenna['id'], type: string, value?: any): void => { public publishAntennaStream = <K extends keyof AntennaStreamTypes>(antennaId: Antenna['id'], type: K, value?: AntennaStreamTypes[K]): void => {
this.publish(`antennaStream:${antennaId}`, type, typeof value === 'undefined' ? null : value); this.publish(`antennaStream:${antennaId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishMessagingStream = (userId: User['id'], otherpartyId: User['id'], type: string, value?: any): void => { public publishMessagingStream = <K extends keyof MessagingStreamTypes>(userId: User['id'], otherpartyId: User['id'], type: K, value?: MessagingStreamTypes[K]): void => {
this.publish(`messagingStream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value); this.publish(`messagingStream:${userId}-${otherpartyId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishGroupMessagingStream = (groupId: UserGroup['id'], type: string, value?: any): void => { public publishGroupMessagingStream = <K extends keyof GroupMessagingStreamTypes>(groupId: UserGroup['id'], type: K, value?: GroupMessagingStreamTypes[K]): void => {
this.publish(`messagingStream:${groupId}`, type, typeof value === 'undefined' ? null : value); this.publish(`messagingStream:${groupId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishMessagingIndexStream = (userId: User['id'], type: string, value?: any): void => { public publishMessagingIndexStream = <K extends keyof MessagingIndexStreamTypes>(userId: User['id'], type: K, value?: MessagingIndexStreamTypes[K]): void => {
this.publish(`messagingIndexStream:${userId}`, type, typeof value === 'undefined' ? null : value); this.publish(`messagingIndexStream:${userId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishReversiStream = (userId: User['id'], type: string, value?: any): void => { public publishReversiStream = <K extends keyof ReversiStreamTypes>(userId: User['id'], type: K, value?: ReversiStreamTypes[K]): void => {
this.publish(`reversiStream:${userId}`, type, typeof value === 'undefined' ? null : value); this.publish(`reversiStream:${userId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishReversiGameStream = (gameId: ReversiGame['id'], type: string, value?: any): void => { public publishReversiGameStream = <K extends keyof ReversiGameStreamTypes>(gameId: ReversiGame['id'], type: K, value?: ReversiGameStreamTypes[K]): void => {
this.publish(`reversiGameStream:${gameId}`, type, typeof value === 'undefined' ? null : value); this.publish(`reversiGameStream:${gameId}`, type, typeof value === 'undefined' ? null : value);
} }
public publishNotesStream = (note: any): void => { public publishNotesStream = (note: PackedNote): void => {
this.publish('notesStream', null, note); this.publish('notesStream', null, note);
} }
public publishAdminStream = (userId: User['id'], type: string, value?: any): void => { public publishAdminStream = <K extends keyof AdminStreamTypes>(userId: User['id'], type: K, value?: AdminStreamTypes[K]): void => {
this.publish(`adminStream:${userId}`, type, typeof value === 'undefined' ? null : value); this.publish(`adminStream:${userId}`, type, typeof value === 'undefined' ? null : value);
} }
} }