-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidied up code after @novavi's review
- Loading branch information
Showing
13 changed files
with
141 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { DesktopAgent} from '@finos/fdc3' | ||
import { loader as loader1 } from './strategies/post-message-load-js' | ||
import { loader as loader2 } from './strategies/electron-event'; | ||
|
||
import { DEFAULT_OPTIONS, Options } from './types'; | ||
|
||
/** | ||
* This return an FDC3 API. Called by Apps. | ||
*/ | ||
export function load(options: Options = DEFAULT_OPTIONS) : Promise<DesktopAgent> { | ||
|
||
function handleGenericOptions(da: DesktopAgent) { | ||
if ((options.setWindowGlobal) && (window.fdc3 == null)) { | ||
window.fdc3 = da; | ||
} | ||
|
||
return da; | ||
} | ||
|
||
const strategies = [ loader1(options), loader2(options) ]; | ||
|
||
return Promise.any(strategies) | ||
.then(da => handleGenericOptions(da)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { DesktopAgent } from "@finos/fdc3"; | ||
import { Loader, Options, } from "../types"; | ||
import { fdc3Ready } from "@finos/fdc3"; | ||
|
||
/** | ||
* This approach will resolve the loader promise if the fdc3Ready event occurs. | ||
* This is done by electron implementations setting window.fdc3. | ||
*/ | ||
export const loader: Loader = (_options: Options) => { | ||
|
||
const out = new Promise<DesktopAgent>((resolve) => { | ||
fdc3Ready().then(() => { | ||
resolve(window.fdc3); | ||
}) | ||
|
||
}); | ||
|
||
return out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,63 @@ | ||
import { AppIdentifier, DesktopAgent} from '@finos/fdc3' | ||
import { APIResponseMessage, AppIdentifierResolver, DesktopAgentDetailResolver, Options, Strategy } from '../types' | ||
import { load } from '../loaders/load-with-import'; | ||
import { DesktopAgent } from '@finos/fdc3' | ||
import { APIResponseMessage, AppChecker, DesktopAgentDetailResolver, Loader, Options, Supplier } from '../types' | ||
import { loadJS } from '../loaders/load-with-import'; | ||
|
||
const FDC3_API_REQUEST_MESSAGE_TYPE = 'FDC3-API-Request'; | ||
const FDC3_API_RESPONSE_MESSAGE_TYPE = 'FDC3-API-Response'; | ||
|
||
|
||
export const strategy : Strategy = { | ||
|
||
supply : (url: string, resolver: AppIdentifierResolver, detailsResolver: DesktopAgentDetailResolver) => { | ||
function createResponseMessage(appIdentifier: AppIdentifier) : APIResponseMessage { | ||
export const supplier: Supplier = (url: string, checker: AppChecker, detailsResolver: DesktopAgentDetailResolver) => { | ||
function createResponseMessage(source: Window): APIResponseMessage { | ||
return { | ||
type: FDC3_API_RESPONSE_MESSAGE_TYPE, | ||
url, | ||
appIdentifier : { | ||
appId: appIdentifier.appId, | ||
instanceId: appIdentifier.instanceId | ||
}, | ||
daDetails: detailsResolver(appIdentifier) | ||
url, | ||
details: detailsResolver(source) | ||
} | ||
} | ||
|
||
window.addEventListener( | ||
"message", | ||
(event) => { | ||
console.log("Received: "+JSON.stringify(event)); | ||
const data = event.data; | ||
if (data == FDC3_API_REQUEST_MESSAGE_TYPE) { | ||
const origin = event.origin; | ||
const source = event.source as Window | ||
const appIdentifier = resolver(source); | ||
if (appIdentifier != null) { | ||
console.log(`API Request Origin: ${origin} Source: ${source}`); | ||
source.postMessage(createResponseMessage(appIdentifier), origin); | ||
console.log("Received: " + JSON.stringify(event)); | ||
const data = event.data; | ||
if (data == FDC3_API_REQUEST_MESSAGE_TYPE) { | ||
const origin = event.origin; | ||
const source = event.source as Window | ||
if (checker(source)) { | ||
console.log(`API Request Origin: ${origin} Source: ${source}`); | ||
source.postMessage(createResponseMessage(source), origin); | ||
} | ||
} | ||
} | ||
}); | ||
}, | ||
} | ||
|
||
load : (options: Options) => { | ||
|
||
function handleOptions(da: DesktopAgent) { | ||
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(data) | ||
.then(da => handleOptions(da)) | ||
.then(da => resolve(da)) | ||
} else { | ||
reject("Incorrect API Response Message"); | ||
} | ||
}, {once: true}); | ||
}); | ||
|
||
const da = window.opener; | ||
export const loader: Loader = (_options: Options) => { | ||
|
||
if (da != null) { | ||
window.opener.postMessage(FDC3_API_REQUEST_MESSAGE_TYPE, "*"); | ||
} | ||
function handleOptions(da: DesktopAgent) { | ||
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) { | ||
loadJS(data) | ||
.then(da => handleOptions(da)) | ||
.then(da => resolve(da)) | ||
} else { | ||
reject("Incorrect API Response Message"); | ||
} | ||
}, { once: true }); | ||
}); | ||
|
||
const da = window.opener; | ||
|
||
return out; | ||
if (da != null) { | ||
window.opener.postMessage(FDC3_API_REQUEST_MESSAGE_TYPE, "*"); | ||
} | ||
|
||
} | ||
return out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { supplier } from './strategies/post-message-load-js' | ||
import { AppChecker, DesktopAgentDetailResolver } from './types'; | ||
|
||
/** | ||
* This configures the postMessage listener to respond to requests for desktop agent APIs. | ||
* Called by the desktop agent | ||
*/ | ||
export function supply(url: string, appIdResolver: AppChecker, detailsResolver: DesktopAgentDetailResolver) { | ||
supplier(url, appIdResolver, detailsResolver); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.