|
|
|
@ -8,10 +8,16 @@ import { LoggingProvider } from '../../providers/logging/logging';
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|