-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from p-x9/feature/delegate
Delegate
- Loading branch information
Showing
6 changed files
with
173 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// AppContainerDelegate.swift | ||
// | ||
// | ||
// Created by p-x9 on 2023/03/06. | ||
// | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol AppContainerDelegate: AnyObject { | ||
func appContainer(_ appContainer: AppContainer, willChangeTo toContainer: Container, from fromContainer: Container?) | ||
func appContainer(_ appContainer: AppContainer, didChangeTo toContainer: Container, from fromContainer: Container?) | ||
} | ||
|
||
extension AppContainerDelegate { | ||
public func appContainer(_ appContainer: AppContainer, willChangeTo toContainer: Container, from fromContainer: Container?) {} | ||
public func appContainer(_ appContainer: AppContainer, didChangeTo toContainer: Container, from fromContainer: Container?) {} | ||
} |
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,47 @@ | ||
// | ||
// WeakHashTable.swift | ||
// | ||
// | ||
// Created by p-x9 on 2023/03/10. | ||
// | ||
|
||
import Foundation | ||
|
||
public class WeakHashTable<T> { | ||
public var objects: [T] { | ||
accessQueue.sync { _objects.allObjects.compactMap { $0 as? T } } | ||
} | ||
|
||
private var _objects: NSHashTable<AnyObject> = NSHashTable.weakObjects() | ||
private let accessQueue: DispatchQueue = DispatchQueue(label:"com.p-x9.appcintainer.WeakHashTable.\(T.self)", | ||
attributes: .concurrent) | ||
|
||
public init() {} | ||
|
||
public init(_ objects: [T]) { | ||
for object in objects { | ||
_objects.add(object as AnyObject) | ||
} | ||
} | ||
|
||
public func add(_ object: T?) { | ||
accessQueue.sync(flags: .barrier) { | ||
_objects.add(object as AnyObject) | ||
} | ||
} | ||
|
||
public func remove(_ object: T?) { | ||
accessQueue.sync(flags: .barrier) { | ||
_objects.remove(object as AnyObject) | ||
} | ||
} | ||
} | ||
|
||
|
||
extension WeakHashTable : Sequence { | ||
public typealias Iterator = Array<T>.Iterator | ||
|
||
public func makeIterator() -> Iterator { | ||
return objects.makeIterator() | ||
} | ||
} |