-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContents.swift
65 lines (53 loc) · 2.19 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
59
60
61
62
63
64
65
import Cocoa
import PlaygroundSupport
import DXFeedFramework
class Listener: DXInstrumentProfileUpdateListener, Hashable {
// Data model to keep all instrument profiles mapped by their ticker symbol
private var profiles = [String: InstrumentProfile]()
let collector: DXInstrumentProfileCollector
init(_ collector: DXInstrumentProfileCollector) {
self.collector = collector
}
func instrumentProfilesUpdated(_ instruments: [DXFeedFramework.InstrumentProfile]) {
// We can observe REMOVED elements - need to add necessary filtering
// (1) We can either process instrument profile updates manually
instruments.forEach { ipf in
if ipf.getIpfType() == .removed {
// Profile was removed - remove it from our data model
self.profiles.removeValue(forKey: ipf.symbol)
} else {
// Profile was updated - collector only notifies us if profile was changed
self.profiles[ipf.symbol] = ipf
}
}
print(
"""
Instrument Profiles:
Total number of profiles (1): \(self.profiles.count)
Last modified: \(
(try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: collector.getLastUpdateTime())) ?? ""
)
"""
)
}
static func == (lhs: Listener, rhs: Listener) -> Bool {
return lhs === rhs
}
func hash(into hasher: inout Hasher) {
hasher.combine("\(self):\(stringReference(self))")
}
}
// An sample that demonstrates a subscription using InstrumentProfile.
let defaultIpfUrl = "https://demo:[email protected]/ipf"
let collector = try DXInstrumentProfileCollector()
let connection = try DXInstrumentProfileConnection(defaultIpfUrl, collector)
// Update period can be used to re-read IPF files, not needed for services supporting IPF "live-update"
try connection.setUpdatePeriod(60000)
try connection.start()
// It is possible to add listener after connection is started - updates will not be missed in this case
let listener = Listener(collector)
try collector.add(listener: listener)
// infinity execution
PlaygroundPage.current.needsIndefiniteExecution = true
// to finish execution run this line
PlaygroundPage.current.finishExecution()