Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App2App for iframes (launch & message) #24

Merged
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -22,6 +22,8 @@

// <reference types="ZLUX" />

const IFRAME_LOAD_TIMEOUT = 180000; //3 minutes

export class RecognizerIndex {
propertyName:string;
valueMap:Map<any,RecognitionRule[]> = new Map();
@@ -53,7 +55,15 @@ export class DispatcherConstants implements ZLUX.DispatcherConstants {
readonly ActionType = ActionType;
}

class IframeContext {
constructor(
public readonly timestamp: number,
public readonly data: any
){}
}

export class Dispatcher implements ZLUX.Dispatcher {
private pendingIframes: Map<string,IframeContext[]> = new Map();
private instancesForTypes : Map<string,ApplicationInstanceWrapper[]> = new Map();
private recognizers:RecognitionRule[] = [];
private actionsByID :Map<string,Action> = new Map();
@@ -99,14 +109,16 @@ export class Dispatcher implements ZLUX.Dispatcher {
if (iterationValue.done) break;
let key = iterationValue.value;
let wrappers:ApplicationInstanceWrapper[]|undefined = this.instancesForTypes.get(key);
this.log.debug("disp.heartbeat: key "+JSON.stringify(key)+" val="+wrappers);
this.log.debug("disp.heartbeat: key "+JSON.stringify(key)+" val=",wrappers);
if (wrappers){
for (let wrapper of (wrappers as ApplicationInstanceWrapper[])){
this.log.debug("wrapper="+wrapper);
this.postMessageCallback(wrapper.applicationInstanceId,
{ dispatchType: "echo",
dispatchData: { a: 1 }
});
this.log.debug("wrapper=",wrapper);
if (wrapper.isIframe && this.postMessageCallback) {
this.postMessageCallback(wrapper.applicationInstanceId,
This conversation was marked as resolved by 1000TurquoisePogs

This comment has been minimized.

Copy link
@1000TurquoisePogs

1000TurquoisePogs May 23, 2019

Author Member

What if postmessagecallback doesnt exist

{ dispatchType: "echo",
dispatchData: { a: 1 }
});
}
}
}

@@ -117,6 +129,20 @@ export class Dispatcher implements ZLUX.Dispatcher {
window.setTimeout(dispatcherHeartbeatFunction,Dispatcher.dispatcherHeartbeatInterval);
}

iframeLoaded(instanceId: MVDHosting.InstanceId, identifier: string):void {
this.log.debug(`Dequeuing iframe data`);
if (this.postMessageCallback) {
const contexts = this.pendingIframes.get(identifier);
if (contexts && contexts.length > 0) {
let context = contexts.shift();
if (context) {
this.log.debug(`Sending postmessage of type launch to ${identifier} instance=${instanceId}`,context.data);
this.postMessageCallback(instanceId,{dispatchType: 'launch', dispatchData: {launchMetadata: context.data}});
}
}
}
}

deregisterPluginInstance(plugin: ZLUX.Plugin, applicationInstanceId: MVDHosting.InstanceId):void {
this.log.info(`Dispatcher requested to deregister plugin ${plugin} with id ${applicationInstanceId}`);
let key = plugin.getKey();
@@ -448,6 +474,29 @@ export class Dispatcher implements ZLUX.Dispatcher {
return Promise.reject("no launch callback established");
}
let launchMetadata = this.buildObjectFromTemplate(action.primaryArgument, eventContext);
if (this.postMessageCallback && plugin.getWebContent().framework === 'iframe') {
let contexts = this.pendingIframes.get(plugin.getIdentifier());
if (!contexts) {
contexts = [];
this.pendingIframes.set(plugin.getIdentifier(), contexts);
This conversation was marked as resolved by 1000TurquoisePogs

This comment has been minimized.

Copy link
@1000TurquoisePogs

1000TurquoisePogs May 23, 2019

Author Member

This needs an expiration so that we dont leak memory on things that never say they are ready.

}
contexts.push(new IframeContext(Date.now()+IFRAME_LOAD_TIMEOUT, launchMetadata));
setTimeout(()=> {
if (contexts) {
let now = Date.now();
for (let i = 0; i < contexts.length; i++) {
if (contexts[i].timestamp > now) {
if (i > 0) {
contexts.splice(0,i);
}
return;
}
}
//clear
this.pendingIframes.set(plugin.getIdentifier(),[]);
}
},IFRAME_LOAD_TIMEOUT+1);
}
let appPromise =
this.launchCallback(plugin, launchMetadata).then( (newAppID:MVDHosting.InstanceId) => {
let wrapper = this.getAppInstanceWrapper(plugin,newAppID);
@@ -506,8 +555,14 @@ export class Dispatcher implements ZLUX.Dispatcher {
break;
case ActionType.Message:
this.log.debug('Invoking message type Action');
//TODO is eventContext here different from this.buildObjectFromTemplate(action.primaryArgument, eventContext);
if (wrapper.callbacks && wrapper.callbacks.onMessage) {
wrapper.callbacks.onMessage(eventContext);
} else if (this.postMessageCallback && wrapper.isIframe) {
this.postMessageCallback(wrapper.applicationInstanceId,
{ dispatchType: 'message',
dispatchData: eventContext
});
}
break;
case ActionType.Minimize:
@@ -40,6 +40,7 @@ declare namespace ZLUX {
makeAction(id: string, defaultName: string, targetMode: ActionTargetMode, type: ActionType, targetPluginID: string, primaryArgument: any): Action;
registerApplicationCallbacks(plugin: Plugin, applicationInstanceId: any, callbacks: ApplicationCallbacks): void;
clear(): void;
iframeLoaded(instanceId: MVDHosting.InstanceId, identifier: string);
constants: DispatcherConstants;
}

@@ -5,7 +5,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 0"
},
"keywords": [],
"author": "",
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.