mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-10 16:13:07 +02:00
resolve #17
This commit is contained in:
parent
ca655c0628
commit
6ae5f76250
4 changed files with 30 additions and 27 deletions
32
README.md
32
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">
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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: チャンネル接続が使いまわされるかのテスト
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue