Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
336
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
23
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
TransFem.org
Sharkey
Commits
99276028
Unverified
Commit
99276028
authored
3 years ago
by
syuilo
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Typed stream (
#13
)
* Update streaming.ts * Update streaming.ts * wip
parent
d7d02cd2
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/entities.ts
+89
-0
89 additions, 0 deletions
src/entities.ts
src/streaming.ts
+81
-6
81 additions, 6 deletions
src/streaming.ts
test/streaming.ts
+4
-4
4 additions, 4 deletions
test/streaming.ts
with
174 additions
and
10 deletions
src/entities.ts
+
89
−
0
View file @
99276028
export
type
ID
=
string
;
type
TODO
=
Record
<
string
,
any
>
;
export
type
User
=
{
id
:
ID
;
username
:
string
;
...
...
@@ -14,6 +16,17 @@ export type User = {
}[];
};
export
type
MeDetailed
=
User
&
{
avatarId
:
DriveFile
[
'
id
'
];
bannerId
:
DriveFile
[
'
id
'
];
autoAcceptFollowed
:
boolean
;
noCrawle
:
boolean
;
isExplorable
:
boolean
;
hideOnlineStatus
:
boolean
;
mutedWords
:
string
[][];
[
other
:
string
]:
any
;
};
export
type
DriveFile
=
{
id
:
ID
;
createdAt
:
string
;
...
...
@@ -59,6 +72,74 @@ export type Note = {
}[];
};
export
type
Notification
=
{
id
:
ID
;
createdAt
:
string
;
isRead
:
boolean
;
}
&
({
type
:
'
reaction
'
;
reaction
:
string
;
user
:
User
;
userId
:
User
[
'
id
'
];
note
:
Note
;
}
|
{
type
:
'
reply
'
;
user
:
User
;
userId
:
User
[
'
id
'
];
note
:
Note
;
}
|
{
type
:
'
renote
'
;
user
:
User
;
userId
:
User
[
'
id
'
];
note
:
Note
;
}
|
{
type
:
'
quote
'
;
user
:
User
;
userId
:
User
[
'
id
'
];
note
:
Note
;
}
|
{
type
:
'
mention
'
;
user
:
User
;
userId
:
User
[
'
id
'
];
note
:
Note
;
}
|
{
type
:
'
pollVote
'
;
user
:
User
;
userId
:
User
[
'
id
'
];
note
:
Note
;
}
|
{
type
:
'
follow
'
;
user
:
User
;
userId
:
User
[
'
id
'
];
}
|
{
type
:
'
followRequestAccepted
'
;
user
:
User
;
userId
:
User
[
'
id
'
];
}
|
{
type
:
'
receiveFollowRequest
'
;
user
:
User
;
userId
:
User
[
'
id
'
];
}
|
{
type
:
'
groupInvited
'
;
// TODO
}
|
{
type
:
'
app
'
;
body
:
string
;
icon
:
string
;
});
export
type
MessagingMessage
=
{
id
:
ID
;
createdAt
:
string
;
file
:
DriveFile
|
null
;
fileId
:
DriveFile
[
'
id
'
]
|
null
;
isRead
:
boolean
;
reads
:
User
[
'
id
'
][];
text
:
string
|
null
;
user
:
User
;
userId
:
User
[
'
id
'
];
groupId
:
string
;
// TODO
};
export
type
InstanceMetadata
=
{
emojis
:
{
category
:
string
;
...
...
@@ -119,5 +200,13 @@ export type Page = {
isLiked
?:
boolean
;
};
export
type
PageEvent
=
{
pageId
:
Page
[
'
id
'
];
event
:
string
;
var
:
any
;
userId
:
User
[
'
id
'
];
user
:
User
;
};
export
type
UserSorting
=
'
+follower
'
|
'
-follower
'
|
'
+createdAt
'
|
'
-createdAt
'
|
'
+updatedAt
'
|
'
-updatedAt
'
;
export
type
OriginType
=
'
combined
'
|
'
local
'
|
'
remote
'
;
This diff is collapsed.
Click to expand it.
src/streaming.ts
+
81
−
6
View file @
99276028
...
...
@@ -3,6 +3,7 @@ import { EventEmitter } from 'eventemitter3';
import
ReconnectingWebsocket
from
'
reconnecting-websocket
'
;
import
{
stringify
}
from
'
querystring
'
;
import
{
markRaw
}
from
'
@vue/reactivity
'
;
import
{
MeDetailed
,
MessagingMessage
,
Note
,
Notification
,
PageEvent
,
User
}
from
'
./entities
'
;
function
urlQuery
(
obj
:
{}):
string
{
return
stringify
(
Object
.
entries
(
obj
)
...
...
@@ -10,10 +11,84 @@ function urlQuery(obj: {}): string {
.
reduce
((
a
,
[
k
,
v
])
=>
(
a
[
k
]
=
v
,
a
),
{}
as
Record
<
string
,
any
>
));
}
type
FIXME
=
any
;
type
ChannelDef
=
{
main
:
{
events
:
{
notification
:
(
payload
:
Notification
)
=>
void
;
mention
:
(
payload
:
Note
)
=>
void
;
reply
:
(
payload
:
Note
)
=>
void
;
renote
:
(
payload
:
Note
)
=>
void
;
follow
:
(
payload
:
User
)
=>
void
;
// 自分が他人をフォローしたとき
followed
:
(
payload
:
User
)
=>
void
;
// 他人が自分をフォローしたとき
unfollow
:
(
payload
:
User
)
=>
void
;
// 自分が他人をフォロー解除したとき
meUpdated
:
(
payload
:
MeDetailed
)
=>
void
;
pageEvent
:
(
payload
:
PageEvent
)
=>
void
;
};
};
homeTimeline
:
{
events
:
{
note
:
(
payload
:
Note
)
=>
void
;
};
};
localTimeline
:
{
events
:
{
note
:
(
payload
:
Note
)
=>
void
;
};
};
hybridTimeline
:
{
events
:
{
note
:
(
payload
:
Note
)
=>
void
;
};
};
globalTimeline
:
{
events
:
{
note
:
(
payload
:
Note
)
=>
void
;
};
};
messaging
:
{
events
:
{
message
:
(
payload
:
MessagingMessage
)
=>
void
;
deleted
:
(
payload
:
MessagingMessage
[
'
id
'
])
=>
void
;
read
:
(
payload
:
MessagingMessage
[
'
id
'
][])
=>
void
;
typing
:
(
payload
:
User
[
'
id
'
])
=>
void
;
};
};
};
type
NoteUpdatedEvent
=
{
id
:
Note
[
'
id
'
];
type
:
'
reacted
'
;
body
:
{
reaction
:
string
;
userId
:
User
[
'
id
'
];
};
}
|
{
id
:
Note
[
'
id
'
];
type
:
'
deleted
'
;
body
:
{
deletedAt
:
string
;
};
}
|
{
id
:
Note
[
'
id
'
];
type
:
'
pollVoted
'
;
body
:
{
choice
:
number
;
userId
:
User
[
'
id
'
];
};
};
type
StreamEvents
=
{
_connected_
:
void
;
_disconnected_
:
void
;
noteUpdated
:
(
payload
:
NoteUpdatedEvent
)
=>
void
;
};
/**
* Misskey stream connection
*/
export
default
class
Stream
extends
EventEmitter
{
export
default
class
Stream
extends
EventEmitter
<
StreamEvents
>
{
private
stream
:
ReconnectingWebsocket
;
public
state
:
'
initializing
'
|
'
reconnecting
'
|
'
connected
'
=
'
initializing
'
;
private
sharedConnectionPools
:
Pool
[]
=
[];
...
...
@@ -38,7 +113,7 @@ export default class Stream extends EventEmitter {
}
@
autobind
public
useSharedConnection
(
channel
:
string
,
name
?:
string
):
SharedConnection
{
public
useSharedConnection
<
C
extends
keyof
ChannelDef
>
(
channel
:
C
,
name
?:
string
):
SharedConnection
<
ChannelDef
[
C
][
'
events
'
]
>
{
let
pool
=
this
.
sharedConnectionPools
.
find
(
p
=>
p
.
channel
===
channel
);
if
(
pool
==
null
)
{
...
...
@@ -62,7 +137,7 @@ export default class Stream extends EventEmitter {
}
@
autobind
public
connectToChannel
(
channel
:
string
,
params
?:
any
):
NonSharedConnection
{
public
connectToChannel
<
C
extends
keyof
ChannelDef
>
(
channel
:
C
,
params
?:
any
):
NonSharedConnection
<
ChannelDef
[
C
][
'
events
'
]
>
{
const
connection
=
markRaw
(
new
NonSharedConnection
(
this
,
channel
,
params
));
this
.
nonSharedConnections
.
push
(
connection
);
return
connection
;
...
...
@@ -227,7 +302,7 @@ class Pool {
}
}
abstract
class
Connection
extends
EventEmitter
{
abstract
class
Connection
<
Events
extends
Record
<
string
,
any
>
=
any
>
extends
EventEmitter
<
Events
>
{
public
channel
:
string
;
protected
stream
:
Stream
;
public
abstract
id
:
string
;
...
...
@@ -261,7 +336,7 @@ abstract class Connection extends EventEmitter {
public
abstract
dispose
():
void
;
}
class
SharedConnection
extends
Connection
{
class
SharedConnection
<
Events
=
any
>
extends
Connection
<
Events
>
{
private
pool
:
Pool
;
public
get
id
():
string
{
...
...
@@ -288,7 +363,7 @@ class SharedConnection extends Connection {
}
}
class
NonSharedConnection
extends
Connection
{
class
NonSharedConnection
<
Events
=
any
>
extends
Connection
<
Events
>
{
public
id
:
string
;
protected
params
:
any
;
...
...
This diff is collapsed.
Click to expand it.
test/streaming.ts
+
4
−
4
View file @
99276028
...
...
@@ -7,7 +7,7 @@ describe('Streaming', () => {
const
stream
=
new
Stream
(
'
https://misskey.test
'
,
{
token
:
'
TOKEN
'
});
const
mainChannelReceived
:
any
[]
=
[];
const
main
=
stream
.
useSharedConnection
(
'
main
'
);
main
.
on
(
'
foo
'
,
payload
=>
{
main
.
on
(
'
meUpdated
'
,
payload
=>
{
mainChannelReceived
.
push
(
payload
);
});
await
server
.
connected
;
...
...
@@ -21,15 +21,15 @@ describe('Streaming', () => {
type
:
'
channel
'
,
body
:
{
id
:
mainChannelId
,
type
:
'
foo
'
,
type
:
'
meUpdated
'
,
body
:
{
bar
:
'
buzz
'
id
:
'
foo
'
}
}
}));
expect
(
mainChannelReceived
[
0
]).
toEqual
({
bar
:
'
buzz
'
id
:
'
foo
'
});
});
});
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