Skip to content

Commit

Permalink
add attach/detach subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Apr 8, 2024
1 parent 6c29dc0 commit a5344a6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
27 changes: 27 additions & 0 deletions DXFeedFramework/Api/DXFeed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import Foundation
public class DXFeed {
/// Feed native wrapper.
private let native: NativeFeed

internal var nativeFeed: NativeFeed {
return native
}

deinit {
}
Expand Down Expand Up @@ -287,3 +291,26 @@ public extension DXFeed {

}
}

public extension DXFeed {
/// Attaches the given subscription to this feed. This method does nothing if the
/// corresponding subscription is already attached to this feed.
///
/// This feed publishes data to the attached subscription.
/// - Parameters:
/// - subscription: The ``DXFeedSubscription``.
/// - Throws: ``GraalException``. Rethrows exception from Java.
func attach(subscription: DXFeedSubscription) throws {
try native.attach(subscription: subscription.nativeSubscription)
}

/// Detaches the given subscription from this feed. This method does nothing if the
/// corresponding subscription is not attached to this feed.
///
/// - Parameters:
/// - subscription: The ``DXFeedSubscription``.
/// - Throws: ``GraalException``. Rethrows exception from Java.
func detach(subscription: DXFeedSubscription) throws {
try native.detach(subscription: subscription.nativeSubscription)
}
}
24 changes: 24 additions & 0 deletions DXFeedFramework/Api/DXFeedSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import Foundation
public class DXFeedSubscription {
/// Subscription native wrapper.
private let native: NativeSubscription

internal var nativeSubscription: NativeSubscription {
return native
}
/// List of event types associated with this ``DXFeedSubscription``
fileprivate let types: [IEventType.Type]
/// A set listeners of events
Expand Down Expand Up @@ -156,3 +160,23 @@ extension DXFeedSubscription: IObservableSubscription {
try native.removeChangeListener(listener)
}
}

public extension DXFeedSubscription {
/// Attaches subscription to the specified feed.
///
/// - Parameters:
/// - feed: The ``DXFeed`` to attach to.
/// - Throws: GraalException. Rethrows exception from Java.
func attach(feed: DXFeed) throws {
try native.attach(feed: feed.nativeFeed)
}

/// Detaches subscription from the specified feed.
///
/// - Parameters:
/// - feed: The ``DXFeed`` to detach from.
/// - Throws: GraalException. Rethrows exception from Java.
func detach(feed: DXFeed) throws {
try native.detach(feed: feed.nativeFeed)
}
}
16 changes: 16 additions & 0 deletions DXFeedFramework/Native/Feed/NativeFeed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,20 @@ class NativeFeed {
toTime))
return NativePromise(promise: &native.pointee.base)
}

func attach(subscription: NativeSubscription) throws {
let thread = currentThread()
try ErrorCheck.nativeCall(thread,
dxfg_DXFeed_attachSubscription(thread,
feed,
subscription.subscription))
}

func detach(subscription: NativeSubscription) throws {
let thread = currentThread()
try ErrorCheck.nativeCall(thread,
dxfg_DXFeed_detachSubscription(thread,
feed,
subscription.subscription))
}
}
18 changes: 18 additions & 0 deletions DXFeedFramework/Native/Subscription/NativeSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,21 @@ extension NativeSubscription {
return result
}
}

extension NativeSubscription {
func attach(feed: NativeFeed) throws {
let thread = currentThread()
try ErrorCheck.nativeCall(thread,
dxfg_DXFeedSubscription_attach(thread,
subscription,
feed.feed))
}

func detach(feed: NativeFeed) throws {
let thread = currentThread()
try ErrorCheck.nativeCall(thread,
dxfg_DXFeedSubscription_detach(thread,
subscription,
feed.feed))
}
}
22 changes: 22 additions & 0 deletions DXFeedFrameworkTests/FeedTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,26 @@ final class FeedTest: XCTestCase {
XCTAssert(false, "Subscription returned null")
}
}

func testAttachDetach() throws {
let endpoint = try DXEndpoint.create()
do {
if let feed = endpoint.getFeed() {
let subcription = try feed.createSubscription([Candle.self])
try subcription.addSymbols("AAPL")
try feed.detach(subscription: subcription)
try feed.detach(subscription: subcription)

try feed.attach(subscription: subcription)
try feed.attach(subscription: subcription)

try subcription.attach(feed: feed)
try subcription.detach(feed: feed)
} else {
XCTAssert(false, "Subscription returned null")
}
} catch {
XCTAssert(false, "Error during attach/detach \(error)")
}
}
}

0 comments on commit a5344a6

Please sign in to comment.