Skip to content

Commit 8a66475

Browse files
committed
feat(main: ipc): create notification
1 parent 9e91d5a commit 8a66475

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

src/main/services/ipc/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { subscribeToContextMenu } from './context-menu'
2+
import { subscribeToNotification } from './notifications'
23

34
export const subscribeToChannels = () => {
45
subscribeToContextMenu()
6+
subscribeToNotification()
57
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { NotificationPayload } from '../../types'
2+
import { ipcMain, Notification } from 'electron'
3+
4+
export const subscribeToNotification = () => {
5+
if (!Notification.isSupported()) return
6+
7+
ipcMain.handle<NotificationPayload>('notification', (event, payload) => {
8+
return new Promise(resolve => {
9+
const { body } = payload
10+
const notification = new Notification({
11+
title: 'massCode',
12+
body
13+
})
14+
notification.show()
15+
resolve()
16+
})
17+
})
18+
}

src/main/types/index.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ChannelSubject,
2525
'context-menu'
2626
>
2727

28-
export type Channel = 'restart' | ContextMenuChannel
28+
export type Channel = 'restart' | 'notification' | ContextMenuChannel
2929
export interface ContextMenuPayload {
3030
name?: string
3131
type: ContextMenuType
@@ -39,6 +39,10 @@ export interface ContextMenuResponse {
3939
data: any
4040
}
4141

42+
export interface NotificationPayload {
43+
body: string
44+
}
45+
4246
interface EventCallback {
4347
(event?: IpcRendererEvent, ...args: any[]): void
4448
}
@@ -59,7 +63,7 @@ interface StoreProperties<T> {
5963

6064
export interface ElectronBridge {
6165
ipc: {
62-
invoke<T, U>(channel: Channel, payload: U): Promise<T>
66+
invoke<T, U = any>(channel: Channel, payload: U): Promise<T>
6367
on(channel: Channel, cb: EventCallback): void
6468
once(channel: Channel, cb: EventCallback): void
6569
}

src/main/types/renderer.d.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import type {
2-
ContextMenuChannel,
3-
ContextMenuPayload,
4-
ElectronBridge,
5-
Channel
6-
} from '.'
1+
import type { ElectronBridge, Channel } from '.'
72

8-
// TODO: почему то нет автокомплита для ContextMenuPayload
93
declare global {
104
interface Window {
115
electron: ElectronBridge
126
}
137

148
namespace Electron {
159
interface IpcMain {
16-
handle<T, U>(
10+
handle<T, U = void>(
1711
channel: Channel,
1812
listener: (event: IpcMainInvokeEvent, payload: T) => Promise<U>
1913
): void

0 commit comments

Comments
 (0)