Skip to content

Commit

Permalink
fixed remaining ts warnings. more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolba committed Mar 31, 2022
1 parent 837c188 commit 952b474
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 26 deletions.
3 changes: 1 addition & 2 deletions packages/main/src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ export class View {
preload: url ? VIEW_PRELOAD : HOME_PRELOAD,
devTools: true,
contextIsolation: true,
// enableRemoteModule:false,
webSecurity: true,
// worldSafeExecuteJavaScript:true
nodeIntegration: false,
},
});
//set bgcolor so view doesn't bleed through to hidden tabs
Expand Down
2 changes: 1 addition & 1 deletion packages/preload/exposedInMainWorld.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ interface Window {
readonly versions: NodeJS.ProcessVersions;
readonly workspace: { isConnected: () => boolean; };
readonly home: { getApps: () => Promise<unknown>; };
readonly fdc3: { getInfo(): import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/ImplementationMetadata").ImplementationMetadata; open: (app: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp, context?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; broadcast: (context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => void; raiseIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp) => Promise<any>; raiseIntentForContext(context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp): Promise<any>; addContextListener: (contextType: string | import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler, handler?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => Listener; addIntentListener: (intent: string, listener: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => Listener; findIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; findIntentsByContext: (context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<any>; getSystemChannels: () => Promise<any>; getOrCreateChannel: (channelId: string) => Promise<any>; joinChannel: (channel: string) => Promise<void>; leaveCurrentChannel: () => Promise<any>; getCurrentChannel: () => Promise<any>; };
readonly fdc3: { getInfo(): import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/ImplementationMetadata").ImplementationMetadata; open: (app: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp, context?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<FDC3Result>; broadcast: (context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => void; raiseIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp) => Promise<FDC3Result>; raiseIntentForContext(context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp): Promise<FDC3Result>; addContextListener: (contextType: string | import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler, handler?: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => Listener; addIntentListener: (intent: string, listener: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => Listener; findIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<FDC3Result>; findIntentsByContext: (context: import("/Users/nicholaskolba/connectifi/agent/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<FDC3Result>; getSystemChannels: () => Promise<FDC3Result>; getOrCreateChannel: (channelId: string) => Promise<FDC3Result>; joinChannel: (channel: string) => Promise<void>; leaveCurrentChannel: () => Promise<FDC3Result>; getCurrentChannel: () => Promise<FDC3Result>; };
}
82 changes: 64 additions & 18 deletions packages/preload/src/view/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TargetApp,
} from '@finos/fdc3';
import { FDC3Event, FDC3EventDetail } from '../../../main/src/types/FDC3Event';
import { ChannelData } from '../../../main/src/types/FDC3Data';
import { FDC3EventEnum } from '../../../main/src/types/FDC3Event';
import { TOPICS } from '../../../main/src/constants';

Expand Down Expand Up @@ -91,13 +92,23 @@ export function createAPI() {
wireMethod(
'broadcast',
{ context: context, channel: channel.id },
true,
{ void: true },
);
},
getCurrentContext: (contextType?: string) => {
return wireMethod('getCurrentContext', {
channel: channel.id,
contextType: contextType,
return new Promise((resolve, reject) => {
wireMethod('getCurrentContext', {
channel: channel.id,
contextType: contextType,
}).then(
(r) => {
const result: Context = r as Context;
resolve(result);
},
(err) => {
reject(err);
},
);
});
},

Expand Down Expand Up @@ -179,11 +190,27 @@ export function createAPI() {
return instance as AppInstance;
};
*/

interface MethodConfig {
void?: boolean;
resultHandler?: { (result: FDC3Result): FDC3Result };
}

type FDC3Result =
| FDC3EventDetail
| Array<ChannelData>
| ChannelData
| Array<Channel>
| Channel
| Context
| null
| void;

const wireMethod = (
method: string,
detail: FDC3EventDetail,
config?: any,
): Promise<any | null> => {
config?: MethodConfig,
): Promise<FDC3Result> => {
const ts: number = Date.now();
const _guid: string = utils.guid();
const eventId = `${method}_${_guid}`;
Expand All @@ -192,14 +219,14 @@ export function createAPI() {
if (config && config.void) {
document.dispatchEvent(utils.fdc3Event(method, detail));
return new Promise((resolve) => {
resolve(null);
resolve();
});
} else {
return new Promise((resolve) => {
document.addEventListener(
`FDC3:return_${eventId}`,
((event: FDC3Event) => {
let r = event.detail;
let r: FDC3Result = event.detail;
if (r !== null && config && config.resultHandler) {
r = config.resultHandler.call(document, r);
}
Expand Down Expand Up @@ -303,11 +330,16 @@ export function createAPI() {
'getSystemChannels',
{},
{
resultHandler: (r: any) => {
const channels = r.map((c: any) => {
return createChannelObject(c.id, 'system', c.displayMetadata);
resultHandler: (r: FDC3Result) => {
r = r as Array<ChannelData>;
const channels = r.map((c: ChannelData) => {
return createChannelObject(
c.id,
'system',
c.displayMetadata || { name: c.id },
);
});
return channels;
return channels as FDC3Result;
},
},
);
Expand All @@ -318,8 +350,17 @@ export function createAPI() {
'getOrCreateChannel',
{ channelId: channelId },
{
resultHandler: (r: any) => {
return createChannelObject(r.id, r.type, r.displayMetadata);
resultHandler: (r: FDC3Result) => {
const result: ChannelData = r as ChannelData;
if (result.id && result.type) {
return createChannelObject(
result.id,
result.type,
result.displayMetadata || { name: result.id },
);
} else {
return null;
}
},
},
);
Expand Down Expand Up @@ -349,8 +390,13 @@ export function createAPI() {
'getCurrentChannel',
{},
{
resultHandler: (r: any) => {
return createChannelObject(r.id, r.type, r.displayMetadata);
resultHandler: (r: FDC3Result) => {
const result: ChannelData = r as ChannelData;
return createChannelObject(
result.id,
result.type,
result.displayMetadata || { name: result.id },
);
},
},
);
Expand Down Expand Up @@ -386,7 +432,7 @@ export function createAPI() {
}
}) as EventListener);

(document as any).addEventListener(TOPICS.FDC3_INTENT, (event: FDC3Event) => {
document.addEventListener(TOPICS.FDC3_INTENT, ((event: FDC3Event) => {
const intent = event.detail.data && event.detail.data.intent;
const context = event.detail.data && event.detail.data.context;
if (intent) {
Expand All @@ -404,7 +450,7 @@ export function createAPI() {
utils.fdc3Event(FDC3EventEnum.IntentComplete, { data: result }),
);
}
});
}) as EventListener);

//map of context listeners by id
const _contextListeners: Map<string, ListenerItem> = new Map();
Expand Down
19 changes: 14 additions & 5 deletions packages/preload/src/view/contentConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ipcRenderer.on(TOPICS.FDC3_START, async (event, args) => {
* handlers will be routinely cleaned up by finding all events that have expired (check timestamp) and rejecting those items
*/
//collection of listeners for api calls coming back from the background script
const returnListeners: Map<string, any> = new Map();
const returnListeners: Map<string, FDC3ReturnListener> = new Map();
//const returnTimeout = 1000 * 60 * 2;

const sendMessage = (msg: FDC3Message) => {
Expand All @@ -63,9 +63,8 @@ const sendMessage = (msg: FDC3Message) => {
port1.onmessage = (event: MessageEvent) => {
const msg = event.data;
//is there a returnlistener registered for the event?
const listener = returnListeners.has(msg.topic)
? returnListeners.get(msg.topic).listener
: null;
const listenerEntry = returnListeners.get(msg.topic);
const listener = listenerEntry ? listenerEntry.listener : null;
if (listener) {
listener.call(window, msg);
returnListeners.delete(msg.name);
Expand All @@ -75,7 +74,17 @@ const sendMessage = (msg: FDC3Message) => {
ipcRenderer.postMessage(`FDC3:${msg.topic}`, msg, [port2]);
};

const wireTopic = (topic: string, config?: any): void => {
interface FDC3ReturnListener {
ts: number;
listener: { (msg: FDC3Message): void };
}

interface TopicConfig {
isVoid?: boolean;
cb?: { (event: FDC3Event): void };
}

const wireTopic = (topic: string, config?: TopicConfig): void => {
document.addEventListener(`FDC3:${topic}`, ((e: FDC3Event) => {
console.log('contentConnect event', e);
const cb = config ? config.cb : null;
Expand Down

0 comments on commit 952b474

Please sign in to comment.