-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
58 lines (46 loc) · 1.44 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Foundation
import DXFeedFramework
// Empty Listener with handler
class Listener: DXEventListener, Hashable {
static func == (lhs: Listener, rhs: Listener) -> Bool {
lhs === rhs
}
func hash(into hasher: inout Hasher) {
hasher.combine("\(self):\(stringReference(self))")
}
var callback: ([MarketEvent]) -> Void = { _ in }
func receiveEvents(_ events: [MarketEvent]) {
self.callback(events)
}
init(overrides: (Listener) -> Listener) {
_ = overrides(self)
}
}
let file: String = ""
let types = [Quote.self]
let symbols = "AAPL"
var eventCounter = 0
// Create endpoint specifically for file parsing.
let endpoint = try DXEndpoint.create(.streamFeed)
let feed = endpoint.getFeed()
// Subscribe to a specified event and symbol.
var sub = try feed?.createSubscription(types)
let listener = Listener { anonymCl in
anonymCl.callback = { events in
events.forEach { event in
eventCounter += 1
print("\(eventCounter): \(event)")
}
}
return anonymCl
}
try sub?.add(listener: listener)
// Add symbols.
try sub?.addSymbols(symbols)
// Connect endpoint to a file.
try endpoint.connect("file:\(file)[speed=max]")
// Wait until file is completely parsed.
try endpoint.awaitNotConnected()
// Close endpoint when we're done.
// This method will gracefully close endpoint, waiting while data processing completes.
try endpoint.closeAndAwaitTermination()