Skip to content

Commit

Permalink
Re-added Private Channel code
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Sep 1, 2023
1 parent 550300e commit f9cae87
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 21 deletions.
28 changes: 14 additions & 14 deletions packages/preload/src/fdc3-2.0/channel.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Channel as Channel1_2, PrivateChannel } from 'fdc3-2.0';
import { Channel as Channel2_0 } from 'fdc3-2.0';
import { Channel as Channel2_0, PrivateChannel } from 'fdc3-2.0';
import { createChannelObject as createChannelObject1_2 } from '../fdc3-1.2/channel';

import { Context, DisplayMetadata } from '/@main/types/FDC3Message';
import { guid } from '../lib/lib';
import { SendMessage } from '../message';
import { FDC3_2_0_TOPICS } from '/@main/handlers/fdc3/2.0/topics';
import { FDC3Listener, SailContextHandler, contextListeners, createListenerItem } from '../fdc3-1.2/listeners';
import { FDC3Listener, SailContextHandler, SailListener, contextListeners, createListenerItem } from '../fdc3-1.2/listeners';
import { FDC3_TOPICS } from '/@main/handlers/fdc3/topics';
import { CreationFailed } from '/@main/types/FDC3Errors';
import { AddContextListener, DisconnectListener, UnsubscribeListener, addContextListeners, createContextTypeListenerItem, createVoidListenerItem, disconnectListeners, unsubscribeListeners } from './listeners';

Check failure on line 11 in packages/preload/src/fdc3-2.0/channel.ts

View workflow job for this annotation

GitHub Actions / eslint

'SailListener' is defined but never used


/**
* This overrides the one from the 1.2 implementation as the addContextListener returns a promise in this version
Expand Down Expand Up @@ -73,12 +74,10 @@ export const createPrivateChannelObject = (sendMessage: SendMessage, id: string)
return {
...privateChannel,

onAddContextListener: (
handler: (contextType?: string) => void,
): FDC3Listener => {
onAddContextListener(handler: (contextType?: string) => void) {
const listenerId: string = guid();

_addContextListeners.set(
addContextListeners.set(
listenerId,
createContextTypeListenerItem(listenerId, handler),
);
Expand All @@ -87,13 +86,14 @@ export const createPrivateChannelObject = (sendMessage: SendMessage, id: string)
listenerId: listenerId,
channel: id,
});
return new AddContextListener(listenerId);

return new AddContextListener(sendMessage, listenerId);
},

onDisconnect: (handler: () => void) => {
onDisconnect(handler: () => void) {
const listenerId: string = guid();

_disconnectListeners.set(
disconnectListeners.set(
listenerId,
createVoidListenerItem(listenerId, handler),
);
Expand All @@ -102,13 +102,13 @@ export const createPrivateChannelObject = (sendMessage: SendMessage, id: string)
listenerId: listenerId,
channel: id,
});
return new DisconnectListener(listenerId);
return new DisconnectListener(sendMessage, listenerId);
},

onUnsubscribe: (handler: (contextType?: string) => void): Listener => {
onUnsubscribe(handler: (contextType?: string) => void) {
const listenerId: string = guid();

_unsubscribeListeners.set(
unsubscribeListeners.set(
listenerId,
createContextTypeListenerItem(listenerId, handler),
);
Expand All @@ -117,7 +117,7 @@ export const createPrivateChannelObject = (sendMessage: SendMessage, id: string)
listenerId: listenerId,
channel: id,
});
return new UnsubscribeListener(listenerId);
return new UnsubscribeListener(sendMessage, listenerId);
},

disconnect: (): void => {
Expand Down
21 changes: 14 additions & 7 deletions packages/preload/src/fdc3-2.0/desktop-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ function convertAppIntent(sai: SailAppIntent) : AppIntent {

export function createDesktopAgentInstance(sendMessage: SendMessage, version: string, base: DesktopAgent1_2, appMetadata: AppMetadata): DesktopAgent {

const getOrCreateChannel1_2 = base.getOrCreateChannel;
const raiseIntentForContext1_2 = base.raiseIntentForContext;
const addIntentListener1_2 = base.addIntentListener;
const addContextListener1_2 = base.addContextListener;

Expand Down Expand Up @@ -89,6 +87,20 @@ export function createDesktopAgentInstance(sendMessage: SendMessage, version: st
return this.getUserChannels()
},

async getOrCreateChannel(channelId: string) {
const result: ChannelData = await sendMessage(
FDC3_2_0_TOPICS.GET_OR_CREATE_CHANNEL,
{ channel: channelId },
);

return createChannelObject(
sendMessage,
result.id,
result.type,
result.displayMetadata || { name: result.id },
);
},

async createPrivateChannel() {
const result: ChannelData = await sendMessage(
FDC3_2_0_TOPICS.CREATE_PRIVATE_CHANNEL,
Expand Down Expand Up @@ -124,11 +136,6 @@ export function createDesktopAgentInstance(sendMessage: SendMessage, version: st
);
},

async getOrCreateChannel(channelId) {
// very slight narrowing of type, otherwise identical
return getOrCreateChannel1_2(channelId) as unknown as Channel
},

async getAppMetadata(app: AppIdentifier) {
const result: AppMetadata = await sendMessage(FDC3_2_0_TOPICS.GET_APP_METADATA, {
app: app
Expand Down
119 changes: 119 additions & 0 deletions packages/preload/src/fdc3-2.0/listeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { IntentHandler } from "fdc3-2.0";
import { ListenerItem, SailListener } from "../fdc3-1.2/listeners";
import { SendMessage } from "../message";
import { FDC3_2_0_TOPICS } from "/@main/handlers/fdc3/2.0/topics";

/**
* Listeners for Private Channel methods. onDisconnect, onUnsubscribe, onAddContextListener
*/

export class AddContextListener implements SailListener {
private id: string;

constructor(sendMessage: SendMessage, listenerId: string) {
this.id = listenerId;

this.unsubscribe = () => {
addContextListeners.delete(this.id);

sendMessage(FDC3_2_0_TOPICS.DROP_ONADDCONTEXT_LISTENER, {
listenerId: this.id,
});
};
}

unsubscribe: () => void;
}

export class UnsubscribeListener implements SailListener {
private id: string;

constructor(sendMessage: SendMessage, listenerId: string) {
this.id = listenerId;

this.unsubscribe = () => {
unsubscribeListeners.delete(this.id);

sendMessage(FDC3_2_0_TOPICS.DROP_ONUNSUBSCRIBE_LISTENER, {
listenerId: this.id,
});
};
}

unsubscribe: () => void;
}

export class DisconnectListener implements SailListener {
private id: string;

constructor(sendMessage: SendMessage, listenerId: string) {
this.id = listenerId;

this.unsubscribe = () => {
disconnectListeners.delete(this.id);

sendMessage(FDC3_2_0_TOPICS.DROP_ONDISCONNECT_LISTENER, {
listenerId: this.id,
});
};
}

unsubscribe: () => void;
}

// listener for async intent handlers (2.0)
export interface IntentListenerItem {
id?: string;
handler?: IntentHandler;
contextType?: string;
}

//listener that takes handler with ContextType arg only
export interface ContextTypeListenerItem {
id?: string;
handler?: (contextType: string) => void;
contextType?: string;
}

//listener with handler that has no args
export interface VoidListenerItem {
id?: string;
handler?: () => void;
contextType?: string;
}


//map of listeners for when a contextListener is added to a private channel
export const addContextListeners: Map<string, ContextTypeListenerItem> = new Map();

//map of listeners for unsubscribing on a private channel
export const unsubscribeListeners: Map<string, ContextTypeListenerItem> = new Map();

//map of listeners for disconnecting on a private channel
export const disconnectListeners: Map<string, ListenerItem> = new Map();

export const createContextTypeListenerItem = (
id: string,
handler: (contextType: string) => void,
contextType?: string,
): ContextTypeListenerItem => {
const listener = {
id,
handler,
contextType
};
return listener;
};

export const createVoidListenerItem = (
id: string,
handler: () => void,
contextType?: string,
): ListenerItem => {
const listener: VoidListenerItem = {
id,
handler,
contextType
};
return listener;
};

0 comments on commit f9cae87

Please sign in to comment.