From 19b2c6d5acad43c5704739abfa8ab6e178310a04 Mon Sep 17 00:00:00 2001 From: CoNET Project Date: Mon, 8 Apr 2024 08:11:41 -0700 Subject: [PATCH] added hooks --- src/API/index.ts | 5 ++--- src/API/platform.ts | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/API/index.ts b/src/API/index.ts index d1240c4..ce88fc8 100644 --- a/src/API/index.ts +++ b/src/API/index.ts @@ -231,11 +231,11 @@ export const postMessage = (cmd: WorkerCommand, close: boolean, resolve: any, C channel.close() } -export const faucet: () => Promise < StartWorkerResolveForAPI > = () => { +export const faucet: (walletAddr: string) => Promise < StartWorkerResolveForAPI > = (walletAddr) => { return new Promise( resolve => { const cmd: WorkerCommand = { cmd: 'getFaucet', - data: [], + data: [walletAddr], uuid: v4() } return postMessage (cmd, true, resolve) @@ -353,7 +353,6 @@ export const stopLiveness: () => Promise < StartWorkerResolveForAPI > = () => { }) } - export const isLivenessRunning: (callback: (err: string, data: string[]) => void) => Promise < StartWorkerResolveForAPI > = (callback) => { return new Promise(resolve => { diff --git a/src/API/platform.ts b/src/API/platform.ts index 46714e7..0db97e1 100644 --- a/src/API/platform.ts +++ b/src/API/platform.ts @@ -44,7 +44,7 @@ const beforeunload = (event: BeforeUnloadEvent) => { return true } -type command = 'profileVer' +type command = 'profileVer'|'assets' interface channelWroker { cmd: command, data: any[] @@ -58,7 +58,12 @@ export const listeningVersionHook = (profileVerHook: React.Dispatch profileVerChannelListening(e, profileVerHook)) } -const profileVerChannelListening = (e: MessageEvent, profileVerHook: React.Dispatch>) => { +export const listeningAssetsHook = (assetsHook: React.Dispatch>) => { + const profileVerChannel = new BroadcastChannel(channelWrokerListenName) + profileVerChannel.addEventListener('message', e => profileVerChannelListening(e, null, assetsHook)) +} + +const profileVerChannelListening = (e: MessageEvent, profileVerHook: React.Dispatch>|null = null, assetsHook:React.Dispatch>|null = null) => { let cmd: channelWroker try { cmd = JSON.parse(e.data) @@ -72,10 +77,17 @@ const profileVerChannelListening = (e: MessageEvent, profileVerHook: React. if (profileVerHook) { return profileVerHook(cmd.data[0]) } - return console.log('profileVerChannelListening Error! profileVerHook === null', `profileVer from backend [${cmd.data}]`) + return console.log('profileVerChannelListening profileVerHook === null', `profileVer from backend [${cmd.data}]`) + } + + case 'assets': { + if (assetsHook) { + return assetsHook(cmd.data[0]) + } + return console.log('profileVerChannelListening assetsHook === null', `profileVer from backend [${cmd.data}]`) } default : { - return console.log(`profileVerChannelListening unknow comd [${ cmd.cmd }] from backend [${ cmd.data }]`) + return console.log(`profileVerChannelListening unknow command [${ cmd.cmd }] from backend [${ cmd.data }]`) } } }