From 3eeefe1accd7a55b962ff1cd42525fa118478510 Mon Sep 17 00:00:00 2001 From: Milad Raeisi Date: Tue, 5 Nov 2024 23:37:42 +0400 Subject: [PATCH] Removed type annotations and unnecessary whitespace in SubscriptionService --- src/app/services/subscription.service.ts | 88 ++++++++---------------- 1 file changed, 27 insertions(+), 61 deletions(-) diff --git a/src/app/services/subscription.service.ts b/src/app/services/subscription.service.ts index c9e040e..eb6e812 100644 --- a/src/app/services/subscription.service.ts +++ b/src/app/services/subscription.service.ts @@ -14,9 +14,9 @@ interface Subscription { providedIn: 'root', }) export class SubscriptionService { - private subscriptions: Map = new Map(); + private subscriptions = new Map(); private activeRelays: string[] = []; - private pendingSubscriptions: Map = new Map(); + private pendingSubscriptions = new Map(); private subscriptionsSubject = new BehaviorSubject>(new Map()); public subscriptions$: Observable> = this.subscriptionsSubject.asObservable(); @@ -26,14 +26,13 @@ export class SubscriptionService { private queueInterval = 3000; private maxSubscriptionsPerBatch = 5; private debounceInterval = 5000; - private lastActionTimestamp: Map = new Map(); + private lastActionTimestamp = new Map(); constructor(private relayService: RelayService) { this.connectToAllRelays(); this.processSubscriptionQueue(); } - public addSubscriptions(filters: Filter[], callback: (event: NostrEvent) => void, p0?: () => void): string { const existingSubscription = this.findExistingSubscription(filters); @@ -43,53 +42,41 @@ export class SubscriptionService { } const subscriptionId = uuidv4(); - const now = Date.now(); - this.lastActionTimestamp.set(subscriptionId, now); + this.lastActionTimestamp.set(subscriptionId, Date.now()); const subscription: Subscription = { filter: filters, callbacks: [callback], id: subscriptionId }; this.subscriptions.set(subscription.id, subscription); this.subscriptionsSubject.next(this.subscriptions); - - this.addToQueue(subscription); return subscription.id; } - private findExistingSubscription(filters: Filter[]): Subscription | undefined { - for (let subscription of this.subscriptions.values()) { - if (JSON.stringify(subscription.filter) === JSON.stringify(filters)) { - return subscription; - } - } - return undefined; + return Array.from(this.subscriptions.values()).find(subscription => + JSON.stringify(subscription.filter) === JSON.stringify(filters) + ); } - public removeSubscriptionById(subscriptionId: string): void { const now = Date.now(); const lastActionTime = this.lastActionTimestamp.get(subscriptionId); if (lastActionTime && now - lastActionTime < this.debounceInterval) { - // console.warn('Skipping removal due to rapid toggling'); - return; + return; // Skip rapid toggling } - if (this.subscriptions.has(subscriptionId)) { - this.subscriptions.delete(subscriptionId); + if (this.subscriptions.delete(subscriptionId)) { this.subscriptionsSubject.next(this.subscriptions); } this.lastActionTimestamp.set(subscriptionId, now); } - private addToQueue(subscription: Subscription): void { this.subscriptionQueue.push(subscription); } - private processSubscriptionQueue(): void { if (this.isProcessingQueue) return; this.isProcessingQueue = true; @@ -97,75 +84,54 @@ export class SubscriptionService { const processQueue = () => { if (this.subscriptionQueue.length > 0) { const subscriptionsBatch = this.subscriptionQueue.splice(0, this.maxSubscriptionsPerBatch); - - subscriptionsBatch.forEach((subscription) => { - try { - this.subscribeToRelays(subscription.filter, subscription); - } catch (error) { - console.error('Failed to subscribe:', error); - } - }); + subscriptionsBatch.forEach(subscription => this.subscribeToRelays(subscription.filter, subscription)); } }; setInterval(processQueue, this.queueInterval); } - - private subscribeToRelays(filters: Filter[], subscription: Subscription): void { if (this.activeRelays.length === 0) { this.pendingSubscriptions.set(subscription.id, subscription); return; } - this.relayService - .ensureConnectedRelays() - .then(() => { - const connectedRelays = this.relayService.getConnectedRelays(); - this.relayService.getPool().subscribeMany(connectedRelays, filters, { - onevent: (event: NostrEvent) => { - subscription.callbacks.forEach((callback) => callback(event)); - }, - onclose: () => { - console.log('Subscription closed'); - }, - }); - }) - .catch((error) => { - console.error('Error subscribing to relays:', error); + this.relayService.ensureConnectedRelays().then(() => { + const connectedRelays = this.relayService.getConnectedRelays(); + this.relayService.getPool().subscribeMany(connectedRelays, filters, { + onevent: (event: NostrEvent) => { + subscription.callbacks.forEach(callback => callback(event)); + }, + onclose: () => console.log('Subscription closed'), }); + }).catch(error => { + console.error('Error subscribing to relays:', error); + }); } - private subscribeToAllRelays(): void { - this.pendingSubscriptions.forEach((subscription) => { + this.pendingSubscriptions.forEach(subscription => { this.subscribeToRelays(subscription.filter, subscription); }); this.pendingSubscriptions.clear(); } - private connectToAllRelays(): void { - this.relayService - .ensureConnectedRelays() - .then(() => { - this.activeRelays = this.relayService.getConnectedRelays(); - this.subscribeToAllRelays(); - }) - .catch((error) => { - console.error('Error connecting to relays:', error); - }); + this.relayService.ensureConnectedRelays().then(() => { + this.activeRelays = this.relayService.getConnectedRelays(); + this.subscribeToAllRelays(); + }).catch(error => { + console.error('Error connecting to relays:', error); + }); } - public clearAllSubscriptions(): void { this.subscriptions.clear(); this.pendingSubscriptions.clear(); this.subscriptionsSubject.next(new Map()); } - public getSubscriptions(): Subscription[] { return Array.from(this.subscriptions.values()); }