From 6ae5f762503331c4e1f5ba5102144c53c5f25b6e Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 23 May 2021 13:34:36 +0900 Subject: [PATCH] resolve #17 --- README.md | 32 ++++++++++---------------------- src/streaming.ts | 9 +++++++++ test-d/streaming.ts | 2 +- test/streaming.ts | 14 ++++++++++---- 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index a0a97eb2f..113ae56fb 100644 --- a/README.md +++ b/README.md @@ -34,30 +34,27 @@ const meta = await cli.request('meta', { detail: true }); import * as Misskey from 'misskey-js'; const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' }); -const mainChannel = stream.useSharedConnection('main'); +const mainChannel = stream.useChannel('main'); mainChannel.on('notification', notification => { console.log('notification received', notification); }); ``` -### チャンネルへの接続(使いまわす場合) -使いまわし可能なチャンネル(=パラメータを持たないチャンネル)に接続するときは、`useSharedConnection`メソッドを使用します。 +### チャンネルへの接続 +チャンネルへの接続は`useChannel`メソッドを使用します。 +パラメータなし ``` ts const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' }); -const mainChannel = stream.useSharedConnection('main'); +const mainChannel = stream.useChannel('main'); ``` -このメソッドを用いてチャンネルに接続することで、(同じStreamインスタンスを共有している場合)プログラム上の複数個所から呼び出しても内部的にまとめられます。 - -### チャンネルへの接続(使いまわし不可の場合) -パラメータを持つチャンネルへの接続は`connectToChannel`メソッドを使用します。 - +パラメータあり ``` ts const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' }); -const messagingChannel = stream.connectToChannel('messaging', { +const messagingChannel = stream.useChannel('messaging', { otherparty: 'xxxxxxxxxx', }); ``` @@ -68,7 +65,7 @@ const messagingChannel = stream.connectToChannel('messaging', { ``` ts const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' }); -const mainChannel = stream.useSharedConnection('main'); +const mainChannel = stream.useChannel('main'); mainChannel.dispose(); ``` @@ -80,7 +77,7 @@ mainChannel.dispose(); import * as Misskey from 'misskey-js'; const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' }); -const mainChannel = stream.useSharedConnection('main'); +const mainChannel = stream.useChannel('main'); mainChannel.on('notification', notification => { console.log('notification received', notification); }); @@ -93,7 +90,7 @@ mainChannel.on('notification', notification => { import * as Misskey from 'misskey-js'; const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' }); -const messagingChannel = stream.connectToChannel('messaging', { +const messagingChannel = stream.useChannel('messaging', { otherparty: 'xxxxxxxxxx', }); @@ -102,15 +99,6 @@ messagingChannel.send('read', { }); ``` -### Reference - -#### `useSharedConnection(channel: string): SharedConnection` -使いまわし可能なチャンネル(=パラメータを持たないチャンネル)に接続します。 -このメソッドを用いて接続したチャンネル接続は内部的に使いまわされるため、プログラム上の複数の場所から呼び出してもコネクションを無駄に増やさずに済みます。 - -#### `connectToChannel(channel: string, params?: any): NonSharedConnection` -チャンネルに接続します。返り値はそのチャンネルへのコネクションインスタンスです。 - ---
diff --git a/src/streaming.ts b/src/streaming.ts index 44b6530d4..66a800d4c 100644 --- a/src/streaming.ts +++ b/src/streaming.ts @@ -114,6 +114,15 @@ export default class Stream extends EventEmitter { this.stream.addEventListener('message', this.onMessage); } + @autobind + public useChannel(channel: C, params?: any): Connection { + if (params) { + return this.connectToChannel(channel, params); + } else { + return this.useSharedConnection(channel); + } + } + @autobind public useSharedConnection(channel: C, name?: string): SharedConnection { let pool = this.sharedConnectionPools.find(p => p.channel === channel); diff --git a/test-d/streaming.ts b/test-d/streaming.ts index de67171ef..6b186bd45 100644 --- a/test-d/streaming.ts +++ b/test-d/streaming.ts @@ -4,7 +4,7 @@ import * as Misskey from '../src'; describe('Streaming', () => { test('emit type', async () => { const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' }); - const mainChannel = stream.useSharedConnection('main'); + const mainChannel = stream.useChannel('main'); mainChannel.on('notification', notification => { expectType(notification); }); diff --git a/test/streaming.ts b/test/streaming.ts index 02cad50c1..afd0852ff 100644 --- a/test/streaming.ts +++ b/test/streaming.ts @@ -2,11 +2,11 @@ import WS from 'jest-websocket-mock'; import Stream from '../src/streaming'; describe('Streaming', () => { - test('useSharedConnection', async () => { + test('useChannel', async () => { const server = new WS('wss://misskey.test/streaming'); const stream = new Stream('https://misskey.test', { token: 'TOKEN' }); const mainChannelReceived: any[] = []; - const main = stream.useSharedConnection('main'); + const main = stream.useChannel('main'); main.on('meUpdated', payload => { mainChannelReceived.push(payload); }); @@ -35,11 +35,15 @@ describe('Streaming', () => { server.close(); }); - test('SharedConnection#dispose', async () => { + test('useChannel with parameters', async () => { + // TODO + }); + + test('Connection#dispose', async () => { const server = new WS('wss://misskey.test/streaming'); const stream = new Stream('https://misskey.test', { token: 'TOKEN' }); const mainChannelReceived: any[] = []; - const main = stream.useSharedConnection('main'); + const main = stream.useChannel('main'); main.on('meUpdated', payload => { mainChannelReceived.push(payload); }); @@ -68,4 +72,6 @@ describe('Streaming', () => { }); // TODO: SharedConnection#dispose して一定時間経ったら disconnect メッセージがサーバーに送られてくるかのテスト + + // TODO: チャンネル接続が使いまわされるかのテスト });