Commit a94a615 1 parent 3efe737 commit a94a615 Copy full SHA for a94a615
File tree 2 files changed +12
-3
lines changed
2 files changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -58,11 +58,12 @@ declare const appAPI: {
58
58
sendCommand : ( command : string ) => Promise < void > ;
59
59
/**
60
60
* Allows the renderer to subscribe to messages from the main process.
61
+ * Returns an unsubscribe function, useful in react hook cleanup functions.
61
62
*/
62
63
onMessage : (
63
64
channel : string ,
64
65
callback : ( event : Electron . IpcRendererEvent , ...args : any [ ] ) => void
65
- ) => void ;
66
+ ) => OnMessageUnsubscribe ;
66
67
/**
67
68
* Allows the renderer to unsubscribe from messages from the main process.
68
69
* Removes all listeners added by the `onMessage` API for a channel.
@@ -75,6 +76,7 @@ declare const appAPI: {
75
76
removeAllListeners ( channel : string ) : void ;
76
77
} ;
77
78
declare global {
79
+ type OnMessageUnsubscribe = ( ) => void ;
78
80
type TypeOfAppAPI = typeof appAPI ;
79
81
type AppAPI = {
80
82
[ K in keyof TypeOfAppAPI ] : TypeOfAppAPI [ K ] ;
Original file line number Diff line number Diff line change @@ -80,12 +80,17 @@ const appAPI = {
80
80
} ,
81
81
/**
82
82
* Allows the renderer to subscribe to messages from the main process.
83
+ * Returns an unsubscribe function, useful in react hook cleanup functions.
83
84
*/
84
85
onMessage : (
85
86
channel : string ,
86
87
callback : ( event : IpcRendererEvent , ...args : Array < any > ) => void
87
- ) => {
88
+ ) : OnMessageUnsubscribe => {
88
89
ipcRenderer . on ( channel , callback ) ;
90
+
91
+ return ( ) => {
92
+ ipcRenderer . off ( channel , callback ) ;
93
+ } ;
89
94
} ,
90
95
/**
91
96
* Allows the renderer to unsubscribe from messages from the main process.
@@ -96,12 +101,14 @@ const appAPI = {
96
101
* is regenerated. To prevent this, ensure to unsubscribe in the hook's
97
102
* destroy function. https://stackoverflow.com/a/73458622/470818
98
103
*/
99
- removeAllListeners ( channel : string ) {
104
+ removeAllListeners ( channel : string ) : void {
100
105
ipcRenderer . removeAllListeners ( channel ) ;
101
106
} ,
102
107
} ;
103
108
104
109
declare global {
110
+ type OnMessageUnsubscribe = ( ) => void ;
111
+
105
112
type TypeOfAppAPI = typeof appAPI ;
106
113
107
114
type AppAPI = {
You can’t perform that action at this time.
0 commit comments