Skip to content

Commit

Permalink
add listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Apr 10, 2024
1 parent fad25cb commit 2c77827
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 11 deletions.
4 changes: 4 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@
64DA26C02AA224EB005B1757 /* NativeIPFConnectionListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DA26BF2AA224EB005B1757 /* NativeIPFConnectionListener.swift */; };
64DB79342A376E1B00229597 /* DXFeedFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 803BAC0D29BFA50700FFAB1C /* DXFeedFramework.framework */; };
64DB79352A376E1B00229597 /* DXFeedFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 803BAC0D29BFA50700FFAB1C /* DXFeedFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
64DF09312BC6BF18009F1486 /* ObservableListModelListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64DF09302BC6BF18009F1486 /* ObservableListModelListener.swift */; };
64E342502AAB083700457994 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6469F8D22A3B401700846831 /* Colors.swift */; };
64E342522AAB29CF00457994 /* InstrumentProfileType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64E342512AAB29CF00457994 /* InstrumentProfileType.swift */; };
64E3637B2AD83459002E2B0D /* SeriesMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 64E3637A2AD83459002E2B0D /* SeriesMapper.swift */; };
Expand Down Expand Up @@ -898,6 +899,7 @@
64DA26BB2AA20B66005B1757 /* InstrumentProfileConnectionState+ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "InstrumentProfileConnectionState+ext.swift"; sourceTree = "<group>"; };
64DA26BD2AA20EDB005B1757 /* DXInstrumentProfileConnectionListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXInstrumentProfileConnectionListener.swift; sourceTree = "<group>"; };
64DA26BF2AA224EB005B1757 /* NativeIPFConnectionListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeIPFConnectionListener.swift; sourceTree = "<group>"; };
64DF09302BC6BF18009F1486 /* ObservableListModelListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservableListModelListener.swift; sourceTree = "<group>"; };
64E342512AAB29CF00457994 /* InstrumentProfileType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfileType.swift; sourceTree = "<group>"; };
64E3637A2AD83459002E2B0D /* SeriesMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeriesMapper.swift; sourceTree = "<group>"; };
64EAA1A12B7A38F8005087BC /* DXPromiseTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXPromiseTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1210,6 +1212,7 @@
644B95F32BC5873500E95CB7 /* OrderBookModelFilter.swift */,
644B95F72BC672E500E95CB7 /* ObservableListModel.swift */,
644B95F92BC673BC00E95CB7 /* OrderBookModelListener.swift */,
64DF09302BC6BF18009F1486 /* ObservableListModelListener.swift */,
);
path = Market;
sourceTree = "<group>";
Expand Down Expand Up @@ -2524,6 +2527,7 @@
64E342522AAB29CF00457994 /* InstrumentProfileType.swift in Sources */,
648E98AA2AAF625800BFD219 /* IIndexedEvent+Ext.swift in Sources */,
64A42F452B0B933B001C3ACC /* NativeTimeUtil.swift in Sources */,
64DF09312BC6BF18009F1486 /* ObservableListModelListener.swift in Sources */,
6423E4672B44613D006B208D /* NativeTimeSeriesSubscription.swift in Sources */,
6447A5E32A8F611700739CCF /* IObservableSubscription.swift in Sources */,
64A42F4E2B0B9FA4001C3ACC /* DXTimeZone.swift in Sources */,
Expand Down
35 changes: 35 additions & 0 deletions DXFeedFramework/Model/Market/ObservableListModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,44 @@ import Foundation

public class ObservableListModel {
let native: NativeObservableListModel
private let listeners = ConcurrentWeakHashTable<AnyObject>()

init(native: NativeObservableListModel) {
self.native = native
}

/// Adds a listener to this order book model.
/// - Parameters:
/// - listener: ``ObservableListModelListener``.
public func add<O>(listener: O) throws
where O: ObservableListModelListener,
O: Hashable {
try listeners.reader { [weak self] in
if $0.count == 0 {
try self?.native.addListener(self)
}
}
listeners.insert(listener)
}

/// Removes a listener from this order book model.
/// - Parameters:
/// - listener: ``ObservableListModelListener``.
public func remove<O>(listener: O)
where O: ObservableListModelListener,
O: Hashable {
listeners.remove(listener)
}

}

extension ObservableListModel: ObservableListModelListener {
public func changed(order: [MarketEvent]) {
listeners.reader { items in
let enumerator = items.objectEnumerator()
while let listener = enumerator.nextObject() as? ObservableListModelListener {
listener.changed(order: order)
}
}
}
}
13 changes: 13 additions & 0 deletions DXFeedFramework/Model/Market/ObservableListModelListener.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
//
// Copyright (C) 2024 Devexperts LLC. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

import Foundation

/// Callback interface that receives notifications about changes in ``ObservableListModel``
public protocol ObservableListModelListener: AnyObject {
func changed(order: [MarketEvent])
}
77 changes: 76 additions & 1 deletion DXFeedFramework/Native/Model/NativeObservableListModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,90 @@
// Copyright (C) 2024 Devexperts LLC. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
//

import Foundation
@_implementationOnly import graal_api

class NativeObservableListModel {
private class WeakModel: WeakBox<NativeObservableListModel> { }

weak var listener: ObservableListModelListener?

let native: UnsafeMutablePointer<dxfg_observable_list_model_t>?

var nativeListener: UnsafeMutablePointer<dxfg_observable_list_model_listener_t>?

private let mapper = EventMapper()

private static let listeners = ConcurrentArray<WeakModel>()

private static let finalizeCallback: dxfg_finalize_function = { _, context in
if let context = context {
let endpoint: AnyObject = bridge(ptr: context)
if let listener = endpoint as? WeakModel {
NativeObservableListModel.listeners.removeAll(where: {
return $0 === listener
})
}
}
}

static let listenerCallback: dxfg_observable_list_model_listener_function = {_, nativeEvents, context in
if let context = context {
var events = [MarketEvent]()
let listener: AnyObject = bridge(ptr: context)
if let listener = listener as? WeakModel {
guard let subscription = listener.value else {
return
}
let count = Int(nativeEvents?.pointee.size ?? 0)
for index in 0..<count {
if let element = nativeEvents?.pointee.elements[index] {
if let event = try? subscription.mapper.fromNative(native: element) {
events.append(event)
}
}
}
ThreadManager.insertPthread()
subscription.listener?.changed(order: events)
}
}
}

deinit {
clearNativeListeners()

if let native = native {
let thread = currentThread()
_ = try? ErrorCheck.nativeCall(thread,
dxfg_JavaObjectHandler_release(thread,
&(native.pointee.handler)))
}
}

init(native: UnsafeMutablePointer<dxfg_observable_list_model_t>?) {
self.native = native
}

func getEvents() throws -> [MarketEvent]? {
return nil
}

func addListener(_ listener: ObservableListModelListener?) throws {

}

fileprivate func clearNativeListeners() {
if let nativeListener = nativeListener {
let thread = currentThread()
_ = try? ErrorCheck.nativeCall(thread,
dxfg_ObservableListModel_removeListener(thread,
native,
nativeListener))
_ = try? ErrorCheck.nativeCall(thread,
dxfg_JavaObjectHandler_release(thread,
&(nativeListener.pointee.handler)))
}
}
}
11 changes: 1 addition & 10 deletions DXFeedFramework/Native/Model/NativeOrderBookModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,13 @@ class NativeOrderBookModel {
}
}

static let listenerCallback: dxfg_order_book_model_listener_function = {_, nativeEvents, context in
static let listenerCallback: dxfg_order_book_model_listener_function = {_, _, context in
if let context = context {
var events = [MarketEvent]()
let listener: AnyObject = bridge(ptr: context)
if let listener = listener as? WeakModel {
guard let subscription = listener.value else {
return
}
// let count = Int(nativeEvents?.pointee.size ?? 0)
// for index in 0..<count {
// if let element = nativeEvents?.pointee.elements[index] {
// if let event = try? subscription.mapper.fromNative(native: element) {
// events.append(event)
// }
// }
// }
ThreadManager.insertPthread()
subscription.listener?.modelChanged()
}
Expand Down

0 comments on commit 2c77827

Please sign in to comment.