Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: deprecate invokers - auto proxying apis - strict types #924

Merged
merged 5 commits into from
Dec 11, 2023

Conversation

louis-menlo
Copy link
Contributor

@louis-menlo louis-menlo commented Dec 9, 2023

Problem

Maintaining IPC APIs is currently challenging. Every time we need to expose a new API, we find ourselves having to perform the following steps:

  • Manually add new proxy routes to preload.ts with route string names, which involves managing numerous invokers.
  • Add new handlers to main.ts with poorly formatted strings.
  • Invoke from the client using that string.
  • Web client making requests dynamically with any type of APIs

This situation poses several problems:

  • The process is highly manual; the proxying in preload.ts should be generated or proxied automatically to reduce code duplication.
  • Passing route names as raw strings among classes and platforms is error-prone.
  • Updating or maintaining route names becomes very difficult.
  • Building adapters for other platforms (e.g., servers) is challenging when the supported and missed routes are not known.
  • Hard to know what APIs supported and no compile time check for API requests
  • Unused apis are not cleaned, and quite hard to clean its dependencies with this raw string function name. Now its easier

This PR is to address the concern of @dan-jan about how we can autogenerate preload.ts code.

To Discuss:

  1. Although this approach would facilitate future updates by eliminating raw string routes and consolidating them in one place (core), there is a risk of overlooking the implementation of newly added routes (handlers). Is there a way to define the Handler/Route as an abstract class so that any implemented class would trigger an error immediately in case of a missing handler implementation?

  2. After refactored the handler logics to /common, could it be proxied automatically instead of adding handler one by one?

@github-actions github-actions bot added the type: chore Maintenance, operational label Dec 9, 2023
core/src/process.ts Outdated Show resolved Hide resolved
electron/preload.ts Outdated Show resolved Hide resolved
@louis-menlo louis-menlo force-pushed the chore/deprecate-invokers branch 2 times, most recently from 861eb67 to 958b272 Compare December 9, 2023 03:28
@louis-menlo louis-menlo force-pushed the chore/deprecate-invokers branch 2 times, most recently from 492ecab to dd2e0f0 Compare December 9, 2023 03:44
@louis-menlo
Copy link
Contributor Author

louis-menlo commented Dec 9, 2023

There is no longer a need to handle the preload script manually, as was done previously. Suppose we need to add a new exists function to the fs module. In this case, we have to:

  1. Define exists bridge in the preload script:
exists: (path: string) => ipcRenderer.invoke('exists', path) // So manual, typo risk
or
exists: (arg1: string, arg2) => ipcRenderer.invoke('exists', arg1, arg2) // So manual, typo risk
  1. Add the exists handler with a raw string name:
ipcMain.handle('exists', async (_event, path: string) => { }) // typo risk
  1. Make a request to an unknown function:
global.core.api?.exists(path) // typo risk, no compile-time check for the `exists` route declaration, e.g., global.core.api?.exist(path) -> app crashes on runtime if someone change the route name in handler / preload / request

Now, simply add new routes to core:

export enum FileSystemRoute {
  deleteFile = 'deleteFile',
  isDirectory = 'isDirectory',
  getUserSpace = 'getUserSpace',
  // ...new routes go here
  // easier to maintain
}

Provide handlers for new routes :

handler.handle(FileSystemRoute.exists, async (_event, path: string) => { } // no typo risk

Make requests from the web app (strict type guard).

global.core?.api?.exists(path) // compile error if the route does not exist, no typo risk

@louis-menlo louis-menlo force-pushed the chore/deprecate-invokers branch 2 times, most recently from 3b4aa2f to bdd6418 Compare December 9, 2023 04:26
@louis-menlo louis-menlo changed the title refactor: deprecate invokers - auto proxying apis refactor: deprecate invokers - auto proxying apis - strict types Dec 9, 2023
@louis-menlo louis-menlo force-pushed the chore/deprecate-invokers branch from bdd6418 to 8ed44fa Compare December 9, 2023 04:31
Copy link
Collaborator

@hiento09 hiento09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@louis-menlo louis-menlo merged commit c4d8def into main Dec 11, 2023
3 checks passed
@louis-menlo louis-menlo deleted the chore/deprecate-invokers branch December 11, 2023 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: chore Maintenance, operational
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants