Skip to content

Commit

Permalink
Update property nameconvention, renamed Fdc3ErrorResponse, remove unn…
Browse files Browse the repository at this point in the history
…ecessary variables, remove unnecessary async
  • Loading branch information
lilla28 committed Jun 27, 2023
1 parent 4b3a51b commit 0154548
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 61 deletions.
50 changes: 23 additions & 27 deletions src/shell/js/composeui-fdc3/src/ComposeUIDesktopAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@ import { ComposeUIChannel } from './infrastructure/ComposeUIChannel';
import { ChannelType } from './infrastructure/ChannelType';
import { ComposeUIListener } from './infrastructure/ComposeUIListener';
import { Fdc3FindChannelRequest } from './infrastructure/messages/Fdc3FindChannelRequest';
import { Fdc3ErrorResponse } from './infrastructure/messages/Fdc3ErrorResponse';
import { Fdc3FindChannelResponse } from './infrastructure/messages/Fdc3FindChannelResponse';
import { ComposeUITopic } from './infrastructure/ComposeUITopic';

//TODO sweep for non-standard errors
export class ComposeUIDesktopAgent implements DesktopAgent {
private appChannels: ComposeUIChannel[] = [];
private userChannels: ComposeUIChannel[] = [];
private privateChannels: ComposeUIChannel[] = [];
private currentChannel?: ComposeUIChannel;
private currentChannel?: ComposeUIChannel | null;
private messageRouterClient!: MessageRouter;
private currentChannelListeners: ComposeUIListener[] = [];

constructor(name: string, messageRouterClient: MessageRouter) {
this.messageRouterClient = messageRouterClient;
var channel = new ComposeUIChannel(
const channel = new ComposeUIChannel(
name,
"user",
this.messageRouterClient);
Expand Down Expand Up @@ -108,12 +107,10 @@ export class ComposeUIDesktopAgent implements DesktopAgent {
return;
}

const stringContextType = contextType as string ?? null;

const listener = <ComposeUIListener>await this.currentChannel!.addContextListener(stringContextType, handler!);
await this.currentChannel!.getCurrentContext(stringContextType)
const listener = <ComposeUIListener>await this.currentChannel!.addContextListener(contextType, handler!);
await this.currentChannel!.getCurrentContext(contextType)
.then(async (resultContext) => {
listener.LatestContext = this.currentChannel!.retrieveCurrentContext(stringContextType);
listener.LatestContext = this.currentChannel!.retrieveCurrentContext(contextType);
if (resultContext != listener.LatestContext) {
//TODO: integrationtest
await listener.handleContextMessage();
Expand Down Expand Up @@ -178,14 +175,14 @@ export class ComposeUIDesktopAgent implements DesktopAgent {
}

public getCurrentChannel(): Promise<Channel | null> {
return Promise.resolve(this.currentChannel!);
return Promise.resolve(this.currentChannel ?? null);
}

//TODO: add messageRouter message that we are leaving the current channel to notify the backend.
public leaveCurrentChannel(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.currentChannel = undefined;
this.currentChannelListeners.forEach(async listener => {
this.currentChannelListeners.forEach(listener => {
const isUnsubscribed = listener.unsubscribe();
if (!isUnsubscribed) {
reject(new Error(`Listener couldn't unsubscribe. IsSubscribed: ${isUnsubscribed}, Listener: ${listener}`));
Expand Down Expand Up @@ -218,7 +215,7 @@ export class ComposeUIDesktopAgent implements DesktopAgent {

//TODO
public getSystemChannels(): Promise<Channel[]> {
throw new Error("Not implemented");
return Promise.resolve(this.userChannels);
}

//TODO: Revisit for private channels
Expand Down Expand Up @@ -293,20 +290,19 @@ export class ComposeUIDesktopAgent implements DesktopAgent {

private async invokeChannelCreationMessage(topic: string, channelId: string, channelType: ChannelType): Promise<void> {
const message = JSON.stringify(new Fdc3FindChannelRequest(channelId, channelType));
await this.messageRouterClient.invoke(topic, message)
.then((response) => {
if(response) {
const message = JSON.parse(response) as TopicMessage;
if(message.payload) {
const fdc3Message = JSON.parse(message.payload) as Fdc3ErrorResponse;
if(fdc3Message.Error) {
throw new Error(fdc3Message.Error); //Type of the message should be created.
} else if (fdc3Message.Found){
this.currentChannel = new ComposeUIChannel(channelId, channelType, this.messageRouterClient);
this.addChannel(this.currentChannel);
}
}
}
});
const response = await this.messageRouterClient.invoke(topic, message);
if(response) {
const message = <TopicMessage>JSON.parse(response);
if(message.payload) {
const fdc3Message = <Fdc3FindChannelResponse>JSON.parse(message.payload);
if(fdc3Message.error) {
throw new Error(fdc3Message.error); //Type of the message should be created.
}
if (fdc3Message.found){
this.currentChannel = new ComposeUIChannel(channelId, channelType, this.messageRouterClient);
this.addChannel(this.currentChannel);
}
}
}
}
}
18 changes: 8 additions & 10 deletions src/shell/js/composeui-fdc3/src/Fdc3ComposeUI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { Channel, ChannelError, Context } from '@finos/fdc3';
import { Fdc3ContextMessage } from './infrastructure/messages/Fdc3ContextMessage';
import { Fdc3GetCurrentContextRequest } from './infrastructure/messages/Fdc3GetCurrentContextRequest';
import { Fdc3FindChannelRequest } from './infrastructure/messages/Fdc3FindChannelRequest';
import { Fdc3ErrorResponse } from './infrastructure/messages/Fdc3ErrorResponse';

const dummyContext = {type: "dummyContextType"};
const dummyChannelId = "dummyId";
Expand Down Expand Up @@ -69,9 +68,9 @@ describe('Tests for ComposeUIChannel implementation API', () => {
const resultContext = await testChannel.getCurrentContext();
expect(messageRouterClient.publish).toHaveBeenCalledTimes(1);
expect(messageRouterClient.invoke).toHaveBeenCalledTimes(1);
expect(messageRouterClient.invoke).toHaveBeenCalledWith(ComposeUITopic.getCurrentContext(dummyTopic, testChannel.type), JSON.stringify(new Fdc3GetCurrentContextRequest(null)));
expect(messageRouterClient.invoke).toHaveBeenCalledWith(ComposeUITopic.getCurrentContext(dummyTopic, testChannel.type), JSON.stringify(new Fdc3GetCurrentContextRequest(undefined)));
expect(messageRouterClient.publish).toHaveBeenCalledWith(ComposeUITopic.broadcast(), JSON.stringify(new Fdc3ContextMessage(dummyTopic, testInstrument)));
expect(resultContext).toMatchObject<Partial<Context>>({Id: dummyChannelId, Context: dummyContext});
expect(resultContext).toMatchObject({id : dummyChannelId, context: dummyContext});
});

it('getCurrentContext will result the lastContext', async() => {
Expand All @@ -90,9 +89,9 @@ describe('Tests for ComposeUIChannel implementation API', () => {
expect(messageRouterClient.publish).toBeCalledTimes(2);
expect(messageRouterClient.publish).toHaveBeenCalledWith(ComposeUITopic.broadcast(), JSON.stringify(new Fdc3ContextMessage(dummyTopic, testInstrument)));
expect(messageRouterClient.publish).toHaveBeenCalledWith(ComposeUITopic.broadcast(), JSON.stringify(new Fdc3ContextMessage(dummyTopic, testInstrument2)));
expect(messageRouterClient.invoke).toHaveBeenCalledWith(ComposeUITopic.getCurrentContext(dummyTopic, testChannel.type), JSON.stringify(new Fdc3GetCurrentContextRequest(null)));
expect(messageRouterClient.invoke).toHaveBeenCalledWith(ComposeUITopic.getCurrentContext(dummyTopic, testChannel.type), JSON.stringify(new Fdc3GetCurrentContextRequest(undefined)));
expect(messageRouterClient.invoke).toHaveBeenCalledWith(ComposeUITopic.getCurrentContext(dummyTopic, testChannel.type), JSON.stringify(new Fdc3GetCurrentContextRequest(testInstrument2.type)));
expect(resultContext).toMatchObject<Partial<Context>>({Id: dummyChannelId, Context: dummyContext});
expect(resultContext).toMatchObject({id : dummyChannelId, context: dummyContext});
expect(resultContextWithContextType).toMatchObject<Partial<Context>>(testInstrument2);
});

Expand Down Expand Up @@ -225,7 +224,6 @@ describe('Tests for ComposeUIDesktopAgent implementation API', () => {
const resultListener = await testDesktopAgent.addContextListener("fdc3.instrument", contextMessageHandlerMock);
expect(resultListener).toBeInstanceOf(ComposeUIListener);
expect(messageRouterClient.subscribe).toBeCalledTimes(1);
//expect(messageRouterClient.subscribe).toHaveBeenCalledWith({Id: "dummyPath", Context: {type: fdc3.instrument", id: { ticker: "AAPL"} }});
});

it('addContextListener will fail as per the current channel is not defined', async() => {
Expand Down Expand Up @@ -277,7 +275,7 @@ describe('Tests for ComposeUIDesktopAgent implementation API', () => {
unregisterEndpoint: jest.fn(() => { return Promise.resolve() }),
registerService: jest.fn(() => { return Promise.resolve() }),
unregisterService: jest.fn(() => { return Promise.resolve() }),
invoke: jest.fn(() => { return Promise.resolve(JSON.stringify({context: "", payload: `${JSON.stringify(new Fdc3ErrorResponse("Error happens..."))}` })) })
invoke: jest.fn(() => { return Promise.resolve(JSON.stringify({context: "", payload: `${JSON.stringify({error: "Error happens..."})}` })) })
};
const testDesktopAgent = new ComposeUIDesktopAgent("dummyPath", messageRouterClientMock);
await expect(testDesktopAgent.joinUserChannel("dummyPath2"))
Expand Down Expand Up @@ -305,7 +303,7 @@ describe('Tests for ComposeUIDesktopAgent implementation API', () => {
await testDesktopAgent.joinUserChannel("dummyPath");
await testDesktopAgent.leaveCurrentChannel();
var result = await testDesktopAgent.getCurrentChannel();
expect(result).toBe(undefined);
expect(result).toBeFalsy();
});

it('leaveCurrentChannel will trigger the current channel listeners to unsubscribe', async() => {
Expand All @@ -318,7 +316,7 @@ describe('Tests for ComposeUIDesktopAgent implementation API', () => {
.not
.toThrow();
var result = await testDesktopAgent.getCurrentChannel();
expect(result).toBe(undefined);
expect(result).toBeFalsy();
});

it('leaveCurrentChannel will fail as per the listener is already unsubscribed', async() => {
Expand All @@ -332,7 +330,7 @@ describe('Tests for ComposeUIDesktopAgent implementation API', () => {
.toThrow(new Error(`Listener couldn't unsubscribe. IsSubscribed: false, Listener: ${listener}`));

var result = await testDesktopAgent.getCurrentChannel();
expect(result).toBe(undefined);
expect(result).toBeFalsy();
});

it('getInfo will provide information of ComposeUI', async() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export class ComposeUIChannel implements Channel {
//TODO add error
public getCurrentContext(contextType?: string | undefined): Promise<Context | null> {
return new Promise<Context | null>(async (resolve, reject) => {
const message = JSON.stringify(new Fdc3GetCurrentContextRequest(contextType ?? null));
const message = JSON.stringify(new Fdc3GetCurrentContextRequest(contextType));
await this.messageRouterClient.invoke(ComposeUITopic.getCurrentContext(this.id, this.type), message)
.then((response) => {
if (response) {
const topicMessage = JSON.parse(response) as TopicMessage;
const topicMessage = <TopicMessage>JSON.parse(response);
if(topicMessage.payload) {
const context = JSON.parse(topicMessage.payload) as Context;
const context = <Context>JSON.parse(topicMessage.payload);
if(context) {
this.lastContext = context;
this.lastContexts.set(context.type, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export class ComposeUIListener implements Listener {
if(topicMessage.context.sourceId == this.messageRouterClient.clientId) return;
//TODO: integration test
const fdc3Message = new Fdc3ContextMessage(topicMessage.topic, JSON.parse(topicMessage.payload!));
if(this.channelId && ComposeUITopic.broadcast() == fdc3Message.Id
&& !this.contextType || this.contextType == fdc3Message.Context!.type) {
this.handler!(fdc3Message.Context!);
if(this.channelId && ComposeUITopic.broadcast() == fdc3Message.id
&& !this.contextType || this.contextType == fdc3Message.context!.type) {
this.handler!(fdc3Message.context!);
}
});
this.isSubscribed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import { Context } from "@finos/fdc3";

export class Fdc3ContextMessage {
public readonly Id!: string;
public readonly Context!: Context;
public readonly id!: string;
public readonly context!: Context;

constructor(id: string, context: Context) {
this.Id = id;
this.Context = context;
this.id = id;
this.context = context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import { ChannelType } from "../ChannelType";

export class Fdc3FindChannelRequest {
public readonly ChannelId: string;
public readonly ChannelType?: ChannelType;
public readonly channelId: string;
public readonly channelType?: ChannelType;

constructor(channelId: string, channelType?: ChannelType) {
this.ChannelId = channelId;
this.ChannelType = channelType;
this.channelId = channelId;
this.channelType = channelType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@
*
*/

export class Fdc3ErrorResponse {
public Error: string | undefined;
public Found: boolean;

constructor(error?: string | undefined, found: boolean = true) {
this.Error = error;
this.Found = found;
}
export interface Fdc3FindChannelResponse {
error: string | undefined;
found: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
*/

export class Fdc3GetCurrentContextRequest {
public readonly contextType?: string | null;
public readonly contextType?: string;

constructor(contextType?: string | null) {
constructor(contextType?: string) {
this.contextType = contextType;
}
}

0 comments on commit 0154548

Please sign in to comment.