@@ -9,6 +9,21 @@ export function useWebPushNotifications() {
9
9
const channel = ref < BroadcastChannel | null > ( null )
10
10
const isSupported = computed ( ( ) => typeof BroadcastChannel !== 'undefined' )
11
11
12
+ /**
13
+ * Closes the broadcast channel in case of error for preventing memory leaks
14
+ */
15
+ const safeCloseChannel = ( ) => {
16
+ if ( channel . value ) {
17
+ try {
18
+ channel . value . close ( )
19
+ } catch ( error ) {
20
+ console . warn ( '[Web Push] Error closing BroadcastChannel:' , error )
21
+ } finally {
22
+ channel . value = null
23
+ }
24
+ }
25
+ }
26
+
12
27
/**
13
28
* Gets reference to pushService (always WebPushService on web)
14
29
*/
@@ -29,6 +44,7 @@ export function useWebPushNotifications() {
29
44
channel . value = new BroadcastChannel ( 'adm_notifications' )
30
45
} catch ( error ) {
31
46
console . error ( '[Web Push] Failed to create BroadcastChannel:' , error )
47
+ channel . value = null
32
48
}
33
49
}
34
50
@@ -43,6 +59,7 @@ export function useWebPushNotifications() {
43
59
return true
44
60
} catch ( error ) {
45
61
console . error ( '[Web Push] Failed to send private key to SW:' , error )
62
+ safeCloseChannel ( )
46
63
return false
47
64
}
48
65
}
@@ -84,6 +101,7 @@ export function useWebPushNotifications() {
84
101
return true
85
102
} catch ( error ) {
86
103
console . error ( '[Web Push] Failed to clear private key:' , error )
104
+ safeCloseChannel ( )
87
105
return false
88
106
}
89
107
}
@@ -106,6 +124,7 @@ export function useWebPushNotifications() {
106
124
return true
107
125
} catch ( error ) {
108
126
console . error ( '[Web Push] Failed to sync settings:' , error )
127
+ safeCloseChannel ( )
109
128
return false
110
129
}
111
130
}
@@ -121,18 +140,14 @@ export function useWebPushNotifications() {
121
140
return true
122
141
} catch ( error ) {
123
142
console . error ( '[Web Push] Failed to set message handler:' , error )
143
+ safeCloseChannel ( )
124
144
return false
125
145
}
126
146
}
127
147
128
148
onMounted ( setupChannel )
129
149
130
- onBeforeUnmount ( ( ) => {
131
- if ( channel . value ) {
132
- channel . value . close ( )
133
- channel . value = null
134
- }
135
- } )
150
+ onBeforeUnmount ( safeCloseChannel )
136
151
137
152
return {
138
153
isSupported,
0 commit comments