Skip to content

Commit 858db41

Browse files
C2-25477: OpenFin - setContext in session context group (#2315)
In the OpenFin world, broadcasting a context in a session context group is different from broadcasting in a default context group. The default context group way: ``` js await openfin.joinContextGroup(id); await openfin.setContext(context) ``` The session context group way: ``` const group = await openfin.joinSessionContextGroup(id); await group.setContext(context) ```
1 parent 8d8ec30 commit 858db41

5 files changed

+99
-5
lines changed

spec/mainApiHandler.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jest.mock('../src/app/openfin-handler', () => {
2020
openfinHandler: {
2121
connect: jest.fn(),
2222
fireIntent: jest.fn(),
23+
setContext: jest.fn(),
2324
joinContextGroup: jest.fn(),
2425
joinSessionContextGroup: jest.fn(),
2526
getContextGroups: jest.fn(),
@@ -749,5 +750,30 @@ describe('main api handler', () => {
749750

750751
expect(spy).toHaveBeenCalledTimes(1);
751752
});
753+
754+
it('should call `setContext`', () => {
755+
const spy = jest.spyOn(openfinHandler, 'setContext');
756+
const value = {
757+
cmd: apiCmds.openfinSetContext,
758+
context: 'context',
759+
};
760+
761+
ipcMain.send(apiName.symphonyApi, value);
762+
763+
expect(spy).toHaveBeenCalledWith('context', undefined);
764+
});
765+
766+
it('should call `setContext` with session group id', () => {
767+
const spy = jest.spyOn(openfinHandler, 'setContext');
768+
const value = {
769+
cmd: apiCmds.openfinSetContext,
770+
context: 'context',
771+
sessionContextGroupId: 'session-context-group-id',
772+
};
773+
774+
ipcMain.send(apiName.symphonyApi, value);
775+
776+
expect(spy).toHaveBeenCalledWith('context', 'session-context-group-id');
777+
});
752778
});
753779
});

spec/openfinHandler.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jest.mock('@openfin/node-adapter', () => ({
1010
connectSync: jest.fn().mockReturnValue({
1111
onDisconnection: jest.fn(),
1212
fireIntent: jest.fn(),
13+
setContext: jest.fn(),
1314
registerIntentHandler: jest.fn(),
1415
getAllClientsInContextGroup: jest.fn(),
1516
joinContextGroup: jest.fn(),
@@ -253,6 +254,52 @@ describe('Openfin', () => {
253254
expect(fireIntentForContextSpy).toHaveBeenCalledTimes(1);
254255
});
255256

257+
it('should set context in current context group', async () => {
258+
const connectSyncMock = await connectMock.Interop.connectSync();
259+
const setContextSpy = jest.spyOn(connectSyncMock, 'setContext');
260+
261+
await openfinHandler.connect();
262+
const context = {
263+
type: 'fdc3.contact',
264+
name: 'Andy Young',
265+
id: {
266+
267+
},
268+
};
269+
await openfinHandler.setContext(context);
270+
271+
expect(setContextSpy).toHaveBeenCalledTimes(1);
272+
});
273+
274+
it('should set context in the provided session context group', async () => {
275+
const connectSyncMock = await connectMock.Interop.connectSync();
276+
const setContextSpy = jest.spyOn(connectSyncMock, 'setContext');
277+
278+
const sessionContextGroupId = 'sessionContextGroupId';
279+
const sessionContextGroup = {
280+
id: sessionContextGroupId,
281+
setContext: jest.fn(),
282+
};
283+
jest
284+
.spyOn(connectSyncMock, 'joinSessionContextGroup')
285+
.mockResolvedValue(sessionContextGroup);
286+
287+
await openfinHandler.connect();
288+
const context = {
289+
type: 'fdc3.contact',
290+
name: 'Andy Young',
291+
id: {
292+
293+
},
294+
};
295+
296+
await openfinHandler.joinSessionContextGroup(sessionContextGroupId);
297+
await openfinHandler.setContext(context, sessionContextGroupId);
298+
299+
expect(setContextSpy).not.toHaveBeenCalled();
300+
expect(sessionContextGroup.setContext).toHaveBeenCalledTimes(1);
301+
});
302+
256303
it('should remove from context group', async () => {
257304
const connectSyncMock = await connectMock.Interop.connectSync();
258305
const removeFromContextGroupSpy = jest.spyOn(

src/app/main-api-handler.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,10 @@ ipcMain.handle(
655655
case apiCmds.openfinRemoveFromContextGroup:
656656
return openfinHandler.removeFromContextGroup();
657657
case apiCmds.openfinSetContext:
658-
return openfinHandler.setContext(arg.context);
658+
return openfinHandler.setContext(
659+
arg.context,
660+
arg.sessionContextGroupId,
661+
);
659662
default:
660663
break;
661664
}

src/app/openfin-handler.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const TIMEOUT_THRESHOLD = 10000;
1212
export class OpenfinHandler {
1313
private interopClient: OpenFin.InteropClient | undefined;
1414
private intentHandlerSubscriptions: Map<UUID, any> = new Map();
15+
private sessionContextGroups: Map<string, OpenFin.SessionContextGroup> =
16+
new Map();
1517
private isConnected: boolean = false;
1618
private fin: NodeFin | undefined;
1719

@@ -135,7 +137,13 @@ export class OpenfinHandler {
135137
public async joinSessionContextGroup(contextGroupId: string) {
136138
return this.interopClient
137139
?.joinSessionContextGroup(contextGroupId)
138-
.then(({ id }) => id);
140+
.then((sessionContextGroup) => {
141+
this.sessionContextGroups.set(
142+
sessionContextGroup.id,
143+
sessionContextGroup,
144+
);
145+
return sessionContextGroup.id;
146+
});
139147
}
140148

141149
/**
@@ -169,6 +177,8 @@ export class OpenfinHandler {
169177
}
170178
});
171179
this.intentHandlerSubscriptions.clear();
180+
181+
this.sessionContextGroups.clear();
172182
}
173183

174184
/**
@@ -205,10 +215,17 @@ export class OpenfinHandler {
205215
}
206216

207217
/**
208-
* Sets a context for the context group of the current entity.
218+
* Sets a context for the current context group.
219+
* If no session context group is specified, the context is set for the current context group (joined through joinContextGroup).
220+
* If a session context group is specified, the context is set for the given session context group (joined through joinSessionContextGroup).
209221
* @param context
210222
*/
211-
public setContext(context: any) {
223+
public setContext(context: any, sessionContextGroupId?: string) {
224+
if (sessionContextGroupId) {
225+
return this.sessionContextGroups
226+
.get(sessionContextGroupId)
227+
?.setContext(context);
228+
}
212229
return this.interopClient?.setContext(context);
213230
}
214231

src/renderer/ssf-api.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1131,10 +1131,11 @@ export class SSFApi {
11311131
* Sets a context for the context group of the current entity.
11321132
* @param context
11331133
*/
1134-
public async openfinSetContext(context: any) {
1134+
public async openfinSetContext(context: any, sessionContextGroupId?: string) {
11351135
const response = await local.ipcRenderer.invoke(apiName.symphonyApi, {
11361136
cmd: apiCmds.openfinSetContext,
11371137
context,
1138+
sessionContextGroupId,
11381139
});
11391140
return response;
11401141
}

0 commit comments

Comments
 (0)