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

feat: call context #301

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs-site/docs/creatingplugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,24 @@ export default class App {
```

**Keep in mind that the config could possibly be `undefined`.**

## Caller
Plugins get access to the caller of the method using the context object that gets passed as the second argument of the constructor. E.g.:

```typescript
import { PluginContext } from '@capacitor/electron';

export default class App {
private context: PluginContext;

constructor(_?: Record<string, any>, context: PluginContext) {
this.context = context;
}

getId(): number {
const { sender } = this.context.caller.get();

return sender.id;
}
}
```
18 changes: 18 additions & 0 deletions src/electron-platform/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CapacitorConfig } from '@capacitor/cli';
import type { WebContents, WebFrameMain, IpcMainInvokeEvent } from 'electron';

export interface SplashOptions {
imageFilePath?: string;
Expand Down Expand Up @@ -27,3 +28,20 @@ export interface ElectronConfig {
export type CapacitorElectronConfig = CapacitorConfig & {
electron?: ElectronConfig;
};

export type PluginContext = {
caller: { readonly get: () => CallContext };
};

export type CallContext = {
/** The internal ID of the renderer process that sent this message */
processId: number;
/** The ID of the renderer frame that sent this message */
frameId: number;
/** Returns the `webContents` that sent the message */
sender: WebContents;
/** The frame that sent this message */
senderFrame: WebFrameMain;
/** The raw `IpcMainInvokeEvent` */
event: IpcMainInvokeEvent;
};
28 changes: 23 additions & 5 deletions src/electron-platform/util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/prefer-for-of */
import { AsyncLocalStorage } from 'async_hooks';
import { app, ipcMain } from 'electron';
import EventEmitter from 'events';
import { existsSync, readFileSync } from 'fs';
import mimeTypes from 'mime-types';
import { join } from 'path';

import type { CapacitorElectronConfig } from './definitions';
import type { CallContext, CapacitorElectronConfig } from './definitions';

class CapElectronEmitter extends EventEmitter {}
let config: CapacitorElectronConfig = {};
Expand Down Expand Up @@ -87,6 +88,14 @@ export function deepClone<T>(object: Record<string, T>): Record<string, T> {
const pluginInstanceRegistry: { [pluginClassName: string]: { [functionName: string]: any } } = {};

export function setupCapacitorElectronPlugins(): void {
const callerStorage = new AsyncLocalStorage();
const caller = Object.defineProperties(
{},
{
get: { value: () => callerStorage.getStore(), writable: false, configurable: false, enumerable: true },
}
);

console.log('in setupCapacitorElectronPlugins');
const rtPluginsPath = join(app.getAppPath(), 'build', 'src', 'rt', 'electron-plugins.js');
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand All @@ -101,7 +110,9 @@ export function setupCapacitorElectronPlugins(): void {
console.log(`-> ${classKey}`);

if (!pluginInstanceRegistry[classKey]) {
pluginInstanceRegistry[classKey] = new plugins[pluginKey][classKey](deepClone(config as Record<string, any>));
pluginInstanceRegistry[classKey] = new plugins[pluginKey][classKey](deepClone(config as Record<string, any>), {
caller,
});
}

const functionList = Object.getOwnPropertyNames(plugins[pluginKey][classKey].prototype).filter(
Expand All @@ -111,11 +122,18 @@ export function setupCapacitorElectronPlugins(): void {
for (const functionName of functionList) {
console.log(`--> ${functionName}`);

ipcMain.handle(`${classKey}-${functionName}`, (_event, ...args) => {
ipcMain.handle(`${classKey}-${functionName}`, (event, ...args) => {
console.log(`called ipcMain.handle: ${classKey}-${functionName}`);
const pluginRef = pluginInstanceRegistry[classKey];

return pluginRef[functionName](...args);
const callContext: CallContext = {
senderFrame: event.senderFrame,
processId: event.processId,
frameId: event.frameId,
sender: event.sender,
event,
};

return callerStorage.run(callContext, () => pluginRef[functionName](...args));
});
}

Expand Down
Loading