Commit e7b406e9 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Add JSON-RPC 2.0 notification support to client

parent 886e3c15
...@@ -98,3 +98,21 @@ export function call( ...@@ -98,3 +98,21 @@ export function call(
throw err; throw err;
}); });
} }
/**
* Send a JSON-RPC 2.0 notification request to another frame via `postMessage`.
* No response is expected.
*
* @param {Window} frame - Frame to send call to
* @param {string} origin - Origin filter for `window.postMessage` call
* @param {string} method - Name of the JSON-RPC method
* @param {unknown[]} params - Parameters of the JSON-RPC method
*/
export function notify(frame, origin, method, params = []) {
const request = {
jsonrpc: '2.0',
method,
params,
};
frame.postMessage(request, origin);
}
import EventEmitter from 'tiny-emitter'; import EventEmitter from 'tiny-emitter';
import { call, $imports } from '../postmessage-json-rpc'; import { call, notify, $imports } from '../postmessage-json-rpc';
class FakeWindow { class FakeWindow {
constructor() { constructor() {
...@@ -14,6 +14,25 @@ describe('sidebar/util/postmessage-json-rpc', () => { ...@@ -14,6 +14,25 @@ describe('sidebar/util/postmessage-json-rpc', () => {
const origin = 'https://embedder.com'; const origin = 'https://embedder.com';
const messageId = 42; const messageId = 42;
describe('notify', () => {
let frame;
beforeEach(() => {
frame = { postMessage: sinon.stub() };
});
it('should send a JSON-RPC 2.0 notification to the frame', () => {
notify(frame, origin, 'testMethod', [1, 2, 3]);
assert.calledOnce(frame.postMessage);
assert.calledWith(frame.postMessage, {
jsonrpc: '2.0',
method: 'testMethod',
params: [1, 2, 3],
});
});
});
describe('call', () => { describe('call', () => {
let frame; let frame;
let fakeWindow; let fakeWindow;
......
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