Commit ccf8f9bc authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate postmessage-json-rpc module to TS

parent cf8cf399
...@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment