Skip to content

Commit

Permalink
Handling options
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Moffat <[email protected]>
  • Loading branch information
robmoffat committed Jul 1, 2023
1 parent 1bfbe91 commit c70bf6e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/lib/loaders/load-with-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { APIResponseMessage, FDC3Initialiser } from "../types";
/**
* This loads the script using an import
*/
export function load(resolve: (da: DesktopAgent) => void, data: APIResponseMessage) {
import(data.url).then(ns => {
export function load(data: APIResponseMessage) : Promise<DesktopAgent> {
return import(/* @vite-ignore */ data.url).then(ns => {
const init = ns.default as FDC3Initialiser;
const da = init(data.appIdentifier);
resolve(da);
return da;
})
}
16 changes: 13 additions & 3 deletions src/lib/strategies/post-message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppIdentifier, DesktopAgent} from '@finos/fdc3'
import { APIResponseMessage, AppIdentifierResolver, Strategy } from '../types'
import { APIResponseMessage, AppIdentifierResolver, Options, Strategy } from '../types'
import { load } from '../loaders/load-with-import';

const FDC3_API_REQUEST_MESSAGE_TYPE = 'FDC3-API-Request';
Expand Down Expand Up @@ -36,14 +36,24 @@ export const strategy : Strategy = {
});
},

load : (options: any) => {
load : (options: Options) => {

function handleOptions(da: DesktopAgent) {
if ((options.setWindowGlobal) && (window.fdc3 == null)) {
window.fdc3 = da;
}

return da;
}

const out = new Promise<DesktopAgent>((resolve, reject) => {
// setup listener for message and retrieve JS URL from it
window.addEventListener("message", (event) => {
const data : APIResponseMessage = event.data ;
if (data.type == FDC3_API_RESPONSE_MESSAGE_TYPE) {
load(resolve, data);
load(data)
.then(da => handleOptions(da))
.then(da => resolve(da))
} else {
reject("Incorrect API Response Message");
}
Expand Down

0 comments on commit c70bf6e

Please sign in to comment.