Skip to content

Commit 0e03dbb

Browse files
committed
first commit after the Devcon. Add more options into the event loop
1 parent 23a1315 commit 0e03dbb

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

web3swift/Web3/Classes/Web3+Eventloop.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,21 @@ extension web3.Eventloop {
2929
}
3030
}
3131

32-
@objc func runnable() {
32+
func runnable() {
3333
for prop in self.monitoredProperties {
3434
let queue = prop.queue
3535
let function = prop.calledFunction
3636
queue.async {
3737
function(self.web3)
3838
}
3939
}
40+
41+
for prop in self.monitoredUserFunctions {
42+
let queue = prop.queue
43+
queue.async {
44+
prop.functionToRun()
45+
}
46+
}
4047
}
4148
}
4249

web3swift/Web3/Classes/Web3+Instance.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,31 @@ public class web3: Web3OptionsInheritable {
166166
}
167167

168168
public class Eventloop: Web3OptionsInheritable {
169+
169170
public typealias EventLoopCall = (web3) -> Void
171+
public typealias EventLoopContractCall = (web3contract) -> Void
170172

171173
public struct MonitoredProperty {
172174
public var name: String
173175
public var queue: DispatchQueue
174176
public var calledFunction: EventLoopCall
175177
}
176178

179+
// public struct MonitoredContract {
180+
// public var name: String
181+
// public var queue: DispatchQueue
182+
// public var calledFunction: EventLoopContractCall
183+
// }
184+
177185
var provider:Web3Provider
178186
// weak var web3: web3?
179187
var web3: web3
180188
var timer: RepeatingTimer? = nil
181-
var monitoredProperties: [MonitoredProperty] = [MonitoredProperty]()
189+
190+
public var monitoredProperties: [MonitoredProperty] = [MonitoredProperty]()
191+
// public var monitoredContracts: [MonitoredContract] = [MonitoredContract]()
192+
public var monitoredUserFunctions: [EventLoopRunnableProtocol] = [EventLoopRunnableProtocol]()
193+
182194
public var options: Web3Options {
183195
return self.web3.options
184196
}

web3swift/Web3/Classes/Web3+Protocols.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,9 @@ public enum Networks {
7575
}
7676
}
7777
}
78+
79+
public protocol EventLoopRunnableProtocol {
80+
var name: String {get}
81+
var queue: DispatchQueue {get}
82+
func functionToRun()
83+
}

web3swiftTests/web3swift_Eventloop_Tests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import EthereumAddress
1212
class web3swift_Eventloop_Tests: XCTestCase {
1313

1414
func testBasicEventLoop() {
15-
var ticksToWait = 10
15+
var ticksToWait = 5
1616
let expectation = self.expectation(description: "Waiting")
1717
func getBlockNumber(_ web3: web3) {
1818
do {
@@ -30,9 +30,9 @@ class web3swift_Eventloop_Tests: XCTestCase {
3030
let functionToCall: web3.Eventloop.EventLoopCall = getBlockNumber
3131
let monitoredProperty = web3.Eventloop.MonitoredProperty.init(name: "onNewBlock", queue: web3main.requestDispatcher.queue, calledFunction: functionToCall)
3232
web3main.eventLoop.monitoredProperties.append(monitoredProperty)
33-
web3main.eventLoop.start(1)
33+
web3main.eventLoop.start(5)
3434

35-
waitForExpectations(timeout: 30, handler: nil)
35+
waitForExpectations(timeout: 60, handler: nil)
3636
}
3737

3838
}

0 commit comments

Comments
 (0)