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
ccf8f9bc
Commit
ccf8f9bc
authored
May 25, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
May 25, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate postmessage-json-rpc module to TS
parent
cf8cf399
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
27 deletions
+27
-27
postmessage-json-rpc.ts
src/sidebar/util/postmessage-json-rpc.ts
+27
-27
No files found.
src/sidebar/util/postmessage-json-rpc.
j
s
→
src/sidebar/util/postmessage-json-rpc.
t
s
View file @
ccf8f9bc
...
@@ -2,11 +2,8 @@ import { generateHexString } from '../../shared/random';
...
@@ -2,11 +2,8 @@ import { generateHexString } from '../../shared/random';
/**
/**
* Return a Promise that rejects with an error after `delay` ms.
* Return a Promise that rejects with an error after `delay` ms.
*
* @param {number} delay
* @param {string} message
*/
*/
function
createTimeout
(
delay
,
message
)
{
function
createTimeout
(
delay
:
number
,
message
:
string
)
{
return
new
Promise
((
_
,
reject
)
=>
{
return
new
Promise
((
_
,
reject
)
=>
{
setTimeout
(()
=>
reject
(
new
Error
(
message
)),
delay
);
setTimeout
(()
=>
reject
(
new
Error
(
message
)),
delay
);
});
});
...
@@ -15,24 +12,23 @@ function createTimeout(delay, message) {
...
@@ -15,24 +12,23 @@ function createTimeout(delay, message) {
/**
/**
* Make a JSON-RPC call to a server in another frame using `postMessage`.
* Make a JSON-RPC call to a server in another frame using `postMessage`.
*
*
* @template T
* @param frame - Frame to send call to
* @param {Window} frame - Frame to send call to
* @param origin - Origin filter for `window.postMessage` call
* @param {string} origin - Origin filter for `window.postMessage` call
* @param method - Name of the JSON-RPC method
* @param {string} method - Name of the JSON-RPC method
* @param params - Parameters of the JSON-RPC method
* @param {unknown[]} params - Parameters of the JSON-RPC method
* @param timeout - Maximum time to wait in ms
* @param {number} [timeout] - Maximum time to wait in ms
* @param window_ - Test seam.
* @param {Window} [window_] - Test seam.
* @return A Promise for the response to the call
* @return {Promise<T>} - A Promise for the response to the call
*/
*/
export
function
call
(
export
function
call
<
T
>
(
frame
,
frame
:
Window
,
origin
,
origin
:
string
,
method
,
method
:
string
,
params
=
[],
params
:
unknown
[]
=
[],
timeout
=
2000
,
timeout
=
2000
,
/* istanbul ignore next */
/* istanbul ignore next */
window_
=
window
window_
:
Window
=
window
)
{
)
:
Promise
<
T
>
{
const
id
=
generateHexString
(
10
);
const
id
=
generateHexString
(
10
);
// Send RPC request.
// Send RPC request.
...
@@ -50,8 +46,7 @@ export function call(
...
@@ -50,8 +46,7 @@ export function call(
}
}
// Await response or timeout.
// Await response or timeout.
/** @type {(e: MessageEvent) => void} */
let
listener
:
(
e
:
MessageEvent
)
=>
void
;
let
listener
;
const
response
=
new
Promise
((
resolve
,
reject
)
=>
{
const
response
=
new
Promise
((
resolve
,
reject
)
=>
{
listener
=
event
=>
{
listener
=
event
=>
{
if
(
event
.
origin
!==
origin
)
{
if
(
event
.
origin
!==
origin
)
{
...
@@ -92,7 +87,7 @@ export function call(
...
@@ -92,7 +87,7 @@ export function call(
return
Promise
.
race
(
responseOrTimeout
)
return
Promise
.
race
(
responseOrTimeout
)
.
then
(
result
=>
{
.
then
(
result
=>
{
window_
.
removeEventListener
(
'message'
,
listener
);
window_
.
removeEventListener
(
'message'
,
listener
);
return
result
;
return
result
as
T
;
})
})
.
catch
(
err
=>
{
.
catch
(
err
=>
{
window_
.
removeEventListener
(
'message'
,
listener
);
window_
.
removeEventListener
(
'message'
,
listener
);
...
@@ -104,12 +99,17 @@ export function call(
...
@@ -104,12 +99,17 @@ export function call(
* Send a JSON-RPC 2.0 notification request to another frame via `postMessage`.
* Send a JSON-RPC 2.0 notification request to another frame via `postMessage`.
* No response is expected.
* No response is expected.
*
*
* @param
{Window}
frame - Frame to send call to
* @param frame - Frame to send call to
* @param
{string}
origin - Origin filter for `window.postMessage` call
* @param origin - Origin filter for `window.postMessage` call
* @param
{string}
method - Name of the JSON-RPC method
* @param method - Name of the JSON-RPC method
* @param
{unknown[]}
params - Parameters of the JSON-RPC method
* @param params - Parameters of the JSON-RPC method
*/
*/
export
function
notify
(
frame
,
origin
,
method
,
params
=
[])
{
export
function
notify
(
frame
:
Window
,
origin
:
string
,
method
:
string
,
params
:
unknown
[]
=
[]
)
{
const
request
=
{
const
request
=
{
jsonrpc
:
'2.0'
,
jsonrpc
:
'2.0'
,
method
,
method
,
...
...
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