From fec9ae6b55a6d29cb5e787d4d4229fa3b2f6a855 Mon Sep 17 00:00:00 2001 From: Robbie Carlton Date: Mon, 1 Feb 2021 16:48:08 -0700 Subject: [PATCH 1/8] handle signals --- src/index.ts | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 19d0896..7babb50 100644 --- a/src/index.ts +++ b/src/index.ts @@ -73,8 +73,8 @@ const COMB = { * @example * const child = await COMB.connect( "http://localhost:8002" ); */ - async connect(url, timeout) { - const child = new ChildAPI(url, timeout); + async connect(url, timeout, signalCb) { + const child = new ChildAPI(url, timeout, signalCb); await child.connect(); return child; }, @@ -115,6 +115,7 @@ class ChildAPI { handshake: any; class_name: string; loaded: boolean; + signalCb: any; /** * Initialize a child frame using the given URL. @@ -130,6 +131,7 @@ class ChildAPI { * @prop {promise} handshake - Promise that is waiting for connection confirmation * @prop {string} class_name - iFrame's unique class name * @prop {boolean} loaded - Indicates if iFrame successfully loaded + * @prop {any} signalCb - A callback that's run when we receive a signal * * @example * const child = new ChildAPI( url ); @@ -138,15 +140,18 @@ class ChildAPI { * await child.set("mode", mode ); * let response = await child.run("signIn"); */ - constructor(url, timeout = 5_000) { + constructor(url, timeout = 5_000, signalCb) { this.url = url; this.msg_count = 0; this.responses = {}; this.loaded = false; + this.signalCb = signalCb; + this.class_name = "comb-frame-" + ChildAPI.frame_count++; this.handshake = async_with_timeout(async () => { // log.info("Init Postmate handshake"); + Postmate.debug = true const handshake = new Postmate({ "container": document.body, "url": this.url, @@ -186,8 +191,7 @@ class ChildAPI { if (this.loaded) { // log.error("iFrame loaded but could not communicate with COMB"); throw new TimeoutError("Failed to complete COMB handshake", err.timeout); - } - else { + } else { // log.error("iFrame did not trigger load event"); throw new TimeoutError("Failed to load iFrame", err.timeout); } @@ -212,6 +216,10 @@ class ChildAPI { delete this.responses[k]; }); + if (this.signalCb) { + child.on('signal', this.signalCb) + } + this.msg_bus = child; return this; @@ -320,9 +328,11 @@ class ParentAPI { * await parent.connect(); */ constructor(methods, properties = {}) { + this.methods = methods; this.properties = properties; + Postmate.debug = true this.listener = new Postmate.Model({ "exec": async (data) => { const [msg_id, method, args] = data; @@ -348,6 +358,9 @@ class ParentAPI { this.properties[key] = value; this.msg_bus.emit("response", [msg_id, true]); + }, + "signal": async (data) => { + console.log('signal listener pow', data) } }); } @@ -367,12 +380,31 @@ class ParentAPI { * }); * await parent.connect(); */ - async connect() { + async connect () { this.msg_bus = await this.listener; return this; } + /** + * Wait for parent to connect. + * + * @async + * + * @param {object} signal - Functions that are available for the parent to call. + * + * @example + * const parent = new ParentAPI({ + * "hello": async function () { + * return "Hello world"; + * } + * }); + * await parent.sendSignal(signal); + */ + async sendSignal (signal) { + this.msg_bus.emit('signal', signal) + } + } export { From 0361d3633d6d384c37773cc244eb3d3e87d92597 Mon Sep 17 00:00:00 2001 From: Robbie Carlton Date: Tue, 2 Feb 2021 11:25:55 -0700 Subject: [PATCH 2/8] remove signal test --- src/index.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7babb50..d8baa25 100644 --- a/src/index.ts +++ b/src/index.ts @@ -358,9 +358,6 @@ class ParentAPI { this.properties[key] = value; this.msg_bus.emit("response", [msg_id, true]); - }, - "signal": async (data) => { - console.log('signal listener pow', data) } }); } From d52226802ed90f5a710d312716dd811e6a60d744 Mon Sep 17 00:00:00 2001 From: Robbie Carlton Date: Wed, 3 Feb 2021 11:00:25 -0700 Subject: [PATCH 3/8] formatting --- html/chaperone/index.html | 55 +++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/html/chaperone/index.html b/html/chaperone/index.html index c30580d..484ca0a 100644 --- a/html/chaperone/index.html +++ b/html/chaperone/index.html @@ -1,35 +1,34 @@ -

Chaperone!

+ try { + COMB.debug(); + const parent = await COMB.listen({ + "test": async function (...args) { + return "Hello World: " + JSON.stringify(args); + }, + "test_synchronous": function () { + return "Hello World"; + }, + "test_error": function (...args) { + return new HolochainTestError("Method did not succeed\n" + JSON.stringify(args)); + }, + "test_synchronous_error": function () { + return new HolochainTestError("Method did not succeed"); + } + }); + } catch (err) { + console.error(String(err)); + } + })(); + \ No newline at end of file From dfa22e24b01299f1054c2ebfa006431c00d7c61c Mon Sep 17 00:00:00 2001 From: Robbie Carlton Date: Wed, 3 Feb 2021 11:34:46 -0700 Subject: [PATCH 4/8] signal test --- html/chaperone/index.html | 3 +++ tests/integration/test_comb.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/html/chaperone/index.html b/html/chaperone/index.html index 484ca0a..3495b68 100644 --- a/html/chaperone/index.html +++ b/html/chaperone/index.html @@ -20,6 +20,9 @@

Chaperone!

"test_synchronous": function () { return "Hello World"; }, + "test_signal": function (signal) { + return parent.sendSignal(signal) + }, "test_error": function (...args) { return new HolochainTestError("Method did not succeed\n" + JSON.stringify(args)); }, diff --git a/tests/integration/test_comb.js b/tests/integration/test_comb.js index 5611369..18f4428 100644 --- a/tests/integration/test_comb.js +++ b/tests/integration/test_comb.js @@ -159,6 +159,28 @@ describe("Testing COMB", function () { } }); + it("should call the provided signalCb when sendSignal is called on the parent", async function () { + try { + // have to setup our spy function in the puppeteer evaluation context + await page.evaluate(() => { + window.signalCb = function(signal) { + window.signalCbCalledWith = signal + } + }); + + const expectedSignal = "Hello COMB"; + + const signalCbCalledWith = await page.evaluate(async function (chap_url, expectedSignal) { + window.child = await COMB.connect(chap_url, 5000, window.signalCb); + await child.run("test_signal", expectedSignal); + return window.signalCbCalledWith + }, chap_url, expectedSignal); + + expect(signalCbCalledWith).to.equal(expectedSignal); + } finally { + } + }); + it("should timeout because of wrong frame URL", async function () { try { const result = await page.evaluate(async function () { From 2d974ee45c92c308473cc2b297c52586ba3ba96e Mon Sep 17 00:00:00 2001 From: Robbie Carlton Date: Wed, 3 Feb 2021 11:44:55 -0700 Subject: [PATCH 5/8] cleanup --- html/chaperone/index.html | 2 +- src/index.ts | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/html/chaperone/index.html b/html/chaperone/index.html index 3495b68..7f70bad 100644 --- a/html/chaperone/index.html +++ b/html/chaperone/index.html @@ -34,4 +34,4 @@

Chaperone!

console.error(String(err)); } })(); - \ No newline at end of file + diff --git a/src/index.ts b/src/index.ts index d8baa25..b9805db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -151,7 +151,6 @@ class ChildAPI { this.class_name = "comb-frame-" + ChildAPI.frame_count++; this.handshake = async_with_timeout(async () => { // log.info("Init Postmate handshake"); - Postmate.debug = true const handshake = new Postmate({ "container": document.body, "url": this.url, @@ -332,7 +331,6 @@ class ParentAPI { this.methods = methods; this.properties = properties; - Postmate.debug = true this.listener = new Postmate.Model({ "exec": async (data) => { const [msg_id, method, args] = data; @@ -384,11 +382,11 @@ class ParentAPI { } /** - * Wait for parent to connect. + * Send holochain conductor signal to parent. * * @async * - * @param {object} signal - Functions that are available for the parent to call. + * @param {object} signal - The signal * * @example * const parent = new ParentAPI({ From a558690d2e64c9ec0eb4edd789c06c1a43bfb165 Mon Sep 17 00:00:00 2001 From: Robbie Carlton Date: Thu, 4 Feb 2021 15:59:21 -0700 Subject: [PATCH 6/8] whitespace --- src/index.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index b9805db..620b9b6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -122,16 +122,16 @@ class ChildAPI { * * @class ChildAPI * - * @param {string} url - URL that is used as 'src' for the iframe + * @param {string} url - URL that is used as 'src' for the iframe * - * @prop {string} url - iFrame URL - * @prop {number} msg_count - Incrementing message ID - * @prop {object} responses - Dictionary of request Promises waiting for their responses - * @prop {object} msg_bus - Postmate instance - * @prop {promise} handshake - Promise that is waiting for connection confirmation - * @prop {string} class_name - iFrame's unique class name - * @prop {boolean} loaded - Indicates if iFrame successfully loaded - * @prop {any} signalCb - A callback that's run when we receive a signal + * @prop {string} url - iFrame URL + * @prop {number} msg_count - Incrementing message ID + * @prop {object} responses - Dictionary of request Promises waiting for their responses + * @prop {object} msg_bus - Postmate instance + * @prop {promise} handshake - Promise that is waiting for connection confirmation + * @prop {string} class_name - iFrame's unique class name + * @prop {boolean} loaded - Indicates if iFrame successfully loaded + * @prop {any} signalCb - A callback that's run when we receive a signal * * @example * const child = new ChildAPI( url ); @@ -231,8 +231,8 @@ class ChildAPI { * @private * * @param {string} method - Internally consistent Postmate method - * @param {string} name - Function name or property name - * @param {*} data - Variable input that is handled by child API + * @param {string} name - Function name or property name + * @param {*} data - Variable input that is handled by child API * * @return {*} Response from child */ @@ -264,7 +264,7 @@ class ChildAPI { * @async * * @param {string} key - Property name - * @param {*} value - Property value + * @param {*} value - Property value * * @return {boolean} Success status * @@ -314,8 +314,8 @@ class ParentAPI { * @param {object} properties - Properties to memorize in the instance for later use, optional * * @prop {promise} listener - Promise that is waiting for parent to connect - * @prop {object} msg_bus - Postmate instance - * @prop {object} methods - Method storage + * @prop {object} msg_bus - Postmate instance + * @prop {object} methods - Method storage * @prop {object} properties - Set properties storage * * @example From 0bd6421397b88d20b367c6af81365032e791bffa Mon Sep 17 00:00:00 2001 From: Robbie Carlton Date: Thu, 4 Feb 2021 16:01:16 -0700 Subject: [PATCH 7/8] more whitespace --- src/index.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index 620b9b6..6041616 100644 --- a/src/index.ts +++ b/src/index.ts @@ -230,9 +230,9 @@ class ChildAPI { * @async * @private * - * @param {string} method - Internally consistent Postmate method - * @param {string} name - Function name or property name - * @param {*} data - Variable input that is handled by child API + * @param {string} method - Internally consistent Postmate method + * @param {string} name - Function name or property name + * @param {*} data - Variable input that is handled by child API * * @return {*} Response from child */ @@ -263,8 +263,8 @@ class ChildAPI { * * @async * - * @param {string} key - Property name - * @param {*} value - Property value + * @param {string} key - Property name + * @param {*} value - Property value * * @return {boolean} Success status * @@ -310,13 +310,13 @@ class ParentAPI { * * @class ParentAPI * - * @param {object} methods - Functions that are available for the parent to call. - * @param {object} properties - Properties to memorize in the instance for later use, optional + * @param {object} methods - Functions that are available for the parent to call. + * @param {object} properties - Properties to memorize in the instance for later use, optional * - * @prop {promise} listener - Promise that is waiting for parent to connect - * @prop {object} msg_bus - Postmate instance - * @prop {object} methods - Method storage - * @prop {object} properties - Set properties storage + * @prop {promise} listener - Promise that is waiting for parent to connect + * @prop {object} msg_bus - Postmate instance + * @prop {object} methods - Method storage + * @prop {object} properties - Set properties storage * * @example * const parent = new ParentAPI({ From 422a7ed403f572079a246690fa41a9d9bd58e763 Mon Sep 17 00:00:00 2001 From: Robbie Carlton Date: Thu, 4 Feb 2021 16:04:27 -0700 Subject: [PATCH 8/8] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c96d6de..614e822 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Both the parent window and iFrame must load COMB.