Skip to content

Commit

Permalink
support for getUserChannels and joinUserChannels. Refactoring for sup…
Browse files Browse the repository at this point in the history
…porting 2.0 handlers. Linting
  • Loading branch information
nkolba committed Oct 26, 2022
1 parent 2e1042f commit 563ee7f
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 68 deletions.
2 changes: 1 addition & 1 deletion packages/main/src/handlers/fdc3/1.2/channels.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getRuntime } from '/@/index';
import { RuntimeMessage } from '/@/handlers/runtimeMessage';
import { ChannelData } from '/@/handlers/fdc3/1.2/types/FDC3Data';
import { ChannelData } from '/@/types/Channel';
import { Context, ChannelError } from 'fdc3-1.2';
import { systemChannels } from './systemChannels';

Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/handlers/fdc3/1.2/systemChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* metadata for the system channels
*/

import type { ChannelData } from '/@/handlers/fdc3/1.2/types/FDC3Data';
import type { ChannelData } from '/@/types/Channel';

export const systemChannels: Array<ChannelData> = [
{
Expand Down
33 changes: 1 addition & 32 deletions packages/main/src/handlers/fdc3/1.2/types/FDC3Data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, DisplayMetadata, IntentMetadata } from 'fdc3-1.2';
import { Context, IntentMetadata } from 'fdc3-1.2';

/**
* represenation of an FDC3 App - whether it is running (connected) or not (directory only)
Expand Down Expand Up @@ -56,34 +56,3 @@ export interface DirectoryIntent {
display_name: string;
contexts: Array<string>;
}

/**
* representation of channel data
*/
export interface ChannelData {
id: string;
type: 'app' | 'system';
displayMetadata?: ChannelMetadata;
}

export class ChannelMetadata implements DisplayMetadata {
/**
* A user-readable name for this channel, e.g: `"Red"`
*/
name?: string;

/**
* The color that should be associated within this channel when displaying this channel in a UI, e.g: `0xFF0000`.
*/
color?: string;

/**
* A URL of an image that can be used to display this channel
*/
glyph?: string;

/**
* alternate / secondary color to use in conjunction with 'color' when creating UIs
*/
color2?: string;
}
119 changes: 119 additions & 0 deletions packages/main/src/handlers/fdc3/2.0/channels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { getRuntime } from '/@/index';
import { RuntimeMessage } from '/@/handlers/runtimeMessage';
import { ChannelData } from '/@/types/Channel';
import { Context, ChannelError } from '@finos/fdc3';
import { userChannels } from '../userChannels';

/** deprecate */
export const getSystemChannels = async () => {
return userChannels.map((c) => {
return { ...c, type: 'system' };
});
};

export const getUserChannels = async () => {
return userChannels;
};

export const getCurrentChannel = async (message: RuntimeMessage) => {
const runtime = getRuntime();

const view = runtime.getView(message.source);
return view?.channel ? getChannelMeta(view.channel) : null;
};

export const getCurrentContext = async (message: RuntimeMessage) => {
const runtime = getRuntime();

const channel = (message.data && message.data.channel) || undefined;
const type = (message.data && message.data.contextType) || undefined;
let ctx: Context | null = null;
if (channel) {
const contexts = runtime.getContexts();
const channelContext = contexts.get(channel);
if (type) {
if (channelContext) {
ctx =
channelContext.find((c) => {
return c.type === type;
}) || null;
}
} else {
ctx = channelContext && channelContext[0] ? channelContext[0] : ctx;
}
}
return ctx;
};

export const getOrCreateChannel = async (message: RuntimeMessage) => {
const runtime = getRuntime();
const id = (message.data && message.data.channelId) || 'default';
//reject with error is reserved 'default' term
if (id === 'default') {
throw ChannelError.CreationFailed;
} else {
let channel: ChannelData | undefined = getChannelMeta(id);

//if not found... create as an app channel
if (!channel) {
channel = { id: id, type: 'app' };
//add an entry for the context listeners
//contextListeners.set(id, new Map());
runtime.getContexts().set(id, []);
runtime.setAppChannel(channel);
}
if (channel) {
return channel;
} else {
return;
}
}
};

export const leaveCurrentChannel = async (message: RuntimeMessage) => {
const runtime = getRuntime();
//'default' means we have left all channels
const view = runtime.getView(message.source);
if (view) {
view.parent?.joinViewToChannel('default', view);
return;
} else {
throw 'View not found';
}
};

export const joinUserChannel = async (message: RuntimeMessage) => {
const runtime = getRuntime();
const channel = message.data && message.data.channel;
const view = runtime.getView(message.source);
if (channel && view) {
await view.parent?.joinViewToChannel(
channel,
view,
(message.data && message.data.restoreOnly) || undefined,
);
return true;
}
};

//generate / get full channel object from an id - returns null if channel id is not a user channel or a registered app channel
const getChannelMeta = (id: string): ChannelData | undefined => {
let channel: ChannelData | undefined;
//is it a user channel?
const sChannels = userChannels;
channel = sChannels.find((c) => {
return c.id === id;
});

//is it already an app channel?
if (!channel) {
const runtime = getRuntime();
const ac = runtime.getAppChannels().find((c) => {
return c.id === id;
});
if (ac) {
channel = { id: id, type: 'app' };
}
}
return channel;
};
57 changes: 57 additions & 0 deletions packages/main/src/handlers/fdc3/2.0/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Runtime } from '/@/runtime';
import { FDC3_TOPICS } from './topics';
import {
getCurrentContext,
getOrCreateChannel,
getUserChannels,
getSystemChannels,
leaveCurrentChannel,
joinUserChannel,
getCurrentChannel,
} from './channels';

//1.2...
import {
dropContextListener,
addContextListener,
} from '/@/handlers/fdc3/1.2/contextListeners';
import { broadcast } from '/@/handlers/fdc3/1.2/broadcast';
import { open } from '/@/handlers/fdc3/1.2/open';
import {
dropIntentListener,
addIntentListener,
} from '/@/handlers/fdc3/1.2/intentListeners';
import {
findIntent,
findIntentsByContext,
} from '/@/handlers/fdc3/1.2/findIntent';
import {
raiseIntent,
raiseIntentForContext,
} from '/@/handlers/fdc3/1.2/raiseIntent';

export const register = (runtime: Runtime) => {
runtime.addHandler(FDC3_TOPICS.GET_CURRENT_CONTEXT, getCurrentContext);
runtime.addHandler(FDC3_TOPICS.GET_OR_CREATE_CHANNEL, getOrCreateChannel);
runtime.addHandler(FDC3_TOPICS.GET_USER_CHANNELS, getUserChannels);
runtime.addHandler(FDC3_TOPICS.GET_SYSTEM_CHANNELS, getSystemChannels);
runtime.addHandler(FDC3_TOPICS.LEAVE_CURRENT_CHANNEL, leaveCurrentChannel);
runtime.addHandler(FDC3_TOPICS.JOIN_USER_CHANNEL, joinUserChannel);
runtime.addHandler(FDC3_TOPICS.JOIN_CHANNEL, joinUserChannel);
runtime.addHandler(FDC3_TOPICS.GET_CURRENT_CHANNEL, getCurrentChannel);

//1.2
runtime.addHandler(FDC3_TOPICS.DROP_CONTEXT_LISTENER, dropContextListener);
runtime.addHandler(FDC3_TOPICS.ADD_CONTEXT_LISTENER, addContextListener);
runtime.addHandler(FDC3_TOPICS.BROADCAST, broadcast);
runtime.addHandler(FDC3_TOPICS.OPEN, open);
runtime.addHandler(FDC3_TOPICS.ADD_INTENT_LISTENER, addIntentListener);
runtime.addHandler(FDC3_TOPICS.DROP_INTENT_LISTENER, dropIntentListener);
runtime.addHandler(FDC3_TOPICS.FIND_INTENT, findIntent);
runtime.addHandler(FDC3_TOPICS.FIND_INTENTS_BY_CONTEXT, findIntentsByContext);
runtime.addHandler(FDC3_TOPICS.RAISE_INTENT, raiseIntent);
runtime.addHandler(
FDC3_TOPICS.RAISE_INTENT_FOR_CONTEXT,
raiseIntentForContext,
);
};
24 changes: 24 additions & 0 deletions packages/main/src/handlers/fdc3/2.0/topics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const FDC3_TOPICS = {
START: 'FDC3:start',
INITIATE: 'FDC3:initiate',
DROP_CONTEXT_LISTENER: 'FDC3:dropContextListener',
ADD_CONTEXT_LISTENER: 'FDC3:addContextListener',
DROP_INTENT_LISTENER: 'FDC3:dropIntentListener',
ADD_INTENT_LISTENER: 'FDC3:addIntentListener',
BROADCAST: 'FDC3:broadcast',
OPEN: 'FDC3:open',
CONTEXT: 'FDC3:context',
GET_CURRENT_CONTEXT: 'FDC3:getCurrentContext',
GET_OR_CREATE_CHANNEL: 'FDC3:getOrCreateChannel',
GET_SYSTEM_CHANNELS: 'FDC3:getSystemChannels',
GET_USER_CHANNELS: 'FDC3:getUserChannels',
LEAVE_CURRENT_CHANNEL: 'FDC3:leaveCurrentChannel',
GET_CURRENT_CHANNEL: 'FDC3:getCurrentChannel',
JOIN_CHANNEL: 'FDC3:joinChannel',
JOIN_USER_CHANNEL: 'FDC3:joinUserChannel',
INTENT: 'FDC3:intent',
FIND_INTENT: 'FDC3:findIntent',
FIND_INTENTS_BY_CONTEXT: 'FDC3:findIntentsByContext',
RAISE_INTENT: 'FDC3:raiseIntent',
RAISE_INTENT_FOR_CONTEXT: 'FDC3:raiseIntentForContext',
};
34 changes: 34 additions & 0 deletions packages/main/src/handlers/fdc3/userChannels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ChannelData } from '/@/types/Channel';

export const userChannels: Array<ChannelData> = [
{
id: 'red',
type: 'user',
displayMetadata: { color: '#da2d2d', color2: '#9d0b0b', name: 'Red' },
},
{
id: 'orange',
type: 'user',
displayMetadata: { color: '#eb8242', color2: '#e25822', name: 'Orange' },
},
{
id: 'yellow',
type: 'user',
displayMetadata: { color: '#f6da63', color2: '#e3c878', name: 'Yellow' },
},
{
id: 'green',
type: 'user',
displayMetadata: { color: '#42b883', color2: '#347474', name: 'Green' },
},
{
id: 'blue',
type: 'user',
displayMetadata: { color: '#1089ff', color2: '#505BDA', name: 'Blue' },
},
{
id: 'purple',
type: 'user',
displayMetadata: { color: '#C355F5', color2: '#AA26DA', name: 'Purple' },
},
];
2 changes: 0 additions & 2 deletions packages/main/src/handlers/runtime/channelPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const hideChannelWindow = async (message: RuntimeMessage) => {

export const pickChannel = async (message: RuntimeMessage) => {
const runtime = getRuntime();
console.log('pickChannel', message.source, message.data);
const sourceWS = runtime.getWorkspace(message.source);
const mouseX: number = message.data.mouseX || 0;
const mouseY: number = message.data.mouseY || 0;
Expand All @@ -20,7 +19,6 @@ export const pickChannel = async (message: RuntimeMessage) => {
};

export const joinChannel = async (message: RuntimeMessage) => {
console.log('joinChannel', message);
const runtime = getRuntime();
const sourceWS = runtime.getWorkspace(message.source);
if (sourceWS) {
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/handlers/runtime/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const openToolsMenu = async (message: RuntimeMessage) => {
const runtime = getRuntime();
//bring selected browserview to front
const workspace = runtime?.getWorkspace(message.source);

if (workspace) {
const template = [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
DirectoryApp,
FDC3App,
IntentInstance,
ChannelData,
ResolverDetail,
} from '/@/handlers/fdc3/1.2/types/FDC3Data';
import { channels } from './system-channels';
Expand All @@ -17,8 +16,9 @@ import utils from './utils';
import { IntentResolver } from './IntentResolver';
import { RuntimeMessage } from './handlers/runtimeMessage';
import { register as registerRuntimeHandlers } from './handlers/runtime/index';
import { register as registerFDC3Handlers } from './handlers/fdc3/1.2/index';
import { register as registerFDC3Handlers } from './handlers/fdc3/2.0/index';
import { FDC3Response } from './types/FDC3Message';
import { ChannelData } from './types/Channel';

// map of all running contexts keyed by channel
const contexts: Map<string, Array<Context>> = new Map([['default', []]]);
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/system-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* metadata for the system channels
*/

import type { ChannelData } from '/@/handlers/fdc3/1.2/types/FDC3Data';
import type { ChannelData } from '/@/types/Channel';

export const channels: Array<ChannelData> = [
{
Expand Down
33 changes: 33 additions & 0 deletions packages/main/src/types/Channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DisplayMetadata } from '@finos/fdc3';
/**
* cross-version
* metadata for channels
*/

export interface ChannelData {
id: string;
type: 'user' | 'app' | 'private' | 'system';
displayMetadata?: ChannelMetadata;
}

export class ChannelMetadata implements DisplayMetadata {
/**
* A user-readable name for this channel, e.g: `"Red"`
*/
name?: string;

/**
* The color that should be associated within this channel when displaying this channel in a UI, e.g: `0xFF0000`.
*/
color?: string;

/**
* A URL of an image that can be used to display this channel
*/
glyph?: string;

/**
* alternate / secondary color to use in conjunction with 'color' when creating UIs
*/
color2?: string;
}
2 changes: 2 additions & 0 deletions packages/main/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,13 @@ export class Workspace {
}
}),
);

if (this.channelWindow) {
this.channelWindow.webContents.send(RUNTIME_TOPICS.CHANNEL_SELECTED, {
channel: channel,
});
}

if (this.window) {
this.window.webContents.send(RUNTIME_TOPICS.CHANNEL_SELECTED, {
channel: channel,
Expand Down
Loading

0 comments on commit 563ee7f

Please sign in to comment.