From 9a5ab01c63acdb855f97723653e789abaa7f7912 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 1 Apr 2019 16:39:15 +0200 Subject: [PATCH] blockly protocol refactor / fixes --- package.json | 2 +- src/pages/blockly/blockly_protocol.ts | 32 ++++++++++++--------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 125e3cd..e505199 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "start": "ionic-app-scripts serve", "clean": "ionic-app-scripts clean", "build": "ionic-app-scripts build --prod", - "android:build": "ionic cordova build --release --prod android", + "android:build": "npm run clean && ionic cordova build --release --prod android", "android:build:debug": "ionic cordova build android", "android:deploy": "adb install -r platforms/android/app/build/outputs/apk/debug/app-debug.apk", "android:start": "adb shell am force-stop de.sensebox.blockly; adb shell am start -n de.sensebox.blockly/de.sensebox.blockly.MainActivity; sleep 5; adb logcat | grep -F \"`adb shell ps | grep de.sensebox.blockly | awk -F' ' '{print $2}'`\"", diff --git a/src/pages/blockly/blockly_protocol.ts b/src/pages/blockly/blockly_protocol.ts index 87f6e9f..4572aaa 100644 --- a/src/pages/blockly/blockly_protocol.ts +++ b/src/pages/blockly/blockly_protocol.ts @@ -8,10 +8,16 @@ import { LoggingProvider } from '../../providers/logging/logging'; */ export class BlocklyMessageProtocol { - ready: Promise + // resolve ready promise once the blocklyFrame is ready + ready = new Promise(resolve => { + window.addEventListener('message', (ev: IframePostMessageEvent) => { + // @HACK @FIXME: timeout is required, as blockly resolves some async functions + // after firing the `ready` event.. + if (ev.data.type === 'ready') setTimeout(resolve, 300) + }) + }) - // defines the expected response type for each request type - private reqResPatterns: BlocklyReqPatterns = { + static reqResPatterns: BlocklyReqPatterns = { 'getSketch': 'sketch', 'getXml': 'xml', 'setXml': null, @@ -26,20 +32,10 @@ export class BlocklyMessageProtocol { else this.log.debug(`received ${ev.data.type} message from blockly`, { message: ev.data }) }) - - // resolve ready promise once the blocklyFrame is ready - this.ready = new Promise(resolve => { - window.addEventListener('message', (ev: IframePostMessageEvent) => { - // @HACK @FIXME: timeout is required, as blockly resolves some async functions - // after firing the `ready` event.. - if (ev.data.type === 'ready') setTimeout(resolve, 300) - }) - }) } toggleView() { this.sendRequest({ type: 'toggleView' }) } setXml(data: string) { this.sendRequest({ type: 'setXml', data }) } - getXml() { return this.sendRequest({ type: 'getXml' }) } getSketch() { return this.sendRequest({ type: 'getSketch' }) } @@ -48,13 +44,13 @@ export class BlocklyMessageProtocol { if ( !this.blocklyFrame || - !this.blocklyFrame.nativeElement && + !this.blocklyFrame.nativeElement || !this.blocklyFrame.nativeElement.contentWindow ) { - throw Error('cannot access blockly frame') + throw new Error('cannot access blockly frame') } - const expectResponse = this.reqResPatterns[req.type] + const expectResponse = BlocklyMessageProtocol.reqResPatterns[req.type] this.log.debug(`sending ${req.type} message to blockly, expecting response: ${expectResponse}`, { message: req }) if (!expectResponse) @@ -65,14 +61,14 @@ export class BlocklyMessageProtocol { const resHandler = ({ data: res }: IframePostMessageEvent) => { if (expectResponse !== res.type) return window.removeEventListener('message', resHandler) - if (expectResponse === 'error') reject(res.data) + if (res.type === 'error') reject(res.data) else resolve(res.data) } // TODO: promise reject after timeout? window.addEventListener('message', resHandler) }) - // send message after registering the subscribe handler! + // send message *after* registering the response handler! this.blocklyFrame.nativeElement.contentWindow.postMessage(req, '*') return resPromise }