-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for getUserChannels and joinUserChannels. Refactoring for sup…
…porting 2.0 handlers. Linting
- Loading branch information
Showing
22 changed files
with
319 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.