Skip to content

Commit

Permalink
fix compile error, factor out one time init
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed Aug 2, 2023
1 parent a2f86a2 commit 9c7d022
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
3 changes: 1 addition & 2 deletions Examples/OCADevice/DeviceApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public enum DeviceApp {
}

let device = AES70Device.shared
let listener = try await AES70OCP1Listener(address: localAddressData)
try await device.add(listener: listener)
let listener = try await AES70OCP1Listener(device: device, address: localAddressData)

class MyBooleanActuator: SwiftOCADevice.OcaBooleanActuator {
override open class var classID: OcaClassID { OcaClassID(parent: super.classID, 65280) }
Expand Down
27 changes: 16 additions & 11 deletions Sources/SwiftOCADevice/AES70/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,26 @@ public actor AES70Device {
return nextObjectNumber
}

public func add(listener: AES70Listener) async throws {
if rootBlock == nil {
rootBlock = try await OcaBlock(
objectNumber: OcaRootBlockONo,
deviceDelegate: self,
addToRootBlock: false
)
rootBlock.type = 1
subscriptionManager = try await OcaSubscriptionManager(deviceDelegate: self)
deviceManager = try await OcaDeviceManager(deviceDelegate: self)
private func initOnce() async throws {
guard rootBlock == nil else {
return
}
rootBlock = try await OcaBlock(
objectNumber: OcaRootBlockONo,
deviceDelegate: self,
addToRootBlock: false
)
rootBlock.type = 1
subscriptionManager = try await OcaSubscriptionManager(deviceDelegate: self)
deviceManager = try await OcaDeviceManager(deviceDelegate: self)
}

public func add(listener: AES70Listener) async throws {
try await initOnce()
listeners.append(listener)
}

public func register(object: OcaRoot, addToRootBlock: Bool = true) throws {
public func register(object: OcaRoot, addToRootBlock: Bool = true) async throws {
precondition(
object.objectNumber != OcaInvalidONo,
"cannot register object with invalid ONo"
Expand All @@ -62,6 +66,7 @@ public actor AES70Device {
objects[object.objectNumber] = object
if addToRootBlock {
precondition(object.objectNumber != OcaRootBlockONo)
try await initOnce()
try rootBlock.add(actionObject: object)
}
}
Expand Down
3 changes: 1 addition & 2 deletions Tests/SwiftOCADeviceTests/SwiftOCADeviceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class MyBooleanActuator: SwiftOCADevice.OcaBooleanActuator {
final class SwiftOCADeviceTests: XCTestCase {
func testLoopbackDevice() async throws {
let device = AES70Device.shared
let listener = try await AES70LocalListener()
try await device.add(listener: listener)
let listener = try await AES70LocalListener(device: device)

let matrix = try await SwiftOCADevice
.OcaMatrix<MyBooleanActuator>(
Expand Down

0 comments on commit 9c7d022

Please sign in to comment.