From bf777cda1d2d1b61d3686781bdceccb5f10ab5c2 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 31 Jan 2024 13:39:12 +0100 Subject: [PATCH] simplify SessionRequestsProvider --- .../Sign/SessionRequestsProvider.swift | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/Sources/WalletConnectSign/Sign/SessionRequestsProvider.swift b/Sources/WalletConnectSign/Sign/SessionRequestsProvider.swift index 0b986d73a..7838b65b7 100644 --- a/Sources/WalletConnectSign/Sign/SessionRequestsProvider.swift +++ b/Sources/WalletConnectSign/Sign/SessionRequestsProvider.swift @@ -4,41 +4,17 @@ import Foundation class SessionRequestsProvider { private let historyService: HistoryService private var sessionRequestPublisherSubject = PassthroughSubject<(request: Request, context: VerifyContext?), Never>() - private var cancellables = Set() - private var lastEmitDate: Date? - private var emitRequestSubject = PassthroughSubject() - public var sessionRequestPublisher: AnyPublisher<(request: Request, context: VerifyContext?), Never> { sessionRequestPublisherSubject.eraseToAnyPublisher() } init(historyService: HistoryService) { self.historyService = historyService - setupEmitRequestHandling() - } - - private func setupEmitRequestHandling() { - emitRequestSubject - .sink { [weak self] _ in - guard let self = self else { return } - let now = Date() - if let lastEmitDate = self.lastEmitDate, now.timeIntervalSince(lastEmitDate) < 1 { - // If the last emit was less than 1 second ago, ignore this request. - return - } - - // Update the last emit time to now. - self.lastEmitDate = now - - // Fetch the oldest request and emit it. - if let oldestRequest = self.historyService.getPendingRequestsSortedByTimestamp().first { - self.sessionRequestPublisherSubject.send(oldestRequest) - } - } - .store(in: &cancellables) } func emitRequestIfPending() { - emitRequestSubject.send(()) + if let oldestRequest = self.historyService.getPendingRequestsSortedByTimestamp().first { + self.sessionRequestPublisherSubject.send(oldestRequest) + } } }