-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DXFeedLiveIpfSample to playground sample
- Loading branch information
Showing
9 changed files
with
79 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Samples/Playgrounds/DXFeedLiveIpfSample.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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 | ||
// See javadoc for InstrumentProfileCollector for more details | ||
|
||
// (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() |
4 changes: 4 additions & 0 deletions
4
Samples/Playgrounds/DXFeedLiveIpfSample.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters