Skip to content

Commit 544171e

Browse files
committed
add async
1 parent a95bfb5 commit 544171e

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

Sources/web3swift/Utils/Hooks/NonceMiddleware.swift

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,27 @@ extension Web3.Utils {
2121
public var synchronizationPeriod: TimeInterval = 300.0 // 5 minutes
2222
var lastSyncTime: Date = Date()
2323

24-
public func functionToRun() {
24+
public func functionToRun() async {
2525
guard let w3 = self.web3 else {return}
26-
var allPromises = [Promise<BigUInt>]()
27-
allPromises.reserveCapacity(self.nonceLookups.keys.count)
28-
let knownKeys = Array(self.nonceLookups.keys)
29-
for k in knownKeys {
30-
let promise = w3.eth.getTransactionCountPromise(address: k, onBlock: "latest")
31-
allPromises.append(promise)
32-
}
33-
when(resolved: allPromises).done(on: w3.requestDispatcher.queue) {results in
34-
self.queue.async {
35-
var i = 0
36-
for res in results {
37-
switch res {
38-
case .fulfilled(let newNonce):
39-
let key = knownKeys[i]
40-
self.nonceLookups[key] = newNonce
41-
i = i + 1
42-
default:
43-
i = i + 1
44-
}
26+
27+
let knownKeys = Array(nonceLookups.keys)
28+
29+
await withTaskGroup(of: BigUInt?.self, returning: Void.self) { group -> Void in
30+
31+
knownKeys.forEach { key in
32+
group.addTask {
33+
try? await w3.eth.getTransactionCountPromise(address: key, onBlock: "latest")
4534
}
4635
}
4736

37+
var i = 0
38+
39+
for await value in group {
40+
let key = knownKeys[i]
41+
self.nonceLookups[key] = value
42+
i = i + 1
43+
}
44+
4845
}
4946
}
5047

@@ -98,9 +95,9 @@ extension Web3.Utils {
9895
public func attach(_ web3: web3) {
9996
self.web3 = web3
10097
web3.eventLoop.monitoredUserFunctions.append(self)
101-
let preHook = AssemblyHook(queue: web3.requestDispatcher.queue, function: self.preAssemblyFunction)
98+
let preHook = AssemblyHook(function: self.preAssemblyFunction)
10299
web3.preAssemblyHooks.append(preHook)
103-
let postHook = SubmissionResultHook(queue: web3.requestDispatcher.queue, function: self.postSubmissionFunction)
100+
let postHook = SubmissionResultHook(function: self.postSubmissionFunction)
104101
web3.postSubmissionHooks.append(postHook)
105102
}
106103

Sources/web3swift/Web3/Web3+Eventloop.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension web3.Eventloop {
3939

4040
for prop in self.monitoredUserFunctions {
4141
Task {
42-
prop.functionToRun()
42+
await prop.functionToRun()
4343
}
4444
}
4545
}

Sources/web3swift/Web3/Web3+Protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ extension Networks: Equatable {
8383

8484
public protocol EventLoopRunnableProtocol {
8585
var name: String {get}
86-
func functionToRun()
86+
func functionToRun() async
8787
}

0 commit comments

Comments
 (0)