Skip to content

Commit

Permalink
fix: compile error on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Asura19 committed Oct 30, 2024
1 parent 79ea000 commit dc9fa23
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Counter {
static let shared: Counter = .init()
var count: Int = 0 {
didSet {
multicastDelegate.invoke { $0.counterDidUpdate(self, to: count) }
multicastDelegate.notify { $0.counterDidUpdate(self, to: count) }
}
}
lazy var timer = RETimer(timeInterval: 1, callbackImmediatelyWhenFired: true) { [weak self] _ in
Expand Down
14 changes: 12 additions & 2 deletions Sources/Utility/MulticastDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public protocol MulticastDelegateProtocol {
public final class MulticastDelegate<T> {

private let delegates: WeakSet<AnyObject>
#if os(Linux)
private let lock = UnfairLock()
#else
private let lock = MutexLock()
#endif


/// ReerKit: Initialize a new `MulticastDelegate`, and delete references will be weak.
public init() {
Expand Down Expand Up @@ -66,12 +71,17 @@ public final class MulticastDelegate<T> {
lock.around { delegates.removeAll() }
}

/// ReerKit: Invoke a closure on each delegate.
@available(*, deprecated, renamed: "notify(_:)", message: "Use notify(_:) instead.")
public func invoke(_ invocation: (T) -> Void) {
notify(invocation)
}

/// ReerKit: Notify a closure on each delegate.
public func notify(_ function: (T) -> Void) {
let delegateCopy = lock.around { delegates }
for delegate in delegateCopy {
if let delegate = delegate as? T {
invocation(delegate)
function(delegate)
}
}
}
Expand Down

0 comments on commit dc9fa23

Please sign in to comment.