Skip to content

Commit

Permalink
chore: deprecate route handling from preload script
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Dec 9, 2023
1 parent 4aedae4 commit bdd6418
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
10 changes: 9 additions & 1 deletion core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@ export type FileSystemRouteFunctions = {
[K in FileSystemRoute]: ApiFunction
}

export type APIRoutes = AppRouteFunctions &
export type APIFunctions = AppRouteFunctions &
AppEventFunctions &
DownloadRouteFunctions &
DownloadEventFunctions &
ExtensionRouteFunctions &
FileSystemRouteFunctions

export const APIRoutes = [
...Object.values(AppRoute),
...Object.values(DownloadRoute),
...Object.values(ExtensionRoute),
...Object.values(FileSystemRoute),
]
export const APIEvents = [...Object.values(AppEvent), ...Object.values(DownloadEvent)]
1 change: 1 addition & 0 deletions electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"build:publish:linux": "tsc -p . && electron-builder -p onTagOrDraft -l deb"
},
"dependencies": {
"@janhq/core": "link:./core",
"@npmcli/arborist": "^7.1.0",
"@types/request": "^2.48.12",
"@uiball/loaders": "^1.3.0",
Expand Down
21 changes: 3 additions & 18 deletions electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,18 @@
* @module preload
*/

import {
AppEvent,
AppRoute,
DownloadEvent,
DownloadRoute,
ExtensionRoute,
FileSystemRoute,
} from '@janhq/core'
import { APIEvents, APIRoutes, AppEvent, DownloadEvent } from '@janhq/core'
const { contextBridge } = require('electron')

const { ipcRenderer } = require('electron')
const ipcMethods = [
...Object.values(AppRoute),
...Object.values(DownloadRoute),
...Object.values(ExtensionRoute),
...Object.values(FileSystemRoute),
]

const ipcEvents = [...Object.values(AppEvent), ...Object.values(DownloadEvent)]

const interfaces: { [key: string]: (...args: any[]) => any } = {}

ipcMethods.forEach((method) => {
APIRoutes.forEach((method) => {
interfaces[method] = (...args: any[]) => ipcRenderer.invoke(method, ...args)
})

ipcEvents.forEach((method) => {
APIEvents.forEach((method) => {
interfaces[method] = (handler: any) => ipcRenderer.on(method, handler)
})

Expand Down
4 changes: 2 additions & 2 deletions web/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIRoutes } from '@janhq/core'
import { APIFunctions } from '@janhq/core'

/* eslint-disable @typescript-eslint/no-explicit-any */
export {}
Expand All @@ -7,7 +7,7 @@ declare global {
declare const PLUGIN_CATALOG: string
declare const VERSION: string
interface Core {
api: APIRoutes
api: APIFunctions
events: EventEmitter
}
interface Window {
Expand Down

0 comments on commit bdd6418

Please sign in to comment.