Skip to content

Commit

Permalink
more fixes for preload refactor. linting
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolba committed Oct 8, 2022
1 parent f81b33b commit 9535910
Show file tree
Hide file tree
Showing 20 changed files with 650 additions and 524 deletions.
3 changes: 2 additions & 1 deletion packages/main/src/handlers/fdc3/1.2/contextListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ export const addContextListener = async (message: RuntimeMessage) => {
pending.context.type === message.data &&
message.data.type
) {
console.log('send pending context');
view.content.webContents.postMessage(FDC3_TOPICS.CONTEXT, {
topic: 'context',
topic: FDC3_TOPICS.CONTEXT,
data: pending.context,
source: source,
});
Expand Down
12 changes: 5 additions & 7 deletions packages/main/src/handlers/fdc3/1.2/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRuntime } from '/@/index';
import { Runtime } from '/@/runtime';
import { dropContextListener, addContextListener } from './contextListeners';
import { broadcast } from './broadcast';
import { open } from './open';
Expand All @@ -13,11 +13,9 @@ import {
} from './channels';
import { dropIntentListener, addIntentListener } from './intentListeners';
import { findIntent, findIntentsByContext } from './findIntent';
import { raiseIntent, raiseIntentsForContext } from './raiseIntent';

export const register = () => {
const runtime = getRuntime();
import { raiseIntent, raiseIntentForContext } from './raiseIntent';

export const register = (runtime: Runtime) => {
runtime.addHandler(FDC3_TOPICS.DROP_CONTEXT_LISTENER, dropContextListener);
runtime.addHandler(FDC3_TOPICS.ADD_CONTEXT_LISTENER, addContextListener);
runtime.addHandler(FDC3_TOPICS.BROADCAST, broadcast);
Expand All @@ -34,7 +32,7 @@ export const register = () => {
runtime.addHandler(FDC3_TOPICS.FIND_INTENTS_BY_CONTEXT, findIntentsByContext);
runtime.addHandler(FDC3_TOPICS.RAISE_INTENT, raiseIntent);
runtime.addHandler(
FDC3_TOPICS.RAISE_INTENTS_FOR_CONTEXT,
raiseIntentsForContext,
FDC3_TOPICS.RAISE_INTENT_FOR_CONTEXT,
raiseIntentForContext,
);
};
3 changes: 2 additions & 1 deletion packages/main/src/handlers/fdc3/1.2/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const resolveTargetAppToName = (target: TargetApp): string | undefined => {
};

export const open = async (message: RuntimeMessage) => {
console.log('open', message);
const runtime = getRuntime();
const name =
message.data && message.data.name
Expand All @@ -35,7 +36,7 @@ export const open = async (message: RuntimeMessage) => {
: '';

const result: DirectoryApp = (await runtime.fetchFromDirectory(
`/apps/${name}`,
`/apps${name !== '' ? '/' + name : ''}`,
)) as DirectoryApp;
if (result && result.start_url) {
//get target workspace
Expand Down
264 changes: 127 additions & 137 deletions packages/main/src/handlers/fdc3/1.2/raiseIntent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,171 +237,161 @@ export const raiseIntent = async (message: RuntimeMessage) => {
const r: Array<FDC3App> = [];
const intent = message.data?.intent;

if (intent) {
//only support string targets for now...
const target: string | undefined =
message.data?.target && typeof message.data.target === 'string'
? message.data.target
: undefined;
const intentListeners = runtime.getIntentListeners(intent, target);

const sourceView = runtime.getView(message.source);
const sourceName =
sourceView && sourceView.directoryData
? sourceView.directoryData.name
: 'unknown';

if (intentListeners) {
// let keys = Object.keys(intentListeners);
intentListeners.forEach((listener) => {
///ignore listeners from the view that raised the intent
if (listener.viewId && listener.viewId !== message.source) {
//look up the details of the window and directory metadata in the "connected" store
const view = runtime.getView(listener.viewId);
//de-dupe
if (
view &&
!r.find((item) => {
return item.details.instanceId === view.id;
})
) {
r.push({
type: 'window',
details: {
instanceId: view.id,
directoryData: view.directoryData,
},
});
}
}
});
}
//pull intent handlers from the directory
let ctx = '';
if (message.data && message.data.context) {
ctx = message.data.context.type;
}
const directoryUrl = await utils.getDirectoryUrl();

const query =
message.data && message.data.target
? resolveTargetAppToQuery(message.data.target)
: '';

const _r = await fetch(
`${directoryUrl}/apps/search?intent=${intent}&context=${ctx}${query}`,
);
//console.log('raiseIntent', _r);
if (_r) {
let data = null;
try {
data = await _r.json();
} catch (err) {
throw ResolveError.NoAppsFound;
}
if (!intent) {
throw 'No Intent Provided';
}

//only support string targets for now...
const target: string | undefined =
message.data?.target && typeof message.data.target === 'string'
? message.data.target
: undefined;
const intentListeners = runtime.getIntentListeners(intent, target);

const sourceView = runtime.getView(message.source);
const sourceName =
sourceView && sourceView.directoryData
? sourceView.directoryData.name
: 'unknown';

if (data) {
data.forEach((entry: DirectoryApp) => {
if (intentListeners) {
// let keys = Object.keys(intentListeners);
intentListeners.forEach((listener) => {
///ignore listeners from the view that raised the intent
if (listener.viewId && listener.viewId !== message.source) {
//look up the details of the window and directory metadata in the "connected" store
const view = runtime.getView(listener.viewId);
//de-dupe
if (
view &&
!r.find((item) => {
return item.details.instanceId === view.id;
})
) {
r.push({
type: 'directory',
details: { directoryData: entry },
type: 'window',
details: {
instanceId: view.id,
directoryData: view.directoryData,
},
});
});
}
}
}
});
}
//pull intent handlers from the directory
let ctx = '';
if (message.data && message.data.context) {
ctx = message.data.context.type;
}

if (r.length > 0) {
if (r.length === 1) {
//if there is only one result, use that
//if it is an existing view, post a message directly to it
//if it is a directory entry resolve the destination for the intent and launch it
//dedupe window and directory items
if (r[0].type === 'window' && r[0].details && r[0].details.instanceId) {
const view = runtime.getView(r[0].details.instanceId);
if (view) {
view.content.webContents.send(FDC3_TOPICS.INTENT, {
topic: 'intent',
data: message.data,
source: message.source,
});
//bringing the tab to front conditional on the type of intent
if (!utils.isDataIntent(intent)) {
/* utils.bringToFront(r[0].details.port); */
}
const query =
message.data && message.data.target
? resolveTargetAppToQuery(message.data.target)
: '';

return {
source: { name: sourceName, appId: message.source },
version: '1.2',
};
}
} else if (r[0].type === 'directory' && r[0].details.directoryData) {
const start_url = r[0].details.directoryData.start_url;
const pending = true;
const data: Array<DirectoryApp> = (await runtime.fetchFromDirectory(
`/apps/search?intent=${intent}&context=${ctx}${query}`,
)) as Array<DirectoryApp>;

//let win = window.open(start_url,"_blank");
const workspace = getRuntime().createWorkspace();
if (data) {
data.forEach((entry: DirectoryApp) => {
r.push({
type: 'directory',
details: { directoryData: entry },
});
});
}

const view = workspace.createView(start_url, {
directoryData: r[0].details.directoryData,
if (r.length > 0) {
if (r.length === 1) {
//if there is only one result, use that
//if it is an existing view, post a message directly to it
//if it is a directory entry resolve the destination for the intent and launch it
//dedupe window and directory items
if (r[0].type === 'window' && r[0].details && r[0].details.instanceId) {
const view = runtime.getView(r[0].details.instanceId);
if (view) {
view.content.webContents.send(FDC3_TOPICS.INTENT, {
topic: 'intent',
data: message.data,
source: message.source,
});
//view.directoryData = r[0].details.directoryData;
//set pending intent for the view..
if (pending) {
view.setPendingIntent(
intent,
(message.data && message.data.context) || undefined,
message.source,
);
//bringing the tab to front conditional on the type of intent
if (!utils.isDataIntent(intent)) {
/* utils.bringToFront(r[0].details.port); */
}

return {
source: { name: sourceName, appId: message.source },
source: { name: view.directoryData?.name, appId: message.source },
version: '1.2',
};

//send the context - if the default start_url was used...
//get the window/tab...
// resolve({result:true});
}
} else {
//show resolver UI
// Send a message to the active tab
//sort results alphabetically, with directory entries first (before window entries)

r.sort(sortApps);
} else if (r[0].type === 'directory' && r[0].details.directoryData) {
const start_url = r[0].details.directoryData.start_url;
const pending = true;

const eventId = `resolveIntent-${Date.now()}`;
//let win = window.open(start_url,"_blank");
const workspace = getRuntime().createWorkspace();

//set a handler for resolving the intent (when end user selects a destination)
ipcMain.on(eventId, async (event, args) => {
const r: IntentResolution = await resolveIntent(args);
return r;
const view = workspace.createView(start_url, {
directoryData: r[0].details.directoryData,
});

//launch window with resolver UI
// console.log('resolve intent - options', r);
const sourceView = getRuntime().getView(message.source);
if (sourceView) {
getRuntime().openResolver(
{
intent: intent,
context: (message.data && message.data.context) || undefined,
},
sourceView,
r,
//view.directoryData = r[0].details.directoryData;
//set pending intent for the view..
if (pending) {
view.setPendingIntent(
intent,
(message.data && message.data.context) || undefined,
message.source,
);
}

return {
source: { name: sourceName, appId: message.source },
version: '1.2',
};

//send the context - if the default start_url was used...
//get the window/tab...
// resolve({result:true});
}
} else {
//show message indicating no handler for the intent...
throw ResolveError.NoAppsFound;
//show resolver UI
// Send a message to the active tab
//sort results alphabetically, with directory entries first (before window entries)

r.sort(sortApps);

const eventId = `resolveIntent-${Date.now()}`;

//set a handler for resolving the intent (when end user selects a destination)
ipcMain.on(eventId, async (event, args) => {
const r: IntentResolution = await resolveIntent(args);
return r;
});

//launch window with resolver UI
// console.log('resolve intent - options', r);
const sourceView = getRuntime().getView(message.source);
if (sourceView) {
getRuntime().openResolver(
{
intent: intent,
context: (message.data && message.data.context) || undefined,
},
sourceView,
r,
);
}
}
} else {
//show message indicating no handler for the intent...
throw ResolveError.NoAppsFound;
}
};

export const raiseIntentsForContext = async (message: RuntimeMessage) => {
export const raiseIntentForContext = async (message: RuntimeMessage) => {
const runtime = getRuntime();
const sourceView = runtime.getView(message.source);
const sourceName =
Expand Down
4 changes: 3 additions & 1 deletion packages/main/src/handlers/fdc3/1.2/topics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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',
Expand All @@ -16,5 +18,5 @@ export const FDC3_TOPICS = {
FIND_INTENT: 'FDC3:findIntent',
FIND_INTENTS_BY_CONTEXT: 'FDC3:findIntentsByContext',
RAISE_INTENT: 'FDC3:raiseIntent',
RAISE_INTENTS_FOR_CONTEXT: 'FDC3:raiseIntentsForContext',
RAISE_INTENT_FOR_CONTEXT: 'FDC3:raiseIntentForContext',
};
1 change: 1 addition & 0 deletions packages/main/src/handlers/runtime/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const fetchFromDirectory = async (message: RuntimeMessage) => {
if (message.data.sourceType && message.data.sourceType === 'view') {
//ensure this is a view that has permissions for this api
const sourceView = runtime.getView(message.source);

if (sourceView && sourceView.isSystemView() && sourceView.content) {
wContents = sourceView.content.webContents;
}
Expand Down
6 changes: 2 additions & 4 deletions packages/main/src/handlers/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRuntime } from '../../index';
import { Runtime } from '/@/runtime';
import { RUNTIME_TOPICS } from './topics';

import {
Expand All @@ -15,9 +15,7 @@ import { pickChannel, joinChannel } from './channelPicker';
import { loadSearchResults } from './search';
import { resolveIntent } from '../fdc3/resolveIntent';

export const register = () => {
const runtime = getRuntime();

export const register = (runtime: Runtime) => {
runtime.addHandler(RUNTIME_TOPICS.TAB_SELECTED, tabSelected);
runtime.addHandler(RUNTIME_TOPICS.TAB_DRAG_START, tabDragStart);
runtime.addHandler(RUNTIME_TOPICS.DROP_TAB, dropTab);
Expand Down
Loading

0 comments on commit 9535910

Please sign in to comment.