Remove Promise from `add(events:)` functions #3957

pull/3958/head
Krypto Pank 3 years ago
parent dd0942795e
commit e563ad0a67
  1. 3
      AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinator.swift
  2. 13
      AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinatorForActivities.swift
  3. 29
      AlphaWallet/TokenScriptClient/Models/EventsActivityDataStore.swift
  4. 32
      AlphaWallet/TokenScriptClient/Models/EventsDataStore.swift
  5. 4
      AlphaWalletTests/TokenScriptClient/FakeEventsDataStore.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<Void> 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)

@ -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<Void> in
if events.isEmpty {
return .value(())
} else {
return eventsDataStore.add(events: events, forTokenContract: tokenContract).then(on: queue, { _ -> Promise<Void> 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)

@ -10,7 +10,7 @@ protocol EventsActivityDataStoreProtocol {
func getRecentEventsSortedByBlockNumber(forContract contract: AlphaWallet.Address, server: RPCServer, eventName: String, interpolatedFilter: String) -> Results<EventActivity>
func getMatchingEventsSortedByBlockNumber(forContract contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, eventName: String) -> Promise<EventActivityInstance?>
func add(events: [EventActivityInstance], forTokenContract contract: AlphaWallet.Address) -> Promise<Void>
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<Void> {
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()
}
}

@ -6,7 +6,7 @@ import PromiseKit
protocol EventsDataStoreProtocol {
func getLastMatchingEventSortedByBlockNumber(forContract contract: AlphaWallet.Address, tokenContract: AlphaWallet.Address, server: RPCServer, eventName: String) -> Promise<EventInstanceValue?>
func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) -> Promise<Void>
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<Void> {
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)
}
}

@ -10,8 +10,8 @@ class FakeEventsDataStore: EventsDataStoreProtocol {
return .value(nil)
}
func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) -> Promise<Void> {
return .init()
func add(events: [EventInstanceValue], forTokenContract contract: AlphaWallet.Address) {
//no-op
}
func deleteEvents(forTokenContract contract: AlphaWallet.Address) {

Loading…
Cancel
Save