Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
25d2b6a0
Commit
25d2b6a0
authored
Jun 05, 2023
by
Robert Knight
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate port-util.js to TypeScript
parent
c0ec257e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
0 deletions
+87
-0
port-util.ts
src/shared/messaging/port-util.ts
+87
-0
No files found.
src/shared/messaging/port-util.
j
s
→
src/shared/messaging/port-util.
t
s
View file @
25d2b6a0
export
type
Frame
=
'guest'
|
'host'
|
'notebook'
|
'profile'
|
'sidebar'
;
/**
* Message sent by `PortProvider` and `PortFinder` to establish a
* MessageChannel-based connection between two frames.
*
* @typedef {'guest'|'host'|'notebook'|'profile'|'sidebar'} Frame
*
* @typedef Message
* @prop {Frame} frame1 - Role of the source frame
* @prop {Frame} frame2 - Role of the target frame
* @prop {'offer'|'request'} type - Message type. "request" messages are sent
* by the source frame to the host frame to request a connection. "offer"
* messages are sent from the host frame back to the source frame and also
* to the target frame, accompanied by a MessagePort.
* @prop {string} requestId - ID of the request. Used to associate "offer"
* messages with their corresponding "request" messages.
* @prop {string} [sourceId] - Identifier for the source frame. This is useful
* in cases where multiple source frames with a given role may connect to
* the same destination frame.
*/
export
type
Message
=
{
/** Role of the source frame. */
frame1
:
Frame
;
/** Role of the target frame. */
frame2
:
Frame
;
/**
* Message type. "request" messages are sent by the source frame to the host
* frame to request a connection. "offer" messages are sent from the host
* frame back to the source frame and also to the target frame, accompanied by
* a MessagePort.
*/
type
:
'offer'
|
'request'
;
/**
* ID of the request. Used to associate "offer" messages with their
* corresponding "request" messages.
*/
requestId
:
string
;
/**
* Identifier for the source frame. This is useful in cases where multiple
* source frames with a given role may connect to the same destination frame.
*/
sourceId
?:
string
;
};
/**
* Return true if an object, eg. from the
data
field of a `MessageEvent`, is a
* Return true if an object, eg. from the
`data`
field of a `MessageEvent`, is a
* valid `Message`.
*
* @param {any} data
* @return {data is Message}
*/
export
function
isMessage
(
data
)
{
export
function
isMessage
(
data
:
any
):
data
is
Message
{
if
(
data
===
null
||
typeof
data
!==
'object'
)
{
return
false
;
}
for
(
le
t
property
of
[
'frame1'
,
'frame2'
,
'type'
,
'requestId'
])
{
for
(
cons
t
property
of
[
'frame1'
,
'frame2'
,
'type'
,
'requestId'
])
{
if
(
typeof
data
[
property
]
!==
'string'
)
{
return
false
;
}
...
...
@@ -41,11 +52,8 @@ export function isMessage(data) {
/**
* Return true if the data payload from a MessageEvent matches `message`.
*
* @param {any} data
* @param {Partial<Message>} message
*/
export
function
isMessageEqual
(
data
,
message
)
{
export
function
isMessageEqual
(
data
:
any
,
message
:
Partial
<
Message
>
)
{
if
(
!
isMessage
(
data
))
{
return
false
;
}
...
...
@@ -60,11 +68,10 @@ export function isMessageEqual(data, message) {
/**
* Check that source is of type Window.
*
* @param {MessageEventSource|null} source
* @return {source is Window}
*/
export
function
isSourceWindow
(
source
)
{
export
function
isSourceWindow
(
source
:
MessageEventSource
|
null
):
source
is
Window
{
if
(
// `source` can be of type Window, MessagePort, ServiceWorker, or null.
// `source instanceof Window` doesn't work in Chrome if `source` is a
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment