Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 7bae295

Browse files
authored
Add client side Event helper (#38)
* Add broadcasting endpoint & dispatch ipc event * Inject Native helper object in browser window * Strip leading slashes before matching event name * added event name as second callback parameter
1 parent 861dfab commit 7bae295

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/preload/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { contextBridge, ipcRenderer } from 'electron'
22
import * as remote from '@electron/remote'
33

4+
import Native from './native';
5+
6+
// @ts-ignore
7+
window.Native = Native;
8+
49
// @ts-ignore
510
window.remote = remote;
611

src/preload/native.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ipcRenderer } from 'electron'
2+
3+
export default {
4+
on: (event, callback) => {
5+
ipcRenderer.on('native-event', (_, data) => {
6+
7+
// Strip leading slashes
8+
event = event.replace(/^(\\)+/, '');
9+
data.event = data.event.replace(/^(\\)+/, '');
10+
11+
if(event === data.event) {
12+
return callback(data.payload, event);
13+
}
14+
})
15+
}
16+
}

0 commit comments

Comments
 (0)