Skip to content

Commit

Permalink
Improved type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Mar 18, 2024
1 parent bdc7b57 commit 6a1cf5e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/da-proxy/src/listeners/DefaultIntentListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class DefaultIntentListener extends AbstractListener<IntentHandler> {
super(messaging,
{ intent },
action,
"subscribeIntentListener", /** TODO : https://github.com/finos/FDC3/issues/1171 */
"unsubscribeIntentListener")
"onAddIntentListener",
"onUnsubscribeIntentListener")

this.intent = intent
}
Expand Down
9 changes: 5 additions & 4 deletions packages/da-server/src/handlers/BroadcastHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PrivateChannelBroadcastAgentRequest
} from "@finos/fdc3/dist/bridging/BridgingTypes";
import { ContextElement } from "@finos/fdc3";
import { OnAddContextListenerAgentRequest, OnUnsubscribeAgentRequest } from "fdc3-common";

type ListenerRegistration = {
appId: string,
Expand Down Expand Up @@ -53,8 +54,8 @@ export class BroadcastHandler implements MessageHandler {
case 'PrivateChannel.onUnsubscribe': return this.handleOnUnsubscribe(msg as PrivateChannelOnUnsubscribeAgentRequest, sc)

// although we don't have messages for these yet, we're going to need them. See: https://github.com/finos/FDC3/issues/1171
case 'onUnsubscribe': return this.handleOnUnsubscribe(msg as PrivateChannelOnUnsubscribeAgentRequest, sc)
case 'onAddContextListener': return this.handleOnAddContextListener(msg as PrivateChannelOnAddContextListenerAgentRequest, sc)
case 'onUnsubscribe': return this.handleOnUnsubscribe(msg as OnUnsubscribeAgentRequest, sc)
case 'onAddContextListener': return this.handleOnAddContextListener(msg as OnAddContextListenerAgentRequest, sc)
case 'broadcastRequest': return this.handleBroadcast(msg as BroadcastAgentRequest, sc)

// handling state synchronisation of channels
Expand Down Expand Up @@ -88,15 +89,15 @@ export class BroadcastHandler implements MessageHandler {
sc.post(out, from)
}

handleOnUnsubscribe(arg0: PrivateChannelOnUnsubscribeAgentRequest, _sc: ServerContext) {
handleOnUnsubscribe(arg0: PrivateChannelOnUnsubscribeAgentRequest | OnUnsubscribeAgentRequest, _sc: ServerContext) {
const lr = createListenerRegistration(arg0)
const fi = this.regs.findIndex((e) => matches(e, lr))
if (fi > -1) {
this.regs.splice(fi, 1)
}
}

handleOnAddContextListener(arg0: PrivateChannelOnAddContextListenerAgentRequest, _sc: ServerContext) {
handleOnAddContextListener(arg0: PrivateChannelOnAddContextListenerAgentRequest | OnAddContextListenerAgentRequest, _sc: ServerContext) {
const lr = createListenerRegistration(arg0)
this.regs.push(lr)
}
Expand Down
9 changes: 5 additions & 4 deletions packages/da-server/src/handlers/IntentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ServerContext } from "../ServerContext";
import { Directory } from "../directory/DirectoryInterface";
import { genericResultTypeSame } from "../directory/BasicDirectory";
import { ResolveError } from "@finos/fdc3";
import { OnAddIntentListenerAgentRequest, OnUnsubscribeIntentListenerAgentRequest } from "fdc3-common";


type ListenerRegistration = {
Expand Down Expand Up @@ -134,8 +135,8 @@ export class IntentHandler implements MessageHandler {
switch (msg.type as string) {
case 'findIntentRequest': return this.findIntentRequest(msg as FindIntentAgentRequest, sc, from)
case 'raiseIntentRequest': return this.raiseIntentRequest(msg as RaiseIntentAgentRequest, sc)
case 'onAddIntentListener': return this.onAddIntentListener(msg as any, sc)
case 'onUnsubscribe': return this.onUnsubscribe(msg as any, sc)
case 'onAddIntentListener': return this.onAddIntentListener(msg as OnAddIntentListenerAgentRequest, sc)
case 'onUnsubscribeIntentListener': return this.onUnsubscribe(msg as OnUnsubscribeIntentListenerAgentRequest, sc)
case 'raiseIntentResponse': return this.raiseIntentResponse(msg as RaiseIntentAgentResponse, sc)
case 'raiseIntentResultResponse': return this.raiseIntentResultResponse(msg as RaiseIntentResultAgentResponse, sc)
}
Expand Down Expand Up @@ -169,15 +170,15 @@ export class IntentHandler implements MessageHandler {
}
}

onUnsubscribe(arg0: any, _sc: ServerContext): void {
onUnsubscribe(arg0: OnUnsubscribeIntentListenerAgentRequest, _sc: ServerContext): void {
const lr = createListenerRegistration(arg0)
const fi = this.regs.findIndex((e) => matches(e, lr))
if (fi > -1) {
this.regs.splice(fi, 1)
}
}

onAddIntentListener(arg0: any, _sc: ServerContext): void {
onAddIntentListener(arg0: OnAddIntentListenerAgentRequest, _sc: ServerContext): void {
const lr = createListenerRegistration(arg0)
this.regs.push(lr)

Expand Down
7 changes: 3 additions & 4 deletions packages/da-server/src/handlers/OpenHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MessageHandler } from "../BasicFDC3Server";
import { ServerContext } from "../ServerContext";
import { Directory, DirectoryApp } from "../directory/DirectoryInterface";
import { ContextElement, ResolveError } from "@finos/fdc3";
import { OnAddContextListenerAgentRequest } from "fdc3-common";

function filterPublicDetails(appD: DirectoryApp): GetAppMetadataAgentResponsePayload['appMetadata'] {
return {
Expand Down Expand Up @@ -46,13 +47,11 @@ export class OpenHandler implements MessageHandler {
case 'openRequest': return this.open(msg as OpenAgentRequest, sc, from)
case 'findInstancesRequest': return this.findInstances(msg as FindInstancesAgentRequest, sc, from)
case 'getAppMetadataRequest': return this.getAppMetadata(msg as GetAppMetadataAgentRequest, sc, from)

// although we don't have messages for these yet, we're going to need them. See: https://github.com/finos/FDC3/issues/1171
case 'onAddContextListener': return this.handleOnAddContextListener(msg as PrivateChannelOnAddContextListenerAgentRequest, sc)
case 'onAddContextListener': return this.handleOnAddContextListener(msg as OnAddContextListenerAgentRequest, sc)
}
}

handleOnAddContextListener(arg0: PrivateChannelOnAddContextListenerAgentRequest, sc: ServerContext) {
handleOnAddContextListener(arg0: PrivateChannelOnAddContextListenerAgentRequest | OnAddContextListenerAgentRequest, sc: ServerContext) {
const instanceId = arg0.meta.source?.instanceId
const pendingContext = instanceId ? this.pendingContexts.get(instanceId) : undefined

Expand Down
5 changes: 3 additions & 2 deletions packages/da-server/test/step-definitions/broadcast.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { When } from '@cucumber/cucumber'
import { CustomWorld } from '../world';
import { BroadcastAgentRequest } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { contextMap, createMeta } from './generic.steps';
import { OnAddContextListenerAgentRequest, OnUnsubscribeAgentRequest } from 'fdc3-common';

When('{string} adds a context listener on {string} with type {string}', function (this: CustomWorld, app: string, channelId: string, contextType: string) {
const meta = createMeta(this, app)
Expand All @@ -12,7 +13,7 @@ When('{string} adds a context listener on {string} with type {string}', function
contextType
},
type: 'onAddContextListener'
}
} as OnAddContextListenerAgentRequest

this.server.receive(message, meta.source)
})
Expand All @@ -26,7 +27,7 @@ When('{string} removes context listener on {string} with type {string}', functio
contextType
},
type: 'onUnsubscribe'
}
} as OnUnsubscribeAgentRequest

this.server.receive(message, meta.source)
})
Expand Down
7 changes: 4 additions & 3 deletions packages/da-server/test/step-definitions/intents.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { APP_FIELD } from "./generic.steps";
import { FindIntentAgentRequest, RaiseIntentAgentRequest, RaiseIntentAgentResponse, RaiseIntentResultAgentResponse } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { handleResolve } from "../support/matching";
import { createMeta, contextMap } from './generic.steps';
import { OnAddIntentListenerAgentRequest, OnUnsubscribeIntentListenerAgentRequest } from "fdc3-common";

type ListensFor = {
[key: string]: {
Expand Down Expand Up @@ -75,22 +76,22 @@ Given('{string} registers an intent listener for {string} with contextType {stri
contextType: handleResolve(contextType, this),
resultType: handleResolve(resultType, this),
}
}
} as OnAddIntentListenerAgentRequest
this.server.receive(message, meta.source)
});


Given('{string} unsubscribes an intent listener for {string} with contextType {string} and result type {string}', function (this: CustomWorld, appStr: string, intentName: string, contextType: string, resultType: string) {
const meta = createMeta(this, appStr)
const message = {
type: 'onUnsubscribe',
type: 'onUnsubscribeIntentListener',
meta,
payload: {
intentName: handleResolve(intentName, this),
contextType: handleResolve(contextType, this),
resultType: handleResolve(resultType, this),
}
}
} as OnUnsubscribeIntentListenerAgentRequest
this.server.receive(message, meta.source)
});

Expand Down
30 changes: 30 additions & 0 deletions packages/fdc3-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AppIdentifier, DesktopAgent } from "@finos/fdc3";
import { exchange, exchangePostMessage, exchangeForMessagePort } from "./exchange";
import { PrivateChannelOnAddContextListenerAgentRequest, PrivateChannelOnAddContextListenerAgentRequestMeta, PrivateChannelOnUnsubscribeAgentRequest } from "@finos/fdc3/dist/bridging/BridgingTypes";

/**
* We need to add options here.
Expand Down Expand Up @@ -68,3 +69,32 @@ export const FDC3_USER_CHANNELS_REQUEST_TYPE = 'FDC3-User-Channels-Request';
export const FDC3_USER_CHANNELS_RESPONSE_TYPE = 'FDC3-User-Channels-Response';
export const FDC3_PORT_TRANSFER_REQUEST_TYPE = 'FDC3-Port-Transfer-Request';
export const FDC3_PORT_TRANSFER_RESPONSE_TYPE = 'FDC3-Port-Transfer-Response';

/** Message Types Not Defined By Bridging, But Needed */
export type OnAddContextListenerAgentRequest = PrivateChannelOnAddContextListenerAgentRequest & {
type: "onAddContextListener"
}

export type OnUnsubscribeAgentRequest = PrivateChannelOnUnsubscribeAgentRequest & {
type: "onUnsubscribe"
}

export type OnAddIntentListenerAgentRequest = {
type: 'onAddIntentListener',
meta: PrivateChannelOnAddContextListenerAgentRequestMeta,
payload: {
intentName: string,
contextType: string | undefined,
resultType: string | undefined,
}
}

export type OnUnsubscribeIntentListenerAgentRequest = {
type: 'onUnsubscribeIntentListener',
meta: PrivateChannelOnAddContextListenerAgentRequestMeta,
payload: {
intentName: string,
contextType: string | undefined,
resultType: string | undefined,
}
}

0 comments on commit 6a1cf5e

Please sign in to comment.