Skip to content

Commit

Permalink
fix: Service Worker networkChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Mar 21, 2024
1 parent cbdbaf9 commit 0bceda0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import { getAddress, getCurrentAccount } from './lib/account'
import { isLocked } from './lib/password'
import { sleep } from './lib/helpers'
import browser from 'webextension-polyfill'
import { network } from '@/lib/network'

// const browser = window.browser as typeof chrome
browser.runtime.onMessage.addListener(async (msg, sender) => {
if (msg.channel === 'to-bg') {
if (msg.eventName === 'networkChanged') {
network.value = msg.args[0]
console.log('network', network.value)
}
return
}

if (msg.channel === 'inter-extension') {
await sleep(100)
return await exActions[msg.fn](...msg.args)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ref } from 'vue'
import { networks } from 'bitcoinjs-lib'

import storage from './storage'
import { notifyBg } from '@/lib/notify-bg'
import { notifyContent } from '@/lib/notify-content'

export type Network = 'mainnet' | 'testnet'
Expand All @@ -11,6 +12,7 @@ export const network = ref<Network>(await storage.get('network', { defaultValue:
export async function setNetwork(_network: Network) {
network.value = _network
notifyContent('networkChanged')(_network)
notifyBg('networkChanged')(_network)
await storage.set('network', _network)
}

Expand Down
18 changes: 18 additions & 0 deletions src/lib/notify-bg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IS_DEV } from '@/data/config'

interface EmitParams {
args: unknown[]
eventName: string
}

async function emit(params: EmitParams) {
if (IS_DEV) {
return
}

window.browser.runtime.sendMessage({ ...params, channel: 'to-bg' })
}

export function notifyBg(eventName: string) {
return (...args: unknown[]) => emit({ eventName, args })
}

0 comments on commit 0bceda0

Please sign in to comment.