From e563ad0a670f5a633bb334e78d7f24d3fc23f6a0 Mon Sep 17 00:00:00 2001 From: Krypto Pank Date: Thu, 17 Feb 2022 11:06:30 +0200 Subject: [PATCH] Remove Promise from `add(events:)` functions #3957 --- .../Coordinators/EventSourceCoordinator.swift | 3 +- .../EventSourceCoordinatorForActivities.swift | 13 ++------ .../Models/EventsActivityDataStore.swift | 29 ++++------------- .../Models/EventsDataStore.swift | 32 ++++--------------- .../FakeEventsDataStore.swift | 4 +-- 5 files changed, 19 insertions(+), 62 deletions(-) diff --git a/AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinator.swift b/AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinator.swift index 657d3b34c..7480dd9c3 100644 --- a/AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinator.swift +++ b/AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinator.swift @@ -121,9 +121,8 @@ extension EventSourceCoordinator.functional { result.compactMap { Self.convertEventToDatabaseObject($0, filterParam: filterParam, eventOrigin: eventOrigin, contractAddress: contractAddress, server: tokenServer) } - }).then(on: queue, { events -> Promise in + }).done(on: .main, { events in eventsDataStore.add(events: events, forTokenContract: contractAddress) - }).done(on: queue, { _ in seal.fulfill(()) }).catch(on: queue, { e in error(value: e, rpcServer: tokenServer, address: contractAddress) diff --git a/AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinatorForActivities.swift b/AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinatorForActivities.swift index 7697debd1..bc53771b1 100644 --- a/AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinatorForActivities.swift +++ b/AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinatorForActivities.swift @@ -140,18 +140,11 @@ extension EventSourceCoordinatorForActivities.functional { return when(resolved: promises).map(on: queue, { values -> [EventActivityInstance] in values.compactMap { $0.optionalValue }.compactMap { $0 } }) - }).then(on: queue, { events -> Promise in - if events.isEmpty { - return .value(()) - } else { - return eventsDataStore.add(events: events, forTokenContract: tokenContract).then(on: queue, { _ -> Promise in - return .value(()) - }) - } - }).done { _ in + }).done(on: .main, { events in + eventsDataStore.add(events: events, forTokenContract: tokenContract) seal.fulfill(()) // swiftlint:disable closure_end_indentation - }.catch { e in + }).catch { e in // swiftlint:enable closure_end_indentation error(value: e, rpcServer: server, address: tokenContract) seal.reject(e) diff --git a/AlphaWallet/TokenScriptClient/Models/EventsActivityDataStore.swift b/AlphaWallet/TokenScriptClient/Models/EventsActivityDataStore.swift index e9e847950..90c082ffd 100644 --- a/AlphaWallet/TokenScriptClient/Models/EventsActivityDataStore.swift +++ b/AlphaWallet/TokenScriptClient/Models/EventsActivityDataStore.swift @@ -10,7 +10,7 @@ protocol EventsActivityDataStoreProtocol { func getRecentEventsSortedByBlockNumber(forContract contract: AlphaWallet.Address, server: RPCServer, eventName: String, interpolatedFilter: String) -> Results func getMatchingEventsSortedByBlockNumber(forContract contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, eventName: String) -> Promise - func add(events: [EventActivityInstance], forTokenContract contract: AlphaWallet.Address) -> Promise + func add(events: [EventActivityInstance], forTokenContract contract: AlphaWallet.Address) } class EventsActivityDataStore: EventsActivityDataStoreProtocol { @@ -83,28 +83,11 @@ class EventsActivityDataStore: EventsActivityDataStoreProtocol { } } - func add(events: [EventActivityInstance], forTokenContract contract: AlphaWallet.Address) -> Promise { - return Promise { seal in - DispatchQueue.main.async { [weak self] in - guard let strongSelf = self else { return seal.reject(PMKError.cancelled) } + func add(events: [EventActivityInstance], forTokenContract contract: AlphaWallet.Address) { + let eventsToSave = events.map { EventActivity(value: $0) } - if events.isEmpty { - seal.fulfill(()) - } else { - let realm = strongSelf.realm - let eventsToSave = events.map { EventActivity(value: $0) } - - do { - realm.beginWrite() - realm.add(eventsToSave, update: .all) - try realm.commitWrite() - - seal.fulfill(()) - } catch { - seal.reject(error) - } - } - } - } + realm.beginWrite() + realm.add(eventsToSave, update: .all) + try? realm.commitWrite() } } diff --git a/AlphaWallet/TokenScriptClient/Models/EventsDataStore.swift b/AlphaWallet/TokenScriptClient/Models/EventsDataStore.swift index e99085040..fb749cbc6 100644 --- a/AlphaWallet/TokenScriptClient/Models/EventsDataStore.swift +++ b/AlphaWallet/TokenScriptClient/Models/EventsDataStore.swift @@ -6,7 +6,7 @@ import PromiseKit protocol EventsDataStoreProtocol { func getLastMatchingEventSortedByBlockNumber(forContract contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, eventName: String) -> Promise - func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) -> Promise + func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) func deleteEvents(forTokenContract contract: AlphaWallet.Address) func getMatchingEvent(forContract contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, eventName: String, filterName: String, filterValue: String) -> EventInstance? func subscribe(_ subscribe: @escaping (_ contract: AlphaWallet.Address) -> Void) @@ -75,30 +75,12 @@ class EventsDataStore: EventsDataStoreProtocol { } } - func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) -> Promise { - return Promise { seal in - DispatchQueue.main.async { [weak self] in - guard let strongSelf = self else { return seal.reject(PMKError.cancelled) } - - if events.isEmpty { - return seal.fulfill(()) - } else { - do { - let realm = strongSelf.realm - let eventsToSave = events.map { EventInstance(event: $0) } + func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) { + let eventsToSave = events.map { EventInstance(event: $0) } - realm.beginWrite() - realm.add(eventsToSave, update: .all) - try realm.commitWrite() - - seal.fulfill(()) - } catch { - seal.reject(error) - } - - strongSelf.triggerSubscribers(forContract: contract) - } - } - } + realm.beginWrite() + realm.add(eventsToSave, update: .all) + try? realm.commitWrite() + triggerSubscribers(forContract: contract) } } diff --git a/AlphaWalletTests/TokenScriptClient/FakeEventsDataStore.swift b/AlphaWalletTests/TokenScriptClient/FakeEventsDataStore.swift index 5a96afe5b..7063006cb 100644 --- a/AlphaWalletTests/TokenScriptClient/FakeEventsDataStore.swift +++ b/AlphaWalletTests/TokenScriptClient/FakeEventsDataStore.swift @@ -10,8 +10,8 @@ class FakeEventsDataStore: EventsDataStoreProtocol { return .value(nil) } - func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) -> Promise { - return .init() + func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) { + //no-op } func deleteEvents(forTokenContract contract: AlphaWallet.Address) {