Skip to content

Commit

Permalink
Merge pull request #46 from nkolba/1.2-compliance
Browse files Browse the repository at this point in the history
fix all 1.2 Conformance Issues
  • Loading branch information
nkolba authored Jun 24, 2022
2 parents 5efd40d + ae57b31 commit 03d1870
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 119 deletions.
40 changes: 26 additions & 14 deletions packages/main/src/listeners/fdc3Listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
IntentMetadata,
TargetApp,
IntentResolution,
ResolveError,
OpenError,
} from '@finos/fdc3';
import { FDC3Message } from '../types/FDC3Message';
import {
Expand Down Expand Up @@ -367,14 +369,17 @@ _listeners.push({
if (newView && msg.context) {
newView.setPendingContext(msg.context, msg.source);
}
//resolve with the window identfier

resolve();
//reject?
} else {
reject(OpenError.AppNotFound);
}
} else {
reject(OpenError.AppNotFound);
}
},
(err) => {
reject(err);
() => {
reject(OpenError.AppNotFound);
},
);
});
Expand Down Expand Up @@ -805,6 +810,9 @@ _listeners.push({
apps: [],
};

if (j.length === 0) {
reject(ResolveError.NoAppsFound);
}
// r.apps = j;
//find intent display name from app directory data
const intnt = j[0].intents.filter((i) => {
Expand All @@ -819,6 +827,7 @@ _listeners.push({
apps: [],
};
}

j.forEach((dirApp) => {
r.apps.push({
name: dirApp.name,
Expand All @@ -832,8 +841,7 @@ _listeners.push({
resolve(r as AppIntent);
},
() => {
//no results found for the app-directory, there may still be intents from live apps
resolve({ intent: { name: intent, displayName: '' }, apps: [] });
reject(ResolveError.NoAppsFound);
},
);
} else {
Expand Down Expand Up @@ -890,14 +898,18 @@ _listeners.push({
r.push(entry);
});
}
resolve(r);
if (r.length > 0) {
resolve(r);
} else {
reject(ResolveError.NoAppsFound);
}
},
(err) => {
reject(err);
() => {
reject(ResolveError.NoAppsFound);
},
);
} else {
reject('no context provided');
reject(ResolveError.NoAppsFound);
}
});
},
Expand Down Expand Up @@ -1073,7 +1085,7 @@ _listeners.push({
try {
data = await _r.json();
} catch (err) {
console.log('error parsing json', err);
reject(ResolveError.NoAppsFound);
}

if (data) {
Expand Down Expand Up @@ -1221,11 +1233,11 @@ _listeners.push({
}
} else {
//show message indicating no handler for the intent...
reject('no apps found for intent');
reject(ResolveError.NoAppsFound);
}
});
} else {
reject('No intent provided');
reject(ResolveError.NoAppsFound);
}
});
},
Expand Down Expand Up @@ -1494,7 +1506,7 @@ _listeners.push({
}
} else {
//show message indicating no handler for the intent...
reject('no apps found for intent');
reject(ResolveError.NoAppsFound);
}
};
raiseIntentForContext();
Expand Down
30 changes: 21 additions & 9 deletions packages/main/src/listeners/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,28 @@ export class RuntimeListener {
const runtime = this.runtime;

ipcMain.on(l.name, (event, args) => {
l.handler.call(this, runtime, args as FDC3Message).then((r) => {
console.log('handler response', r, 'args', args);
l.handler.call(this, runtime, args as FDC3Message).then(
(r) => {
console.log('handler response', r, 'args', args);

if (event.ports) {
event.ports[0].postMessage({
topic: args.data.eventId,
data: r,
});
}
});
if (event.ports) {
event.ports[0].postMessage({
topic: args.data.eventId,
data: r,
});
}
},
(err) => {
console.log('handler error', err, 'args', args);

if (event.ports) {
event.ports[0].postMessage({
topic: args.data.eventId,
error: err,
});
}
},
);
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/main/src/types/FDC3Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface FDC3EventDetail {
source?: string;
/* identifier of the browserView the event originated from */
viewId?: string;
error?: string;
}

export interface FDC3ResponseData {
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/types/FDC3Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface FDC3Message {
tabId?: number;
selected?: FDC3App | null;
context?: Context | null;
error?: string;
}

export interface FDC3MessageData {
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/electron-fdc3/node_modules/@finos/fdc3/dist/api/ImplementationMetadata").ImplementationMetadata; open: (app: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp, context?: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<FDC3Result>; broadcast: (context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => void; raiseIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp) => Promise<FDC3Result>; raiseIntentForContext(context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp): Promise<FDC3Result>; addContextListener: (contextType: string | import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler, handler?: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => Listener; addIntentListener: (intent: string, listener: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => Listener; findIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<FDC3Result>; findIntentsByContext: (context: import("/Users/nicholaskolba/connectifi/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>; };
readonly fdc3: { getInfo(): import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/ImplementationMetadata").ImplementationMetadata; open: (app: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp, context?: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<FDC3Result>; broadcast: (context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => void; raiseIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp) => Promise<FDC3Result>; raiseIntentForContext(context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context, app?: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").TargetApp): Promise<FDC3Result>; addContextListener: (contextType: string | import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler, handler?: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => FDC3Listener; addIntentListener: (intent: string, listener: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/api/Types").ContextHandler) => FDC3Listener; findIntent: (intent: string, context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<FDC3Result>; findIntentsByContext: (context: import("/Users/nicholaskolba/connectifi/electron-fdc3/node_modules/@finos/fdc3/dist/context/ContextTypes").Context) => Promise<FDC3Result>; getSystemChannels: () => Promise<FDC3Result>; getOrCreateChannel: (channelId: string) => Promise<FDC3Result>; joinChannel: (channel: string) => Promise<FDC3Result>; leaveCurrentChannel: () => Promise<FDC3Result>; getCurrentChannel: () => Promise<FDC3Result>; };
}
121 changes: 32 additions & 89 deletions packages/preload/src/view/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { contextBridge } from 'electron';
import utils from '../../../main/src/utils';
import { Listener as fdc3Listener } from '@finos/fdc3';
import { Listener } from '@finos/fdc3';
import {
Context,
DisplayMetadata,
Expand Down Expand Up @@ -43,7 +43,7 @@ export function createAPI() {
/**
* the Listener class
*/
class Listener implements fdc3Listener {
class FDC3Listener implements Listener {
private id: string;

type: string;
Expand All @@ -56,29 +56,31 @@ export function createAPI() {
if (type === 'intent' && intent) {
this.intent = intent;
}
}

unsubscribe() {
if (this.type === 'context') {
_contextListeners.delete(this.id);
//notify the background script
document.dispatchEvent(
utils.fdc3Event(FDC3EventEnum.DropContextListener, { id: this.id }),
);
} else if (this.type === 'intent' && this.intent) {
const listeners = _intentListeners.get(this.intent);
if (listeners) {
listeners.delete(this.id);
this.unsubscribe = () => {
if (this.type === 'context') {
_contextListeners.delete(this.id);
//notify the background script
document.dispatchEvent(
utils.fdc3Event(FDC3EventEnum.DropContextListener, { id: this.id }),
);
} else if (this.type === 'intent' && this.intent) {
const listeners = _intentListeners.get(this.intent);
if (listeners) {
listeners.delete(this.id);
}
//notify the background script
document.dispatchEvent(
utils.fdc3Event(FDC3EventEnum.DropIntentListener, {
id: this.id,
intent: this.intent,
}),
);
}
//notify the background script
document.dispatchEvent(
utils.fdc3Event(FDC3EventEnum.DropIntentListener, {
id: this.id,
intent: this.intent,
}),
);
}
};
}

unsubscribe: () => void;
}

interface ListenerItem {
Expand Down Expand Up @@ -154,64 +156,13 @@ export function createAPI() {
contextType: thisContextType,
}),
);
return new Listener('context', listenerId);
return new FDC3Listener('context', listenerId);
},
};

return channel;
};

//type instanceStatus = 'ready' | 'loading' | 'unregistered';
/**
* the AppInstance class
*/
/*const createAppInstance = (
id: string,
status: instanceStatus,
): AppInstance => {
const instance: any = {};
instance.instanceId = id;
instance.status = status;
instance.addContextListener = (
contextType: string | ContextHandler,
handler?: ContextHandler,
) => {
const thisListener: ContextHandler = handler ? handler : (contextType as ContextHandler);
const thisContextType = handler ? (contextType as string) : undefined;
const listenerId: string = guid();
_contextListeners.set(
listenerId,
createListenerItem(listenerId, thisListener, thisContextType),
);
document.dispatchEvent(
utils.fdc3Event(FDC3EventEnum.AddContextListener, {
id: listenerId,
instanceId: instance.instanceId,
contextType: thisContextType,
}),
);
return new Listener('context', listenerId);
};
instance.broadcast = (context: Context) => {
wireMethod(
'broadcast',
{ context: context, instanceId: instance.instanceId },
true,
);
};
/* instance.onStatusChanged = (
handler: (newVal: string, oldVal: string) => {},
) => {};*/

/*
return instance as AppInstance;
};
*/

interface MethodConfig {
void?: boolean;
resultHandler?: { (result: FDC3Result): FDC3Result };
Expand Down Expand Up @@ -243,11 +194,14 @@ export function createAPI() {
resolve();
});
} else {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
document.addEventListener(
`FDC3:return_${eventId}`,
((event: FDC3Event) => {
let r: FDC3Result = event.detail;
if (r.error) {
reject(r.error);
}
if (r !== null && config && config.resultHandler) {
r = config.resultHandler.call(document, r);
}
Expand Down Expand Up @@ -314,7 +268,7 @@ export function createAPI() {
contextType: thisContextType,
}),
);
return new Listener('context', listenerId);
return new FDC3Listener('context', listenerId);
},

addIntentListener: (intent: string, listener: ContextHandler) => {
Expand All @@ -331,7 +285,7 @@ export function createAPI() {
intent: intent,
}),
);
return new Listener('intent', listenerId, intent);
return new FDC3Listener('intent', listenerId, intent);
} else {
console.error('listener could not be created');
return null;
Expand Down Expand Up @@ -388,18 +342,7 @@ export function createAPI() {
},

joinChannel: (channel: string) => {
return new Promise<void>((resolve) => {
document.addEventListener(
TOPICS.CONFIRM_JOIN,
(() => {
resolve();
}) as EventListener,
{ once: true },
);
document.dispatchEvent(
utils.fdc3Event(FDC3EventEnum.JoinChannel, { channel: channel }),
);
});
return wireMethod('joinChannel', { channel: channel });
},

leaveCurrentChannel: () => {
Expand Down
21 changes: 15 additions & 6 deletions packages/preload/src/view/contentConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,21 @@ const wireTopic = (topic: string, config?: TopicConfig): void => {
ts: e.ts,
listener: function (msg: FDC3Message) {
if (msg) {
document.dispatchEvent(
utils.fdc3Event(
`return_${eventId}`,
msg && msg.data ? msg.data : {},
),
);
//handle errors
if (msg.error) {
document.dispatchEvent(
utils.fdc3Event(`return_${eventId}`, {
error: msg.error,
}),
);
} else {
document.dispatchEvent(
utils.fdc3Event(
`return_${eventId}`,
msg && msg.data ? msg.data : {},
),
);
}
}
},
});
Expand Down

0 comments on commit 03d1870

Please sign in to comment.