This commit is contained in:
syuilo 2021-12-28 00:45:47 +09:00
parent 4ff49b0a3f
commit d2b634c349
5 changed files with 37 additions and 33 deletions

View File

@ -3,3 +3,5 @@ node_modules
/coverage /coverage
/.eslintrc.js /.eslintrc.js
/jest.config.ts /jest.config.ts
/test
/test-d

View File

@ -1,6 +1,10 @@
module.exports = { module.exports = {
root: true, root: true,
parser: '@typescript-eslint/parser', parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
plugins: [ plugins: [
'@typescript-eslint', '@typescript-eslint',
], ],

View File

@ -73,10 +73,10 @@ export class APIClient {
method: 'POST', method: 'POST',
body: JSON.stringify({ body: JSON.stringify({
...params, ...params,
i: credential !== undefined ? credential : this.credential i: credential !== undefined ? credential : this.credential,
}), }),
credentials: 'omit', credentials: 'omit',
cache: 'no-cache' cache: 'no-cache',
}).then(async (res) => { }).then(async (res) => {
const body = res.status === 204 ? null : await res.json(); const body = res.status === 204 ? null : await res.json();
@ -87,7 +87,7 @@ export class APIClient {
} else { } else {
reject({ reject({
[MK_API_ERROR]: true, [MK_API_ERROR]: true,
...body.error ...body.error,
}); });
} }
}).catch(reject); }).catch(reject);

View File

@ -2,7 +2,7 @@ import {
Ad, Announcement, Antenna, App, AuthSession, Blocking, Channel, Clip, DateString, DetailedInstanceMetadata, DriveFile, DriveFolder, Following, FollowingFolloweePopulated, FollowingFollowerPopulated, FollowRequest, GalleryPost, Instance, InstanceMetadata, Ad, Announcement, Antenna, App, AuthSession, Blocking, Channel, Clip, DateString, DetailedInstanceMetadata, DriveFile, DriveFolder, Following, FollowingFolloweePopulated, FollowingFollowerPopulated, FollowRequest, GalleryPost, Instance, InstanceMetadata,
LiteInstanceMetadata, LiteInstanceMetadata,
MeDetailed, MeDetailed,
Note, NoteFavorite, OriginType, Page, ServerInfo, Stats, User, UserDetailed, UserGroup, UserList, UserSorting, Notification, NoteReaction, Signin, MessagingMessage Note, NoteFavorite, OriginType, Page, ServerInfo, Stats, User, UserDetailed, UserGroup, UserList, UserSorting, Notification, NoteReaction, Signin, MessagingMessage,
} from './entities'; } from './entities';
type TODO = Record<string, any> | null; type TODO = Record<string, any> | null;

View File

@ -49,7 +49,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
this.stream = new ReconnectingWebsocket(`${wsOrigin}/streaming?${query}`, '', { this.stream = new ReconnectingWebsocket(`${wsOrigin}/streaming?${query}`, '', {
minReconnectionDelay: 1, // https://github.com/pladaria/reconnecting-websocket/issues/91 minReconnectionDelay: 1, // https://github.com/pladaria/reconnecting-websocket/issues/91
WebSocket: options.WebSocket WebSocket: options.WebSocket,
}); });
this.stream.addEventListener('open', this.onOpen); this.stream.addEventListener('open', this.onOpen);
this.stream.addEventListener('close', this.onClose); this.stream.addEventListener('close', this.onClose);
@ -85,12 +85,12 @@ export default class Stream extends EventEmitter<StreamEvents> {
} }
@autobind @autobind
public removeSharedConnection(connection: SharedConnection) { public removeSharedConnection(connection: SharedConnection): void {
this.sharedConnections = this.sharedConnections.filter(c => c !== connection); this.sharedConnections = this.sharedConnections.filter(c => c !== connection);
} }
@autobind @autobind
public removeSharedConnectionPool(pool: Pool) { public removeSharedConnectionPool(pool: Pool): void {
this.sharedConnectionPools = this.sharedConnectionPools.filter(p => p !== pool); this.sharedConnectionPools = this.sharedConnectionPools.filter(p => p !== pool);
} }
@ -102,7 +102,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
} }
@autobind @autobind
public disconnectToChannel(connection: NonSharedConnection) { public disconnectToChannel(connection: NonSharedConnection): void {
this.nonSharedConnections = this.nonSharedConnections.filter(c => c !== connection); this.nonSharedConnections = this.nonSharedConnections.filter(c => c !== connection);
} }
@ -110,7 +110,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
* Callback of when open connection * Callback of when open connection
*/ */
@autobind @autobind
private onOpen() { private onOpen(): void {
const isReconnect = this.state === 'reconnecting'; const isReconnect = this.state === 'reconnecting';
this.state = 'connected'; this.state = 'connected';
@ -118,10 +118,8 @@ export default class Stream extends EventEmitter<StreamEvents> {
// チャンネル再接続 // チャンネル再接続
if (isReconnect) { if (isReconnect) {
for (const p of this.sharedConnectionPools) for (const p of this.sharedConnectionPools) p.connect();
p.connect(); for (const c of this.nonSharedConnections) c.connect();
for (const c of this.nonSharedConnections)
c.connect();
} }
} }
@ -129,7 +127,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
* Callback of when close connection * Callback of when close connection
*/ */
@autobind @autobind
private onClose() { private onClose(): void {
if (this.state === 'connected') { if (this.state === 'connected') {
this.state = 'reconnecting'; this.state = 'reconnecting';
this.emit('_disconnected_'); this.emit('_disconnected_');
@ -140,7 +138,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
* Callback of when received a message from connection * Callback of when received a message from connection
*/ */
@autobind @autobind
private onMessage(message: { data: string; }) { private onMessage(message: { data: string; }): void {
const { type, body } = JSON.parse(message.data); const { type, body } = JSON.parse(message.data);
if (type === 'channel') { if (type === 'channel') {
@ -157,7 +155,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
} }
} }
for (const c of connections.filter(c => c != null)) { for (const c of connections) {
c.emit(body.type, Object.freeze(body.body)); c.emit(body.type, Object.freeze(body.body));
c.inCount++; c.inCount++;
} }
@ -170,10 +168,10 @@ export default class Stream extends EventEmitter<StreamEvents> {
* Send a message to connection * Send a message to connection
*/ */
@autobind @autobind
public send(typeOrPayload: any, payload?: any) { public send(typeOrPayload: any, payload?: any): void {
const data = payload === undefined ? typeOrPayload : { const data = payload === undefined ? typeOrPayload : {
type: typeOrPayload, type: typeOrPayload,
body: payload body: payload,
}; };
this.stream.send(JSON.stringify(data)); this.stream.send(JSON.stringify(data));
@ -183,7 +181,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
* Close this connection * Close this connection
*/ */
@autobind @autobind
public close() { public close(): void {
this.stream.close(); this.stream.close();
} }
} }
@ -207,12 +205,12 @@ class Pool {
} }
@autobind @autobind
private onStreamDisconnected() { private onStreamDisconnected(): void {
this.isConnected = false; this.isConnected = false;
} }
@autobind @autobind
public inc() { public inc(): void {
if (this.users === 0 && !this.isConnected) { if (this.users === 0 && !this.isConnected) {
this.connect(); this.connect();
} }
@ -227,7 +225,7 @@ class Pool {
} }
@autobind @autobind
public dec() { public dec(): void {
this.users--; this.users--;
// そのコネクションの利用者が誰もいなくなったら // そのコネクションの利用者が誰もいなくなったら
@ -241,17 +239,17 @@ class Pool {
} }
@autobind @autobind
public connect() { public connect(): void {
if (this.isConnected) return; if (this.isConnected) return;
this.isConnected = true; this.isConnected = true;
this.stream.send('connect', { this.stream.send('connect', {
channel: this.channel, channel: this.channel,
id: this.id id: this.id,
}); });
} }
@autobind @autobind
private disconnect() { private disconnect(): void {
this.stream.off('_disconnected_', this.onStreamDisconnected); this.stream.off('_disconnected_', this.onStreamDisconnected);
this.stream.send('disconnect', { id: this.id }); this.stream.send('disconnect', { id: this.id });
this.stream.removeSharedConnectionPool(this); this.stream.removeSharedConnectionPool(this);
@ -264,8 +262,8 @@ abstract class Connection<Channel extends AnyOf<Channels> = any> extends EventEm
public abstract id: string; public abstract id: string;
public name?: string; // for debug public name?: string; // for debug
public inCount: number = 0; // for debug public inCount = 0; // for debug
public outCount: number = 0; // for debug public outCount = 0; // for debug
constructor(stream: Stream, channel: string, name?: string) { constructor(stream: Stream, channel: string, name?: string) {
super(); super();
@ -276,11 +274,11 @@ abstract class Connection<Channel extends AnyOf<Channels> = any> extends EventEm
} }
@autobind @autobind
public send<T extends keyof Channel['receives']>(type: T, body: Channel['receives'][T]) { public send<T extends keyof Channel['receives']>(type: T, body: Channel['receives'][T]): void {
this.stream.send('ch', { this.stream.send('ch', {
id: this.id, id: this.id,
type: type, type: type,
body: body body: body,
}); });
this.outCount++; this.outCount++;
@ -304,7 +302,7 @@ class SharedConnection<Channel extends AnyOf<Channels> = any> extends Connection
} }
@autobind @autobind
public dispose() { public dispose(): void {
this.pool.dec(); this.pool.dec();
this.removeAllListeners(); this.removeAllListeners();
this.stream.removeSharedConnection(this); this.stream.removeSharedConnection(this);
@ -325,16 +323,16 @@ class NonSharedConnection<Channel extends AnyOf<Channels> = any> extends Connect
} }
@autobind @autobind
public connect() { public connect(): void {
this.stream.send('connect', { this.stream.send('connect', {
channel: this.channel, channel: this.channel,
id: this.id, id: this.id,
params: this.params params: this.params,
}); });
} }
@autobind @autobind
public dispose() { public dispose(): void {
this.removeAllListeners(); this.removeAllListeners();
this.stream.send('disconnect', { id: this.id }); this.stream.send('disconnect', { id: this.id });
this.stream.disconnectToChannel(this); this.stream.disconnectToChannel(this);