Skip to content

Commit f93242e

Browse files
committed
Исправление багов
- Исправил аргумент класса в функциях on и once - Исправил ошибку с уничтоженными отправителями
1 parent b5ac88a commit f93242e

File tree

4 files changed

+29
-38
lines changed

4 files changed

+29
-38
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "emr-bridge",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Allows you to easily give access to main methods from the renderer process",
55
"main": "esm/entries/index.js",
66
"type": "module",
@@ -47,6 +47,6 @@
4747
"electron": ">=20.0.0"
4848
},
4949
"devDependencies": {
50-
"typescript": "^5.5.3"
50+
"typescript": "^5.7.2"
5151
}
5252
}

src/scripts/helpers/web-contents-events-container.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class WebContentsEventContainer {
77
/**
88
* Контейнер событий.
99
*/
10-
private container = {} as {
10+
private containers = {} as {
1111
/**
1212
* Название события.
1313
*/
@@ -30,8 +30,7 @@ export default class WebContentsEventContainer {
3030
* @param sender - Отправитель.
3131
*/
3232
public addOn(event: string, sender: WebContents): void {
33-
this.addEvent(event)
34-
this.container[event].on.push(sender)
33+
this.addEvent(event).on.push(sender)
3534
}
3635

3736
/**
@@ -40,8 +39,7 @@ export default class WebContentsEventContainer {
4039
* @param sender - Отправитель.
4140
*/
4241
public addOnce(event: string, sender: WebContents): void {
43-
this.addEvent(event)
44-
this.container[event].once.push(sender)
42+
this.addEvent(event).once.push(sender)
4543
}
4644

4745
/**
@@ -50,7 +48,7 @@ export default class WebContentsEventContainer {
5048
* @returns многоразовые отправители для события.
5149
*/
5250
public getOn(event: string): WebContents[] {
53-
return this.getEvent(event).on.filter(sender => !sender.isDestroyed())
51+
return this.addEvent(event).on
5452
}
5553

5654
/**
@@ -59,7 +57,7 @@ export default class WebContentsEventContainer {
5957
* @returns Одноразовые отправители для события.
6058
*/
6159
public getOnce(event: string): WebContents[] {
62-
return this.getEvent(event).once.filter(sender => !sender.isDestroyed())
60+
return this.addEvent(event).once
6361
}
6462

6563
/**
@@ -68,35 +66,30 @@ export default class WebContentsEventContainer {
6866
* @returns Отправители для события.
6967
*/
7068
public getAll(event: string): WebContents[] {
71-
const senders = this.getEvent(event)
69+
const container = this.addEvent(event)
7270

73-
return [...senders.on, ...senders.once]
71+
return [...container.on, ...container.once]
7472
}
7573

7674
/**
7775
* Удалить одноразовых отправителей для события.
7876
* @param event - Имя события.
7977
*/
8078
public clearOnce(event: string): void {
81-
this.getEvent(event).once = []
79+
this.addEvent(event).once = []
8280
}
8381

8482
/**
8583
* Добавить контейнер события.
8684
* @param name - Имя события.
85+
* @returns Добавленный контейнер.
8786
*/
88-
private addEvent(name: string): void {
89-
this.container[name] ??= { on: [], once: [] }
90-
}
91-
92-
/**
93-
* Получить контейнер события.
94-
* @param name - Имя события.
95-
* @returns Контейнер события.
96-
*/
97-
private getEvent(name: string): (typeof this.container)[string] {
98-
this.addEvent(name)
87+
private addEvent(name: string): (typeof this.containers)[string] {
88+
const container = this.containers[name] ??= { on: [], once: [] }
9989

100-
return this.container[name]
90+
return this.containers[name] = {
91+
on: container.on.filter(item => !item.isDestroyed()),
92+
once: container.once.filter(item => !item.isDestroyed()),
93+
}
10194
}
10295
}

src/scripts/main/publish.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/*
2-
Методы публикации в main процессе.
3-
*/
1+
// Методы публикации в main процессе.
42

53
import { ipcMain } from 'electron'
64

@@ -83,13 +81,13 @@ export function emitEvent<T = unknown>(name: string, value?: T): void {
8381
export function on<TValue = any>(
8482
name: string,
8583
listener: EventListener<TValue>,
86-
receives: HasSnapshotClass<TValue>
84+
receives?: HasSnapshotClass<TValue>
8785
): EventUnsubscribe {
8886
canThrowError()
8987

9088
const emitChannel = IpcChannel.event.emit(name)
9189
const handler = (_: any, { value }: IResult<TValue>) => {
92-
if (Transfer.isSnapshotValue(value)) {
90+
if (Transfer.isSnapshotValue(value) && receives) {
9391
listener(Transfer.getFromSnapshot(value, receives) as TValue)
9492
} else {
9593
listener(value!)
@@ -111,13 +109,13 @@ export function on<TValue = any>(
111109
export function once<TValue = any>(
112110
name: string,
113111
listener: EventListener<TValue>,
114-
receives: HasSnapshotClass<TValue>
112+
receives?: HasSnapshotClass<TValue>
115113
): EventUnsubscribe {
116114
canThrowError()
117115

118116
const emitChannel = IpcChannel.event.emit(name)
119117
const handler = (_: any, { value }: IResult<TValue>) => {
120-
if (Transfer.isSnapshotValue(value)) {
118+
if (Transfer.isSnapshotValue(value) && receives) {
121119
listener(Transfer.getFromSnapshot(value, receives) as TValue)
122120
} else {
123121
listener(value!)

0 commit comments

Comments
 (0)