This commit is contained in:
tamaina 2021-09-05 23:47:41 +09:00
parent 65d9c304df
commit 987c98faec
4 changed files with 61 additions and 4 deletions

View File

@ -209,6 +209,7 @@
"seedrandom": "3.0.5",
"sharp": "0.29.0",
"speakeasy": "2.0.0",
"strict-event-emitter-types": "2.0.0",
"stringz": "2.1.0",
"style-loader": "3.2.1",
"summaly": "2.4.1",

View File

@ -15,6 +15,7 @@ import { UserProfile } from '@/models/entities/user-profile';
import { publishChannelStream, publishGroupMessagingStream, publishMessagingStream } from '@/services/stream';
import { UserGroup } from '@/models/entities/user-group';
import { PackedNote } from '@/models/repositories/note';
import { StreamEventEmitter, UserEvent } from './types';
/**
* Main stream connection
@ -28,7 +29,7 @@ export default class Connection {
public followingChannels: Set<ChannelModel['id']> = new Set();
public token?: AccessToken;
private wsConnection: websocket.connection;
public subscriber: EventEmitter;
public subscriber: StreamEventEmitter;
private channels: Channel[] = [];
private subscribingNotes: any = {};
private cachedNotes: PackedNote[] = [];
@ -57,14 +58,14 @@ export default class Connection {
this.updateFollowingChannels();
this.updateUserProfile();
this.subscriber.on(`user:${this.user.id}`, ({ type, body }) => {
this.onUserEvent(type, body);
this.subscriber.on(`user:${this.user.id}`, (ev) => {
this.onUserEvent(ev);
});
}
}
@autobind
private onUserEvent(type: string, body: any) {
private onUserEvent({ type, body }: UserEvent) {
switch (type) {
case 'follow':
this.following.add(body.id);

View File

@ -0,0 +1,50 @@
import { User } from '@/models/entities/user';
import { EventEmitter } from 'events';
import Emitter from 'strict-event-emitter-types';
import StreamTypes from 'misskey-js/built/streaming.types';
import { Channel } from '@/models/entities/channel';
import { UserProfile } from '@/models/entities/user-profile';
import { PackedUser } from '@/models/repositories/user';
type Payload<T extends (payload: any) => void> = T extends (payload: infer P) => void ? P : never;
// https://stackoverflow.com/questions/49311989/can-i-infer-the-type-of-a-value-using-extends-keyof-type
type EventUnionFromDictionary<
T extends object,
U = { [K in keyof T]: { type: K; body: T[K]} }
> = U[keyof U];
export type BroadcastStream<T extends keyof StreamTypes.BroadcasrEvents> = {
name: 'broadcast';
type: T;
body: Payload<StreamTypes.BroadcasrEvents[T]>;
};
export interface UserEventTypes {
terminate: {};
followChannel: Channel;
unfollowChannel: Channel;
updateUserProfile: UserProfile;
mute: User;
unmute: User;
follow: PackedUser;
unfollow: PackedUser;
userAdded: PackedUser;
};
// UserList userRemoved: PackedUser;
export type UserEventName = `user:${User['id']}`;
export type UserEvent = EventUnionFromDictionary<UserEventTypes>;
interface StreamEvents {
'broadcast': <T extends keyof StreamTypes.BroadcasrEvents>(e: BroadcastStream<T>) => void;
}
interface AuthenticatedStreamEvents {
[key: UserEventName]: <T extends keyof UserEventTypes>(e: UserEvent<T>) => void;
[key: `mainStream:${User['id']}`]: (e: { type: string; body: any }) => void;
[key: `driveStream:${User['id']}`]: (e: { type: string; body: any }) => void;
}
export type StreamEventEmitter = Emitter<EventEmitter, AuthenticatedStreamEvents & StreamEvents>;

View File

@ -10249,6 +10249,11 @@ streamsearch@0.1.2:
resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-0.1.2.tgz#808b9d0e56fc273d809ba57338e929919a1a9f1a"
integrity sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=
strict-event-emitter-types@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strict-event-emitter-types/-/strict-event-emitter-types-2.0.0.tgz#05e15549cb4da1694478a53543e4e2f4abcf277f"
integrity sha512-Nk/brWYpD85WlOgzw5h173aci0Teyv8YdIAEtV+N88nDB0dLlazZyJMIsN6eo1/AR61l+p6CJTG1JIyFaoNEEA==
strict-uri-encode@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"