Skip to content

Commit

Permalink
SDA-4657: Patch for OpenFin (#2271)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinerollindev authored Jan 23, 2025
1 parent 72be5a4 commit 2a85238
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 61 deletions.
21 changes: 17 additions & 4 deletions spec/mainApiHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ jest.mock('../src/app/openfin-handler', () => {
connect: jest.fn(),
fireIntent: jest.fn(),
joinContextGroup: jest.fn(),
joinSessionContextGroup: jest.fn(),
getContextGroups: jest.fn(),
getConnectionStatus: jest.fn(),
getInfo: jest.fn(),
getAllClientsInContextGroup: jest.fn(),
registerIntentHandler: jest.fn(),
unregisterIntentHandler: jest.fn(),
fireIntentForContext: jest.fn(),
removeClientFromContextGroup: jest.fn(),
removeFromContextGroup: jest.fn(),
},
};
});
Expand Down Expand Up @@ -726,10 +727,22 @@ describe('main api handler', () => {
expect(spy).toHaveBeenCalledTimes(1);
});

it('should call `removeClientFromContextGroup`', () => {
const spy = jest.spyOn(openfinHandler, 'removeClientFromContextGroup');
it('should call `removeFromContextGroup`', () => {
const spy = jest.spyOn(openfinHandler, 'removeFromContextGroup');
const value = {
cmd: apiCmds.openfinRemoveClientFromContextGroup,
cmd: apiCmds.openfinRemoveFromContextGroup,
};

ipcMain.send(apiName.symphonyApi, value);

expect(spy).toHaveBeenCalledTimes(1);
});

it('should call `joinSessionContextGroup`', () => {
const spy = jest.spyOn(openfinHandler, 'joinSessionContextGroup');
const value = {
cmd: apiCmds.openfinJoinSessionContextGroup,
contextGroupId: 'group-id',
};

ipcMain.send(apiName.symphonyApi, value);
Expand Down
33 changes: 25 additions & 8 deletions spec/openfinHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ jest.mock('@openfin/node-adapter', () => ({
registerIntentHandler: jest.fn(),
getAllClientsInContextGroup: jest.fn(),
joinContextGroup: jest.fn(),
joinSessionContextGroup: jest.fn(),
getContextGroups: jest.fn(),
fireIntentForContext: jest.fn(),
removeClientFromContextGroup: jest.fn(),
removeFromContextGroup: jest.fn(),
}),
},
});
Expand Down Expand Up @@ -185,6 +186,19 @@ describe('Openfin', () => {
expect(joinContextGroupSpy).toHaveBeenCalledTimes(1);
});

it('should join a session context group', async () => {
const connectSyncMock = await connectMock.Interop.connectSync();
const joinSessionContextGroupSpy = jest.spyOn(
connectSyncMock,
'joinSessionContextGroup',
);

await openfinHandler.connect();
await openfinHandler.joinSessionContextGroup('contextGroupId');

expect(joinSessionContextGroupSpy).toHaveBeenCalledTimes(1);
});

it('should return all context groups', async () => {
const connectSyncMock = await connectMock.Interop.connectSync();
const getContextGroupsSpy = jest.spyOn(connectSyncMock, 'getContextGroups');
Expand All @@ -210,7 +224,10 @@ describe('Openfin', () => {

it('should fire an intent for a given context', async () => {
const connectSyncMock = await connectMock.Interop.connectSync();
const fireIntentSpy = jest.spyOn(connectSyncMock, 'fireIntentForContext');
const fireIntentForContextSpy = jest.spyOn(
connectSyncMock,
'fireIntentForContext',
);

await openfinHandler.connect();
const context = {
Expand All @@ -222,18 +239,18 @@ describe('Openfin', () => {
};
await openfinHandler.fireIntentForContext(context);

expect(fireIntentSpy).toHaveBeenCalledTimes(1);
expect(fireIntentForContextSpy).toHaveBeenCalledTimes(1);
});

it('should remove client from context group', async () => {
it('should remove from context group', async () => {
const connectSyncMock = await connectMock.Interop.connectSync();
const fireIntentSpy = jest.spyOn(
const removeFromContextGroupSpy = jest.spyOn(
connectSyncMock,
'removeClientFromContextGroup',
'removeFromContextGroup',
);

await openfinHandler.removeClientFromContextGroup();
await openfinHandler.removeFromContextGroup();

expect(fireIntentSpy).toHaveBeenCalledTimes(1);
expect(removeFromContextGroupSpy).toHaveBeenCalledTimes(1);
});
});
27 changes: 12 additions & 15 deletions src/app/main-api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,21 +560,6 @@ ipcMain.on(

helpMenu.setValue(helpCenter);
break;
case apiCmds.openfinFireIntent:
openfinHandler.fireIntent(arg.intent);
break;
case apiCmds.openfinJoinContextGroup:
openfinHandler.joinContextGroup(arg.contextGroupId, arg.target);
break;
case apiCmds.openfinUnregisterIntentHandler:
openfinHandler.unregisterIntentHandler(arg.intentName);
break;
case apiCmds.openfinFireIntentForContext:
openfinHandler.fireIntentForContext(arg.context);
break;
case apiCmds.openfinRemoveClientFromContextGroup:
openfinHandler.removeClientFromContextGroup();
break;
default:
break;
}
Expand Down Expand Up @@ -657,6 +642,18 @@ ipcMain.handle(
return openfinHandler.getAllClientsInContextGroup(arg.contextGroupId);
case apiCmds.openfinGetClientInfo:
return openfinHandler.getClientInfo();
case apiCmds.openfinFireIntent:
return openfinHandler.fireIntent(arg.intent);
case apiCmds.openfinJoinContextGroup:
return openfinHandler.joinContextGroup(arg.contextGroupId, arg.target);
case apiCmds.openfinJoinSessionContextGroup:
return openfinHandler.joinSessionContextGroup(arg.contextGroupId);
case apiCmds.openfinUnregisterIntentHandler:
return openfinHandler.unregisterIntentHandler(arg.intentName);
case apiCmds.openfinFireIntentForContext:
return openfinHandler.fireIntentForContext(arg.context);
case apiCmds.openfinRemoveFromContextGroup:
return openfinHandler.removeFromContextGroup();
default:
break;
}
Expand Down
42 changes: 26 additions & 16 deletions src/app/openfin-handler.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/// <reference types="@openfin/core/fin" />

import { connect } from '@openfin/node-adapter';
import { randomUUID, UUID } from 'crypto';
import { logger } from '../common/openfin-logger';
import { config, IConfig } from './config-handler';
import { windowHandler } from './window-handler';

const OPENFIN_PROVIDER = 'Openfin';
const OPENFIN_PROVIDER = 'OpenFin';
const TIMEOUT_THRESHOLD = 10000;

export class OpenfinHandler {
private interopClient;
private interopClient: OpenFin.InteropClient | undefined;
private intentHandlerSubscriptions: Map<UUID, any> = new Map();
private isConnected: boolean = false;
private fin: any;

/**
* Connection to interop brocker
* Connection to interop broker
*/
public async connect() {
const { openfin }: IConfig = config.getConfigFields(['openfin']);
Expand All @@ -30,7 +32,8 @@ export class OpenfinHandler {
const connectionTimeoutPromise = new Promise((_, reject) =>
setTimeout(() => {
logger.error(
`openfin-handler: Connection timeout after ${timeoutValue / 1000
`openfin-handler: Connection timeout after ${
timeoutValue / 1000
} seconds`,
);
return reject(
Expand Down Expand Up @@ -61,7 +64,7 @@ export class OpenfinHandler {
this.interopClient = this.fin.Interop.connectSync(openfin.channelName);
this.isConnected = true;

this.interopClient.onDisconnection((event) => {
this.interopClient?.onDisconnection((event) => {
const { brokerName } = event;
logger.warn(
`openfin-handler: Disconnected from Interop Broker ${brokerName}`,
Expand Down Expand Up @@ -95,15 +98,15 @@ export class OpenfinHandler {
* Sends an intent to the Interop Broker
*/
public fireIntent(intent) {
this.interopClient.fireIntent(intent);
return this.interopClient?.fireIntent(intent);
}

/**
* Adds an intent handler for incoming intents
*/
public async registerIntentHandler(intentName: string): Promise<UUID> {
const unsubscriptionCallback =
await this.interopClient.registerIntentHandler(
await this.interopClient?.registerIntentHandler(
this.intentHandler,
intentName,
);
Expand All @@ -125,21 +128,28 @@ export class OpenfinHandler {
* Join all Interop Clients at the given identity to context group contextGroupId. If no target is specified, it adds the sender to the context group.
*/
public async joinContextGroup(contextGroupId: string, target?: any) {
await this.interopClient.joinContextGroup(contextGroupId, target);
return this.interopClient?.joinContextGroup(contextGroupId, target);
}

/**
* Joins or create a context group that does not persist between runs and aren't present on snapshots.
*/
public async joinSessionContextGroup(contextGroupId: string) {
return this.interopClient?.joinSessionContextGroup(contextGroupId);
}

/**
* Returns the Interop-Broker-defined context groups available for an entity to join.
*/
public async getContextGroups() {
return this.interopClient.getContextGroups();
return this.interopClient?.getContextGroups();
}

/**
* Gets all clients for a context group.
*/
public getAllClientsInContextGroup(contextGroupId: string) {
return this.interopClient.getAllClientsInContextGroup(contextGroupId);
public async getAllClientsInContextGroup(contextGroupId: string) {
return this.interopClient?.getAllClientsInContextGroup(contextGroupId);
}

/**
Expand Down Expand Up @@ -178,7 +188,7 @@ export class OpenfinHandler {
public getInfo() {
return {
provider: OPENFIN_PROVIDER,
fdc3Version: '',
fdc3Version: '2.0',
optionalFeatures: {
OriginatingAppMetadata: false,
UserChannelMembershipAPIs: false,
Expand All @@ -193,14 +203,14 @@ export class OpenfinHandler {
* @param context
*/
public fireIntentForContext(context: any) {
this.interopClient.fireIntentForContext(context);
return this.interopClient?.fireIntentForContext(context);
}

/**
* Removes a client from current context group
* Leaves current context group
*/
public removeClientFromContextGroup() {
this.interopClient.removeClientFromContextGroup();
public removeFromContextGroup() {
return this.interopClient?.removeFromContextGroup();
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/common/api-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ export enum apiCmds {
openfinGetConnectionStatus = 'openfin-get-connection-status',
openfinGetInfo = 'openfin-get-info',
openfinJoinContextGroup = 'openfin-join-context-group',
openfinJoinSessionContextGroup = 'openfin-join-session-context-group',
openfinGetContextGroups = 'openfin-get-context-groups',
openfinGetAllClientsInContextGroup = 'openfin-get-all-clients-in-context-group',
openfinFireIntentForContext = 'openfin-fire-intent-for-context',
openfinRemoveClientFromContextGroup = 'openfin-remove-client-from-context-group',
openfinRemoveFromContextGroup = 'openfin-remove-from-context-group',
openfinGetClientInfo = 'openfin-get-client-info',
}

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/preload-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ if (ssfWindow.ssf) {
unregisterIntentHandler: ssfWindow.ssf.openfinUnregisterIntentHandler,
getContextGroups: ssfWindow.ssf.openfinGetContextGroups,
joinContextGroup: ssfWindow.ssf.openfinJoinContextGroup,
joinSessionContextGroup: ssfWindow.ssf.openfinJoinSessionContextGroup,
getAllClientsInContextGroup:
ssfWindow.ssf.openfinGetAllClientsInContextGroup,
fireIntentForContext: ssfWindow.ssf.openfinFireIntentForContext,
removeClientFromContextGroup:
ssfWindow.ssf.openfinRemoveClientFromContextGroup,
removeFromContextGroup: ssfWindow.ssf.openfinRemoveFromContextGroup,
getClientInfo: ssfWindow.ssf.openfinGetClientInfo,
});
}
Expand Down
Loading

0 comments on commit 2a85238

Please sign in to comment.