mirror of
https://github.com/sensebox/blockly-app
synced 2025-02-22 06:23:59 +01:00
blockly protocol refactor / fixes
This commit is contained in:
parent
bdca552dea
commit
9a5ab01c63
2 changed files with 15 additions and 19 deletions
|
@ -8,7 +8,7 @@
|
||||||
"start": "ionic-app-scripts serve",
|
"start": "ionic-app-scripts serve",
|
||||||
"clean": "ionic-app-scripts clean",
|
"clean": "ionic-app-scripts clean",
|
||||||
"build": "ionic-app-scripts build --prod",
|
"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:build:debug": "ionic cordova build android",
|
||||||
"android:deploy": "adb install -r platforms/android/app/build/outputs/apk/debug/app-debug.apk",
|
"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}'`\"",
|
"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}'`\"",
|
||||||
|
|
|
@ -8,10 +8,16 @@ import { LoggingProvider } from '../../providers/logging/logging';
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class BlocklyMessageProtocol {
|
export class BlocklyMessageProtocol {
|
||||||
ready: Promise<void>
|
// 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
|
static reqResPatterns: BlocklyReqPatterns = {
|
||||||
private reqResPatterns: BlocklyReqPatterns = {
|
|
||||||
'getSketch': 'sketch',
|
'getSketch': 'sketch',
|
||||||
'getXml': 'xml',
|
'getXml': 'xml',
|
||||||
'setXml': null,
|
'setXml': null,
|
||||||
|
@ -26,20 +32,10 @@ export class BlocklyMessageProtocol {
|
||||||
else
|
else
|
||||||
this.log.debug(`received ${ev.data.type} message from blockly`, { message: ev.data })
|
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' }) }
|
toggleView() { this.sendRequest({ type: 'toggleView' }) }
|
||||||
setXml(data: string) { this.sendRequest({ type: 'setXml', data }) }
|
setXml(data: string) { this.sendRequest({ type: 'setXml', data }) }
|
||||||
|
|
||||||
getXml() { return this.sendRequest({ type: 'getXml' }) }
|
getXml() { return this.sendRequest({ type: 'getXml' }) }
|
||||||
getSketch() { return this.sendRequest({ type: 'getSketch' }) }
|
getSketch() { return this.sendRequest({ type: 'getSketch' }) }
|
||||||
|
|
||||||
|
@ -48,13 +44,13 @@ export class BlocklyMessageProtocol {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!this.blocklyFrame ||
|
!this.blocklyFrame ||
|
||||||
!this.blocklyFrame.nativeElement &&
|
!this.blocklyFrame.nativeElement ||
|
||||||
!this.blocklyFrame.nativeElement.contentWindow
|
!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 })
|
this.log.debug(`sending ${req.type} message to blockly, expecting response: ${expectResponse}`, { message: req })
|
||||||
|
|
||||||
if (!expectResponse)
|
if (!expectResponse)
|
||||||
|
@ -65,14 +61,14 @@ export class BlocklyMessageProtocol {
|
||||||
const resHandler = ({ data: res }: IframePostMessageEvent) => {
|
const resHandler = ({ data: res }: IframePostMessageEvent) => {
|
||||||
if (expectResponse !== res.type) return
|
if (expectResponse !== res.type) return
|
||||||
window.removeEventListener('message', resHandler)
|
window.removeEventListener('message', resHandler)
|
||||||
if (expectResponse === 'error') reject(res.data)
|
if (res.type === 'error') reject(res.data)
|
||||||
else resolve(res.data)
|
else resolve(res.data)
|
||||||
}
|
}
|
||||||
// TODO: promise reject after timeout?
|
// TODO: promise reject after timeout?
|
||||||
window.addEventListener('message', resHandler)
|
window.addEventListener('message', resHandler)
|
||||||
})
|
})
|
||||||
|
|
||||||
// send message after registering the subscribe handler!
|
// send message *after* registering the response handler!
|
||||||
this.blocklyFrame.nativeElement.contentWindow.postMessage(req, '*')
|
this.blocklyFrame.nativeElement.contentWindow.postMessage(req, '*')
|
||||||
return resPromise
|
return resPromise
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue