Skip to content

Commit

Permalink
simplify SessionRequestsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Jan 31, 2024
1 parent 6300045 commit bf777cd
Showing 1 changed file with 3 additions and 27 deletions.
30 changes: 3 additions & 27 deletions Sources/WalletConnectSign/Sign/SessionRequestsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyCancellable>()
private var lastEmitDate: Date?
private var emitRequestSubject = PassthroughSubject<Void, Never>()

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)
}
}
}

0 comments on commit bf777cd

Please sign in to comment.