From 1961f4744ca0db4666a2df4335c768ed86fc4e2f Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 2 Jun 2023 14:26:28 -0300 Subject: remove deprecated notifications, implement isOffline --- packages/taler-wallet-webextension/src/platform/api.ts | 7 +++++++ .../taler-wallet-webextension/src/platform/chrome.ts | 18 ++++++++++++++++++ packages/taler-wallet-webextension/src/platform/dev.ts | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+) (limited to 'packages/taler-wallet-webextension/src/platform') diff --git a/packages/taler-wallet-webextension/src/platform/api.ts b/packages/taler-wallet-webextension/src/platform/api.ts index f985cc4f8..e3e97264d 100644 --- a/packages/taler-wallet-webextension/src/platform/api.ts +++ b/packages/taler-wallet-webextension/src/platform/api.ts @@ -286,4 +286,11 @@ export interface ForegroundPlatformAPI { listenToWalletBackground( listener: (message: MessageFromBackend) => void, ): () => void; + + /** + * Notify when platform went offline + */ + listenNetworkConnectionState( + listener: (state: "on" | "off") => void, + ): () => void; } diff --git a/packages/taler-wallet-webextension/src/platform/chrome.ts b/packages/taler-wallet-webextension/src/platform/chrome.ts index 0f6b5fb0d..34057a310 100644 --- a/packages/taler-wallet-webextension/src/platform/chrome.ts +++ b/packages/taler-wallet-webextension/src/platform/chrome.ts @@ -58,6 +58,7 @@ const api: BackgroundPlatformAPI & ForegroundPlatformAPI = { sendMessageToBackground, useServiceWorkerAsBackgroundProcess, keepAlive, + listenNetworkConnectionState, }; export default api; @@ -762,3 +763,20 @@ async function findTalerUriInActiveTab(): Promise { if (!tab || tab.id === undefined) return; return findTalerUriInTab(tab.id); } + +function listenNetworkConnectionState( + notify: (state: "on" | "off") => void, +): () => void { + function notifyOffline() { + notify("off"); + } + function notifyOnline() { + notify("on"); + } + window.addEventListener("offline", notifyOffline); + window.addEventListener("online", notifyOnline); + return () => { + window.removeEventListener("offline", notifyOffline); + window.removeEventListener("online", notifyOnline); + }; +} diff --git a/packages/taler-wallet-webextension/src/platform/dev.ts b/packages/taler-wallet-webextension/src/platform/dev.ts index 005421876..976ac05f5 100644 --- a/packages/taler-wallet-webextension/src/platform/dev.ts +++ b/packages/taler-wallet-webextension/src/platform/dev.ts @@ -34,6 +34,7 @@ const api: BackgroundPlatformAPI & ForegroundPlatformAPI = { keepAlive: (cb: VoidFunction) => cb(), findTalerUriInActiveTab: async () => undefined, findTalerUriInClipboard: async () => undefined, + listenNetworkConnectionState, getPermissionsApi: () => ({ addPermissionsListener: () => undefined, containsHostPermissions: async () => true, @@ -197,3 +198,20 @@ interface IframeMessageCommand { } export default api; + +function listenNetworkConnectionState( + notify: (state: "on" | "off") => void, +): () => void { + function notifyOffline() { + notify("off"); + } + function notifyOnline() { + notify("on"); + } + window.addEventListener("offline", notifyOffline); + window.addEventListener("online", notifyOnline); + return () => { + window.removeEventListener("offline", notifyOffline); + window.removeEventListener("online", notifyOnline); + }; +} -- cgit v1.2.3