diff --git a/.eslintignore b/.eslintignore index e308e03..f22128f 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,3 +3,5 @@ node_modules /coverage /.eslintrc.js /jest.config.ts +/test +/test-d diff --git a/.eslintrc.js b/.eslintrc.js index 2ff94f1..81dfdaf 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,6 +1,10 @@ module.exports = { root: true, parser: '@typescript-eslint/parser', + parserOptions: { + tsconfigRootDir: __dirname, + project: ['./tsconfig.json'], + }, plugins: [ '@typescript-eslint', ], diff --git a/src/api.ts b/src/api.ts index 33f6e7b..2cf8488 100644 --- a/src/api.ts +++ b/src/api.ts @@ -73,10 +73,10 @@ export class APIClient { method: 'POST', body: JSON.stringify({ ...params, - i: credential !== undefined ? credential : this.credential + i: credential !== undefined ? credential : this.credential, }), credentials: 'omit', - cache: 'no-cache' + cache: 'no-cache', }).then(async (res) => { const body = res.status === 204 ? null : await res.json(); @@ -87,7 +87,7 @@ export class APIClient { } else { reject({ [MK_API_ERROR]: true, - ...body.error + ...body.error, }); } }).catch(reject); diff --git a/src/api.types.ts b/src/api.types.ts index fd09f4a..2a08af7 100644 --- a/src/api.types.ts +++ b/src/api.types.ts @@ -2,7 +2,7 @@ import { Ad, Announcement, Antenna, App, AuthSession, Blocking, Channel, Clip, DateString, DetailedInstanceMetadata, DriveFile, DriveFolder, Following, FollowingFolloweePopulated, FollowingFollowerPopulated, FollowRequest, GalleryPost, Instance, InstanceMetadata, LiteInstanceMetadata, 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'; type TODO = Record | null; diff --git a/src/streaming.ts b/src/streaming.ts index 3807a72..341bfe4 100644 --- a/src/streaming.ts +++ b/src/streaming.ts @@ -49,7 +49,7 @@ export default class Stream extends EventEmitter { this.stream = new ReconnectingWebsocket(`${wsOrigin}/streaming?${query}`, '', { 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('close', this.onClose); @@ -85,12 +85,12 @@ export default class Stream extends EventEmitter { } @autobind - public removeSharedConnection(connection: SharedConnection) { + public removeSharedConnection(connection: SharedConnection): void { this.sharedConnections = this.sharedConnections.filter(c => c !== connection); } @autobind - public removeSharedConnectionPool(pool: Pool) { + public removeSharedConnectionPool(pool: Pool): void { this.sharedConnectionPools = this.sharedConnectionPools.filter(p => p !== pool); } @@ -102,7 +102,7 @@ export default class Stream extends EventEmitter { } @autobind - public disconnectToChannel(connection: NonSharedConnection) { + public disconnectToChannel(connection: NonSharedConnection): void { this.nonSharedConnections = this.nonSharedConnections.filter(c => c !== connection); } @@ -110,7 +110,7 @@ export default class Stream extends EventEmitter { * Callback of when open connection */ @autobind - private onOpen() { + private onOpen(): void { const isReconnect = this.state === 'reconnecting'; this.state = 'connected'; @@ -118,10 +118,8 @@ export default class Stream extends EventEmitter { // チャンネル再接続 if (isReconnect) { - for (const p of this.sharedConnectionPools) - p.connect(); - for (const c of this.nonSharedConnections) - c.connect(); + for (const p of this.sharedConnectionPools) p.connect(); + for (const c of this.nonSharedConnections) c.connect(); } } @@ -129,7 +127,7 @@ export default class Stream extends EventEmitter { * Callback of when close connection */ @autobind - private onClose() { + private onClose(): void { if (this.state === 'connected') { this.state = 'reconnecting'; this.emit('_disconnected_'); @@ -140,7 +138,7 @@ export default class Stream extends EventEmitter { * Callback of when received a message from connection */ @autobind - private onMessage(message: { data: string; }) { + private onMessage(message: { data: string; }): void { const { type, body } = JSON.parse(message.data); if (type === 'channel') { @@ -157,7 +155,7 @@ export default class Stream extends EventEmitter { } } - for (const c of connections.filter(c => c != null)) { + for (const c of connections) { c.emit(body.type, Object.freeze(body.body)); c.inCount++; } @@ -170,10 +168,10 @@ export default class Stream extends EventEmitter { * Send a message to connection */ @autobind - public send(typeOrPayload: any, payload?: any) { + public send(typeOrPayload: any, payload?: any): void { const data = payload === undefined ? typeOrPayload : { type: typeOrPayload, - body: payload + body: payload, }; this.stream.send(JSON.stringify(data)); @@ -183,7 +181,7 @@ export default class Stream extends EventEmitter { * Close this connection */ @autobind - public close() { + public close(): void { this.stream.close(); } } @@ -207,12 +205,12 @@ class Pool { } @autobind - private onStreamDisconnected() { + private onStreamDisconnected(): void { this.isConnected = false; } @autobind - public inc() { + public inc(): void { if (this.users === 0 && !this.isConnected) { this.connect(); } @@ -227,7 +225,7 @@ class Pool { } @autobind - public dec() { + public dec(): void { this.users--; // そのコネクションの利用者が誰もいなくなったら @@ -241,17 +239,17 @@ class Pool { } @autobind - public connect() { + public connect(): void { if (this.isConnected) return; this.isConnected = true; this.stream.send('connect', { channel: this.channel, - id: this.id + id: this.id, }); } @autobind - private disconnect() { + private disconnect(): void { this.stream.off('_disconnected_', this.onStreamDisconnected); this.stream.send('disconnect', { id: this.id }); this.stream.removeSharedConnectionPool(this); @@ -264,8 +262,8 @@ abstract class Connection = any> extends EventEm public abstract id: string; public name?: string; // for debug - public inCount: number = 0; // for debug - public outCount: number = 0; // for debug + public inCount = 0; // for debug + public outCount = 0; // for debug constructor(stream: Stream, channel: string, name?: string) { super(); @@ -276,11 +274,11 @@ abstract class Connection = any> extends EventEm } @autobind - public send(type: T, body: Channel['receives'][T]) { + public send(type: T, body: Channel['receives'][T]): void { this.stream.send('ch', { id: this.id, type: type, - body: body + body: body, }); this.outCount++; @@ -304,7 +302,7 @@ class SharedConnection = any> extends Connection } @autobind - public dispose() { + public dispose(): void { this.pool.dec(); this.removeAllListeners(); this.stream.removeSharedConnection(this); @@ -325,16 +323,16 @@ class NonSharedConnection = any> extends Connect } @autobind - public connect() { + public connect(): void { this.stream.send('connect', { channel: this.channel, id: this.id, - params: this.params + params: this.params, }); } @autobind - public dispose() { + public dispose(): void { this.removeAllListeners(); this.stream.send('disconnect', { id: this.id }); this.stream.disconnectToChannel(this);