From 09c3e3ab998a18d371afd8611e748bc4cf8271e6 Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Fri, 9 Aug 2024 03:22:54 +0300 Subject: [PATCH] Improve typings on MessagesRef entry store --- src/contexts/Kaspa.tsx | 18 ++++++++++++------ src/pages/Wallet/Settings.tsx | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/contexts/Kaspa.tsx b/src/contexts/Kaspa.tsx index 5b9695d..c52e723 100644 --- a/src/contexts/Kaspa.tsx +++ b/src/contexts/Kaspa.tsx @@ -13,6 +13,12 @@ export interface IKaspa { provider: string } +interface MessageEntry { + resolve: (value: ResponseMappings[M]) => void; + reject: (reason?: any) => void; + message: Request; +} + export const defaultState: IKaspa = { status: Status.Uninitialized, connected: false, @@ -34,7 +40,7 @@ export function KaspaProvider ({ children }: { const [ kaspa, setState ] = useState(defaultState) const connectionRef = useRef(null) - const messagesRef = useRef(new Map()) // TODO: Improve typing + const messagesRef = useRef(new Map>()) const nonceRef = useRef(0) const getConnection = useCallback(() => { @@ -44,7 +50,7 @@ export function KaspaProvider ({ children }: { connection.onMessage.addListener(async (message: Response | Event) => { if (!isEvent(message)) { - const [ resolve, reject ] = messagesRef.current.get(message.id) + const { resolve, reject } = messagesRef.current.get(message.id)! if (!message.error) { resolve(message.result) @@ -87,9 +93,9 @@ export function KaspaProvider ({ children }: { connectionRef.current = null - messagesRef.current.forEach(message => { - getConnection().postMessage(message[2]) - }) + for (const entry of messagesRef.current.values()) { + getConnection().postMessage(entry.message) + } }) connectionRef.current = connection @@ -105,7 +111,7 @@ export function KaspaProvider ({ children }: { } return new Promise((resolve, reject) => { - messagesRef.current.set(message.id, [ resolve, reject, message ]) + messagesRef.current.set(message.id, { resolve, reject, message }) getConnection().postMessage(message) }) diff --git a/src/pages/Wallet/Settings.tsx b/src/pages/Wallet/Settings.tsx index 3bb3491..71a1fe1 100644 --- a/src/pages/Wallet/Settings.tsx +++ b/src/pages/Wallet/Settings.tsx @@ -1,4 +1,4 @@ -import { i18n, runtime } from "webextension-polyfill" +import { runtime, i18n } from "webextension-polyfill" import { SettingsIcon } from "lucide-react" import GeneralTab from "@/pages/Wallet/Settings/General" import WalletTab from "@/pages/Wallet/Settings/Wallet"