From 41b3ba552f3c08a444ebcb750f192b3dd2d30845 Mon Sep 17 00:00:00 2001 From: AnisHADJARAB Date: Wed, 6 Nov 2024 15:31:11 +0000 Subject: [PATCH] put subscription to event in a list to clean them properly --- src/components/header/header.ts | 21 ++++++--------------- src/pages/home/home.ts | 8 ++++---- src/router.ts | 2 +- src/services/modal.service.ts | 1 - src/services/service.ts | 24 ------------------------ src/utils/sp-address.utils.ts | 27 +++++++++++++++++++++++++-- 6 files changed, 36 insertions(+), 47 deletions(-) diff --git a/src/components/header/header.ts b/src/components/header/header.ts index 16bef79..c6e11ee 100644 --- a/src/components/header/header.ts +++ b/src/components/header/header.ts @@ -40,7 +40,8 @@ export async function initHeader() { if (currentRoute === 'home') { hideSomeFunctionnalities(); } else { - launchNotificationWorker() + fetchNotifications() + setInterval(fetchNotifications, 2 * 60 * 1000); } } @@ -80,19 +81,9 @@ async function setNotification(notifications: INotification[]): Promise { } } -function launchNotificationWorker() { - if (window.Worker) { - // Initialize the worker - const worker = new Worker('/src/workers/notification.worker.ts', { type: 'module' }); - - // Listen for messages from the worker - worker.addEventListener('message', (event) => { - const data = event.data; - console.log('Received data from worker:', data); - setNotification(data); - }); -} else { - console.error('Your browser doesn\'t support web workers.'); -} +async function fetchNotifications() { + const service = await Services.getInstance() + const data = service.getNotifications(); + setNotification(data) } diff --git a/src/pages/home/home.ts b/src/pages/home/home.ts index 5a7d1dc..2758495 100644 --- a/src/pages/home/home.ts +++ b/src/pages/home/home.ts @@ -2,7 +2,7 @@ import { Html5QrcodeScanner } from 'html5-qrcode'; import Routing from '../../services/modal.service'; import Services from '../../services/service'; import { addSubscription } from '../../utils/subscription.utils'; -import { displayEmojis } from '../../utils/sp-address.utils'; +import { displayEmojis, generateQRCode } from '../../utils/sp-address.utils'; let resultContainer = document.getElementById('qr-reader-results'); let lastResult: any, @@ -11,18 +11,18 @@ let lastResult: any, export async function initHomePage(): Promise { console.log('INIT'); document.querySelectorAll('.tab').forEach((tab) => { - tab.addEventListener('click', () => { + addSubscription(tab, 'click', () => { document.querySelectorAll('.tab').forEach((t) => t.classList.remove('active')); tab.classList.add('active'); document.querySelectorAll('.tab-content').forEach((content) => content.classList.remove('active')); document.getElementById(tab.getAttribute('data-tab') as string)?.classList.add('active'); - }); + }) }); const service = await Services.getInstance(); const spAddress = await service.getDeviceAddress() - service.generateQRCode(spAddress) + generateQRCode(spAddress) displayEmojis(spAddress) const notifBell = document.getElementById('notification-bell') diff --git a/src/router.ts b/src/router.ts index b9b5eb9..95e60d7 100644 --- a/src/router.ts +++ b/src/router.ts @@ -88,7 +88,7 @@ async function init(): Promise { if (pairingAddress) { setTimeout(async () => await services.sendPairingTx(pairingAddress), 2000); } - await navigate('process'); + await navigate('home'); } }, 200); } catch (error) { diff --git a/src/services/modal.service.ts b/src/services/modal.service.ts index 5752652..b4ddbfc 100644 --- a/src/services/modal.service.ts +++ b/src/services/modal.service.ts @@ -9,7 +9,6 @@ import { addressToEmoji } from '../utils/sp-address.utils'; export default class ModalService { private static instance: ModalService; - private sdkClient: any; private currentPrd: any; private currentOutpoint?: string; private constructor() {} diff --git a/src/services/service.ts b/src/services/service.ts index 41063e7..9e6a108 100644 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -3,11 +3,9 @@ import { INotification } from '~/models/notification.model'; import { IProcess } from '~/models/process.model'; // import Database from './database'; import { WebSocketClient } from '../websockets'; -import QRCode from 'qrcode'; import { ApiReturn, Member } from '../../dist/pkg/sdk_client'; import ModalService from './modal.service'; import { navigate } from '../router'; -import { copyToClipboard } from '../utils/sp-address.utils'; type ProcessesCache = { [key: string]: any; @@ -67,28 +65,6 @@ export default class Services { } } - generateQRCode = async (text: string) => { - try { - const container = document.getElementById('containerId'); - const currentUrl = 'https://' + window.location.host; - const url = await QRCode.toDataURL(currentUrl + '?sp_address=' + text); - const qrCode = container?.querySelector('.qr-code img'); - qrCode?.setAttribute('src', url); - - //Generate Address CopyBtn - const address = container?.querySelector('.sp-address-btn'); - if (address) { - address.textContent = 'Copy address'; - } - const copyBtn = document.getElementById('copyBtn'); - if (copyBtn) { - copyBtn.addEventListener('click', () => copyToClipboard(currentUrl + '?sp_address=' + text)); - } - } catch (err) { - console.error(err); - } - }; - public isPaired(): boolean | undefined { try { return this.sdkClient.is_linking(); diff --git a/src/utils/sp-address.utils.ts b/src/utils/sp-address.utils.ts index 1188495..10dcba9 100644 --- a/src/utils/sp-address.utils.ts +++ b/src/utils/sp-address.utils.ts @@ -1,5 +1,6 @@ import Services from "../services/service"; import { addSubscription } from "./subscription.utils"; +import QRCode from 'qrcode'; //Copy Address export async function copyToClipboard(fullAddress: string) { @@ -139,7 +140,7 @@ async function emojisPairingRequest() { }) if (okButton) { - okButton.addEventListener('click', () => { + addSubscription(okButton, 'click', () => { onOkButtonClick(); }); } @@ -149,4 +150,26 @@ async function onOkButtonClick() { const service = await Services.getInstance() const addressInput = (document.getElementById('addressInput') as HTMLInputElement).value; await service.sendPairingTx(addressInput); - } \ No newline at end of file + } + +export async function generateQRCode(spAddress: string) { + try { + const container = document.getElementById('containerId'); + const currentUrl = 'https://' + window.location.host; + const url = await QRCode.toDataURL(currentUrl + '?sp_address=' + spAddress); + const qrCode = container?.querySelector('.qr-code img'); + qrCode?.setAttribute('src', url); + + //Generate Address CopyBtn + const address = container?.querySelector('.sp-address-btn'); + if (address) { + address.textContent = 'Copy address'; + } + const copyBtn = document.getElementById('copyBtn'); + if (copyBtn) { + addSubscription(copyBtn, 'click', () => copyToClipboard(currentUrl + '?sp_address=' + spAddress)) + } + } catch (err) { + console.error(err); + } + }; \ No newline at end of file