mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-24 22:13:10 +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';
|
import * as Misskey from 'misskey-js';
|
||||||
|
|
||||||
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
const mainChannel = stream.useSharedConnection('main');
|
const mainChannel = stream.useChannel('main');
|
||||||
mainChannel.on('notification', notification => {
|
mainChannel.on('notification', notification => {
|
||||||
console.log('notification received', notification);
|
console.log('notification received', notification);
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### チャンネルへの接続(使いまわす場合)
|
### チャンネルへの接続
|
||||||
使いまわし可能なチャンネル(=パラメータを持たないチャンネル)に接続するときは、`useSharedConnection`メソッドを使用します。
|
チャンネルへの接続は`useChannel`メソッドを使用します。
|
||||||
|
|
||||||
|
パラメータなし
|
||||||
``` ts
|
``` ts
|
||||||
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
|
|
||||||
const mainChannel = stream.useSharedConnection('main');
|
const mainChannel = stream.useChannel('main');
|
||||||
```
|
```
|
||||||
|
|
||||||
このメソッドを用いてチャンネルに接続することで、(同じStreamインスタンスを共有している場合)プログラム上の複数個所から呼び出しても内部的にまとめられます。
|
パラメータあり
|
||||||
|
|
||||||
### チャンネルへの接続(使いまわし不可の場合)
|
|
||||||
パラメータを持つチャンネルへの接続は`connectToChannel`メソッドを使用します。
|
|
||||||
|
|
||||||
``` ts
|
``` ts
|
||||||
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
|
|
||||||
const messagingChannel = stream.connectToChannel('messaging', {
|
const messagingChannel = stream.useChannel('messaging', {
|
||||||
otherparty: 'xxxxxxxxxx',
|
otherparty: 'xxxxxxxxxx',
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -68,7 +65,7 @@ const messagingChannel = stream.connectToChannel('messaging', {
|
||||||
``` ts
|
``` ts
|
||||||
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
|
|
||||||
const mainChannel = stream.useSharedConnection('main');
|
const mainChannel = stream.useChannel('main');
|
||||||
|
|
||||||
mainChannel.dispose();
|
mainChannel.dispose();
|
||||||
```
|
```
|
||||||
|
@ -80,7 +77,7 @@ mainChannel.dispose();
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
|
|
||||||
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
const mainChannel = stream.useSharedConnection('main');
|
const mainChannel = stream.useChannel('main');
|
||||||
mainChannel.on('notification', notification => {
|
mainChannel.on('notification', notification => {
|
||||||
console.log('notification received', notification);
|
console.log('notification received', notification);
|
||||||
});
|
});
|
||||||
|
@ -93,7 +90,7 @@ mainChannel.on('notification', notification => {
|
||||||
import * as Misskey from 'misskey-js';
|
import * as Misskey from 'misskey-js';
|
||||||
|
|
||||||
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
const messagingChannel = stream.connectToChannel('messaging', {
|
const messagingChannel = stream.useChannel('messaging', {
|
||||||
otherparty: 'xxxxxxxxxx',
|
otherparty: 'xxxxxxxxxx',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -102,15 +99,6 @@ messagingChannel.send('read', {
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Reference
|
|
||||||
|
|
||||||
#### `useSharedConnection(channel: string): SharedConnection`
|
|
||||||
使いまわし可能なチャンネル(=パラメータを持たないチャンネル)に接続します。
|
|
||||||
このメソッドを用いて接続したチャンネル接続は内部的に使いまわされるため、プログラム上の複数の場所から呼び出してもコネクションを無駄に増やさずに済みます。
|
|
||||||
|
|
||||||
#### `connectToChannel(channel: string, params?: any): NonSharedConnection`
|
|
||||||
チャンネルに接続します。返り値はそのチャンネルへのコネクションインスタンスです。
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
|
|
|
@ -114,6 +114,15 @@ export default class Stream extends EventEmitter<StreamEvents> {
|
||||||
this.stream.addEventListener('message', this.onMessage);
|
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
|
@autobind
|
||||||
public useSharedConnection<C extends keyof ChannelDef>(channel: C, name?: string): SharedConnection<ChannelDef[C]['events']> {
|
public useSharedConnection<C extends keyof ChannelDef>(channel: C, name?: string): SharedConnection<ChannelDef[C]['events']> {
|
||||||
let pool = this.sharedConnectionPools.find(p => p.channel === channel);
|
let pool = this.sharedConnectionPools.find(p => p.channel === channel);
|
||||||
|
|
|
@ -4,7 +4,7 @@ import * as Misskey from '../src';
|
||||||
describe('Streaming', () => {
|
describe('Streaming', () => {
|
||||||
test('emit type', async () => {
|
test('emit type', async () => {
|
||||||
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
const mainChannel = stream.useSharedConnection('main');
|
const mainChannel = stream.useChannel('main');
|
||||||
mainChannel.on('notification', notification => {
|
mainChannel.on('notification', notification => {
|
||||||
expectType<Misskey.entities.Notification>(notification);
|
expectType<Misskey.entities.Notification>(notification);
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,11 +2,11 @@ import WS from 'jest-websocket-mock';
|
||||||
import Stream from '../src/streaming';
|
import Stream from '../src/streaming';
|
||||||
|
|
||||||
describe('Streaming', () => {
|
describe('Streaming', () => {
|
||||||
test('useSharedConnection', async () => {
|
test('useChannel', async () => {
|
||||||
const server = new WS('wss://misskey.test/streaming');
|
const server = new WS('wss://misskey.test/streaming');
|
||||||
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
const mainChannelReceived: any[] = [];
|
const mainChannelReceived: any[] = [];
|
||||||
const main = stream.useSharedConnection('main');
|
const main = stream.useChannel('main');
|
||||||
main.on('meUpdated', payload => {
|
main.on('meUpdated', payload => {
|
||||||
mainChannelReceived.push(payload);
|
mainChannelReceived.push(payload);
|
||||||
});
|
});
|
||||||
|
@ -35,11 +35,15 @@ describe('Streaming', () => {
|
||||||
server.close();
|
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 server = new WS('wss://misskey.test/streaming');
|
||||||
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
|
const stream = new Stream('https://misskey.test', { token: 'TOKEN' });
|
||||||
const mainChannelReceived: any[] = [];
|
const mainChannelReceived: any[] = [];
|
||||||
const main = stream.useSharedConnection('main');
|
const main = stream.useChannel('main');
|
||||||
main.on('meUpdated', payload => {
|
main.on('meUpdated', payload => {
|
||||||
mainChannelReceived.push(payload);
|
mainChannelReceived.push(payload);
|
||||||
});
|
});
|
||||||
|
@ -68,4 +72,6 @@ describe('Streaming', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: SharedConnection#dispose して一定時間経ったら disconnect メッセージがサーバーに送られてくるかのテスト
|
// TODO: SharedConnection#dispose して一定時間経ったら disconnect メッセージがサーバーに送られてくるかのテスト
|
||||||
|
|
||||||
|
// TODO: チャンネル接続が使いまわされるかのテスト
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue