diff --git a/README.md b/README.md index a0a97eb2fbef24e4a670656df8701a09882f0a24..113ae56fbc82cd15b3f4dd4c1e59ba4a9c9cc8af 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` -ãƒãƒ£ãƒ³ãƒãƒ«ã«æŽ¥ç¶šã—ã¾ã™ã€‚返り値ã¯ãã®ãƒãƒ£ãƒ³ãƒãƒ«ã¸ã®ã‚³ãƒã‚¯ã‚·ãƒ§ãƒ³ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã§ã™ã€‚ - --- <div align="center"> diff --git a/src/streaming.ts b/src/streaming.ts index 44b6530d443bbbfa22d583493bae2b536d578e07..66a800d4c0a2f30fef42f958e4c20d695317b962 100644 --- a/src/streaming.ts +++ b/src/streaming.ts @@ -114,6 +114,15 @@ export default class Stream extends EventEmitter<StreamEvents> { this.stream.addEventListener('message', this.onMessage); } + @autobind + public useChannel<C extends keyof ChannelDef>(channel: C, params?: any): Connection<ChannelDef[C]['events']> { + if (params) { + return this.connectToChannel(channel, params); + } else { + return this.useSharedConnection(channel); + } + } + @autobind public useSharedConnection<C extends keyof ChannelDef>(channel: C, name?: string): SharedConnection<ChannelDef[C]['events']> { let pool = this.sharedConnectionPools.find(p => p.channel === channel); diff --git a/test-d/streaming.ts b/test-d/streaming.ts index de67171ef33ec6b73340ebaa13cb81464de85d1e..6b186bd45af9bcab432e407d4755ff1db499f050 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<Misskey.entities.Notification>(notification); }); diff --git a/test/streaming.ts b/test/streaming.ts index 02cad50c1806229aae61bc108ea91dbce3ba6e6b..afd0852ff8d00089960c7be9a7bb490de5d8c953 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: ãƒãƒ£ãƒ³ãƒãƒ«æŽ¥ç¶šãŒä½¿ã„ã¾ã‚ã•ã‚Œã‚‹ã‹ã®ãƒ†ã‚¹ãƒˆ });