Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsil committed Dec 13, 2023
1 parent 2dda646 commit 37fa578
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
15 changes: 13 additions & 2 deletions lib/papi-dts/papi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,17 @@ declare module 'shared/utils/util' {
},
objId?: string,
): Set<string>;
/**
* Run an array of promises, and either return an array of the outcomes if them all were fulfilled
* or throw if at least one of them was rejected
*
* @param promises Array of promises to resolve
* @returns Array of `PromiseSettledResult` values from each promise if all promises were fulfilled.
* Otherwise an exception will be thrown.
*/
export function runPromisesAndThrowIfRejected(
...promises: Promise<unknown>[]
): Promise<PromiseSettledResult<unknown>[]>;
}
declare module 'shared/utils/papi-util' {
import { ProcessType } from 'shared/global-this.model';
Expand Down Expand Up @@ -4794,14 +4805,14 @@ declare module '@papi/frontend' {
*
* Note that the Node WebSocket implementation is different and not wrapped here.
*/
webSocket: typeof PapiRendererWebSocket;
WebSocket: typeof PapiRendererWebSocket;
/** This wraps the browser's XMLHttpRequest implementation to
* provide better control over internet access. It is isomorphic with the standard XMLHttpRequest,
* so it should act as a drop-in replacement.
*
* Note that Node doesn't have a native implementation, so this is only for the renderer.
*/
xmlHttpRequest: typeof PapiRendererXMLHttpRequest;
XMLHttpRequest: typeof PapiRendererXMLHttpRequest;
/**
*
* The command service allows you to exchange messages with other components in the platform. You
Expand Down
24 changes: 3 additions & 21 deletions src/renderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,13 @@ import { startWebViewService } from '@renderer/services/web-view.service-host';
import logger from '@shared/services/logger.service';
import webViewProviderService from '@shared/services/web-view-provider.service';
import { startDialogService } from '@renderer/services/dialog.service-host';
import { runPromisesAndThrowIfRejected } from '@shared/utils/util';
import App from './app.component';
import { cleanupOldWebViewState } from './services/web-view-state.service';
import { blockWebSocketsToPapiNetwork } from './services/renderer-web-socket.service';

logger.info('Starting renderer');

window.onerror = (message, source, lineno, colno, error): void => {
logger.error(`An error occurred: ${message}`);
logger.error(`Source file: ${source}`);
logger.error(`Line number: ${lineno}`);
logger.error(`Column number: ${colno}`);
logger.error(`Error object: ${error}`);
};

if (window.process) {
window.process.on('uncaughtException', (error) => {
const { dialog } = window.require('electron');
dialog.showMessageBoxSync({
type: 'error',
message: `Unexpected error occurred: ${error}`,
title: 'Error',
});
});
}

// App-wide service setup
// We are not awaiting these service startups for a few reasons:
// - They internally await other services when they need others in order to start
Expand All @@ -44,12 +26,12 @@ if (window.process) {
// This needs to run before web views start running and after the network service is running
blockWebSocketsToPapiNetwork();

await Promise.allSettled([
await runPromisesAndThrowIfRejected(
commandService.initialize(),
webViewProviderService.initialize(),
startWebViewService(),
startDialogService(),
]);
);
} catch (e) {
logger.error(`Service(s) failed to initialize! Error: ${e}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/services/papi-frontend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ const papi = {

// Classes
/** JSDOC DESTINATION PapiRendererWebSocket */
webSocket: PapiRendererWebSocket,
WebSocket: PapiRendererWebSocket,
/** JSDOC DESTINATION PapiRendererXMLHttpRequest */
xmlHttpRequest: PapiRendererXMLHttpRequest,
XMLHttpRequest: PapiRendererXMLHttpRequest,

// Services/modules
/** JSDOC DESTINATION commandService */
Expand Down
1 change: 1 addition & 0 deletions src/renderer/services/renderer-web-socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function isPotentialConnectionToPapiNetwork(url: string | URL): boolean {
hostname === '127.0.0.1' ||
hostname === '::1' ||
hostname.endsWith('.local') ||
hostname.endsWith('.localhost') ||
parseInt(port, 10) === parseInt(`${WEBSOCKET_PORT}`, 10)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/services/web-view.service-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,8 @@ export const getWebView = async (
var updateWebViewDefinitionById = window.parent.updateWebViewDefinitionById;
window.updateWebViewDefinition = (webViewDefinitionUpdateInfo) => { return updateWebViewDefinitionById('${webView.id}', webViewDefinitionUpdateInfo)}
window.fetch = papi.fetch;
window.WebSocket = papi.webSocket;
window.XMLHttpRequest = papi.xmlHttpRequest;
window.WebSocket = papi.WebSocket;
window.XMLHttpRequest = papi.XMLHttpRequest;
delete window.parent;
delete window.top;
delete window.frameElement;
Expand Down
21 changes: 21 additions & 0 deletions src/shared/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,24 @@ export function getAllObjectFunctionNames(

return objectFunctionNames;
}

/**
* Run an array of promises, and either return an array of the outcomes if them all were fulfilled
* or throw if at least one of them was rejected
*
* @param promises Array of promises to resolve
* @returns Array of `PromiseSettledResult` values from each promise if all promises were fulfilled.
* Otherwise an exception will be thrown.
*/
export async function runPromisesAndThrowIfRejected(...promises: Promise<unknown>[]) {
const resolutions = await Promise.allSettled(promises);
const rejections = resolutions.filter((resolution) => resolution.status === 'rejected');
if (rejections.length > 0) {
const reasons = rejections.map((rejection, index) => {
if (rejection.status !== 'rejected') return "Why doesn't TS know we already checked this?";
return `[${index}]: ${rejection.reason}`;
});
throw new Error(`${reasons}`);
}
return resolutions;
}

0 comments on commit 37fa578

Please sign in to comment.