Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dima Krasner
Sharkey
Commits
6ae5f762
Commit
6ae5f762
authored
3 years ago
by
syuilo
Browse files
Options
Downloads
Patches
Plain Diff
resolve #17
parent
ca655c06
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
README.md
+10
-22
10 additions, 22 deletions
README.md
src/streaming.ts
+9
-0
9 additions, 0 deletions
src/streaming.ts
test-d/streaming.ts
+1
-1
1 addition, 1 deletion
test-d/streaming.ts
test/streaming.ts
+10
-4
10 additions, 4 deletions
test/streaming.ts
with
30 additions
and
27 deletions
README.md
+
10
−
22
View file @
6ae5f762
...
...
@@ -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
.
use
S
ha
redConnection
(
'
main
'
);
const
mainChannel
=
stream
.
use
C
ha
nnel
(
'
main
'
);
mainChannel
.
on
(
'
notification
'
,
notification
=>
{
console
.
log
(
'
notification received
'
,
notification
);
});
```
### チャンネルへの接続
(使いまわす場合)
使いまわし可能なチャンネル(=パラメータを持たないチャンネル)に接続するときは、
`use
S
ha
redConnection
`
メソッドを使用します。
### チャンネルへの接続
チャンネルへの接続は
`use
C
ha
nnel
`
メソッドを使用します。
パラメータなし
```
ts
const
stream
=
new
Misskey
.
Stream
(
'
https://misskey.test
'
,
{
token
:
'
TOKEN
'
});
const
mainChannel
=
stream
.
use
S
ha
redConnection
(
'
main
'
);
const
mainChannel
=
stream
.
use
C
ha
nnel
(
'
main
'
);
```
このメソッドを用いてチャンネルに接続することで、(同じStreamインスタンスを共有している場合)プログラム上の複数個所から呼び出しても内部的にまとめられます。
### チャンネルへの接続(使いまわし不可の場合)
パラメータを持つチャンネルへの接続は
`connectToChannel`
メソッドを使用します。
パラメータあり
```
ts
const
stream
=
new
Misskey
.
Stream
(
'
https://misskey.test
'
,
{
token
:
'
TOKEN
'
});
const
messagingChannel
=
stream
.
connectTo
Channel
(
'
messaging
'
,
{
const
messagingChannel
=
stream
.
use
Channel
(
'
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
.
use
S
ha
redConnection
(
'
main
'
);
const
mainChannel
=
stream
.
use
C
ha
nnel
(
'
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
.
use
S
ha
redConnection
(
'
main
'
);
const
mainChannel
=
stream
.
use
C
ha
nnel
(
'
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
.
connectTo
Channel
(
'
messaging
'
,
{
const
messagingChannel
=
stream
.
use
Channel
(
'
messaging
'
,
{
otherparty
:
'
xxxxxxxxxx
'
,
});
...
...
@@ -102,15 +99,6 @@ messagingChannel.send('read', {
});
```
### Reference
#### `useSharedConnection(channel: string): SharedConnection`
使いまわし可能なチャンネル(=パラメータを持たないチャンネル)に接続します。
このメソッドを用いて接続したチャンネル接続は内部的に使いまわされるため、プログラム上の複数の場所から呼び出してもコネクションを無駄に増やさずに済みます。
#### `connectToChannel(channel: string, params?: any): NonSharedConnection`
チャンネルに接続します。返り値はそのチャンネルへのコネクションインスタンスです。
---
<div
align=
"center"
>
...
...
This diff is collapsed.
Click to expand it.
src/streaming.ts
+
9
−
0
View file @
6ae5f762
...
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
test-d/streaming.ts
+
1
−
1
View file @
6ae5f762
...
...
@@ -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
.
use
S
ha
redConnection
(
'
main
'
);
const
mainChannel
=
stream
.
use
C
ha
nnel
(
'
main
'
);
mainChannel
.
on
(
'
notification
'
,
notification
=>
{
expectType
<
Misskey
.
entities
.
Notification
>
(
notification
);
});
...
...
This diff is collapsed.
Click to expand it.
test/streaming.ts
+
10
−
4
View file @
6ae5f762
...
...
@@ -2,11 +2,11 @@ import WS from 'jest-websocket-mock';
import
Stream
from
'
../src/streaming
'
;
describe
(
'
Streaming
'
,
()
=>
{
test
(
'
use
S
ha
redConnection
'
,
async
()
=>
{
test
(
'
use
C
ha
nnel
'
,
async
()
=>
{
const
server
=
new
WS
(
'
wss://misskey.test/streaming
'
);
const
stream
=
new
Stream
(
'
https://misskey.test
'
,
{
token
:
'
TOKEN
'
});
const
mainChannelReceived
:
any
[]
=
[];
const
main
=
stream
.
use
S
ha
redConnection
(
'
main
'
);
const
main
=
stream
.
use
C
ha
nnel
(
'
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
.
use
S
ha
redConnection
(
'
main
'
);
const
main
=
stream
.
use
C
ha
nnel
(
'
main
'
);
main
.
on
(
'
meUpdated
'
,
payload
=>
{
mainChannelReceived
.
push
(
payload
);
});
...
...
@@ -68,4 +72,6 @@ describe('Streaming', () => {
});
// TODO: SharedConnection#dispose して一定時間経ったら disconnect メッセージがサーバーに送られてくるかのテスト
// TODO: チャンネル接続が使いまわされるかのテスト
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment