put subscription to event in a list to clean them properly

This commit is contained in:
AnisHADJARAB 2024-11-06 15:31:11 +00:00
parent 4a59424d05
commit 41b3ba552f
6 changed files with 36 additions and 47 deletions

View File

@ -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<void> {
}
}
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)
}

View File

@ -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<void> {
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')

View File

@ -88,7 +88,7 @@ async function init(): Promise<void> {
if (pairingAddress) {
setTimeout(async () => await services.sendPairingTx(pairingAddress), 2000);
}
await navigate('process');
await navigate('home');
}
}, 200);
} catch (error) {

View File

@ -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() {}

View File

@ -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();

View File

@ -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();
});
}
@ -150,3 +151,25 @@ async function onOkButtonClick() {
const addressInput = (document.getElementById('addressInput') as HTMLInputElement).value;
await service.sendPairingTx(addressInput);
}
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);
}
};