TSK-1499 Old safari not support BroadcastChannel (#3375)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2023-06-07 12:43:54 +06:00 committed by GitHub
parent d1a29613a4
commit bdbf2c650d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,7 +29,7 @@ function syncDrafts (): void {
// #region Broadcast
const bc = new BroadcastChannel(activeDraftsKey)
const bc = BroadcastChannel !== undefined ? new BroadcastChannel(activeDraftsKey) : undefined
type BroadcastMessage = BroadcastGetMessage | BroadcastGetResp | BroadcastAddMessage | BroadcastRemoveMessage
@ -53,7 +53,7 @@ interface BroadcastAddMessage {
}
function sendMessage (req: BroadcastMessage): void {
bc.postMessage(req)
bc?.postMessage(req)
}
function syncActive (): void {
@ -66,23 +66,25 @@ function loadActiveDrafts (): void {
sendMessage({ type: 'get_all' })
}
bc.onmessage = (e: MessageEvent<BroadcastMessage>) => {
if (e.data.type === 'get_all') {
sendMessage({ type: 'get_all_response', value: Array.from(activeDrafts.values()) })
}
if (e.data.type === 'get_all_response') {
for (const val of e.data.value) {
activeDrafts.add(val)
if (bc !== undefined) {
bc.onmessage = (e: MessageEvent<BroadcastMessage>) => {
if (e.data.type === 'get_all') {
sendMessage({ type: 'get_all_response', value: Array.from(activeDrafts.values()) })
}
if (e.data.type === 'get_all_response') {
for (const val of e.data.value) {
activeDrafts.add(val)
}
syncActive()
}
if (e.data.type === 'add') {
activeDrafts.add(e.data.value)
syncActive()
}
if (e.data.type === 'remove') {
activeDrafts.delete(e.data.value)
syncActive()
}
syncActive()
}
if (e.data.type === 'add') {
activeDrafts.add(e.data.value)
syncActive()
}
if (e.data.type === 'remove') {
activeDrafts.delete(e.data.value)
syncActive()
}
}