-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add: NSObject Publisher #88
Open
viewDidAppear
wants to merge
4
commits into
CombineCommunity:main
Choose a base branch
from
viewDidAppear:add-interception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 71 additions & 30 deletions
101
CombineCocoa.xcodeproj/xcshareddata/xcschemes/CombineCocoa-Package.xcscheme
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 |
---|---|---|
@@ -1,32 +1,73 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme LastUpgradeVersion = "9999" version = "1.3"> | ||
<BuildAction parallelizeBuildables = "YES" buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry buildForTesting = "YES" buildForRunning = "YES" buildForProfiling = "YES" buildForArchiving = "YES" buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BuildableName = "'lib$(TARGET_NAME)'" | ||
BlueprintName = "Runtime" | ||
ReferencedContainer = "container:CombineCocoa.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
<BuildActionEntry buildForTesting = "YES" buildForRunning = "YES" buildForProfiling = "YES" buildForArchiving = "YES" buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BuildableName = "'lib$(TARGET_NAME)'" | ||
BlueprintName = "CombineCocoa" | ||
ReferencedContainer = "container:CombineCocoa.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
codeCoverageEnabled = "YES"> | ||
<Testables> | ||
</Testables> | ||
</TestAction> | ||
<Scheme | ||
LastUpgradeVersion = "9999" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "CombineCocoa::Runtime" | ||
BuildableName = "Runtime.framework" | ||
BlueprintName = "Runtime" | ||
ReferencedContainer = "container:CombineCocoa.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "CombineCocoa::CombineCocoa" | ||
BuildableName = "CombineCocoa.framework" | ||
BlueprintName = "CombineCocoa" | ||
ReferencedContainer = "container:CombineCocoa.xcodeproj"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
codeCoverageEnabled = "YES"> | ||
<Testables> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
167 changes: 167 additions & 0 deletions
167
Sources/CombineCocoa/Interception/NSObject+Association.swift
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,167 @@ | ||
// | ||
// NSObject+Association.swift | ||
// CombineCocoa | ||
// | ||
// Created by Maxim Krouk on 22.06.21. | ||
// Copyright © 2020 Combine Community. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
#if canImport(Runtime) | ||
import Runtime | ||
#endif | ||
|
||
internal struct AssociationKey<Value> { | ||
fileprivate let address: UnsafeRawPointer | ||
fileprivate let `default`: Value! | ||
|
||
/// Create an ObjC association key. | ||
/// | ||
/// - warning: The key must be uniqued. | ||
/// | ||
/// - parameters: | ||
/// - default: The default value, or `nil` to trap on undefined value. It is | ||
/// ignored if `Value` is an optional. | ||
init(default: Value? = nil) { | ||
self.address = UnsafeRawPointer( | ||
UnsafeMutablePointer<UInt8>.allocate(capacity: 1) | ||
) | ||
self.default = `default` | ||
} | ||
|
||
/// Create an ObjC association key from a `StaticString`. | ||
/// | ||
/// - precondition: `key` has a pointer representation. | ||
/// | ||
/// - parameters: | ||
/// - default: The default value, or `nil` to trap on undefined value. It is | ||
/// ignored if `Value` is an optional. | ||
init(_ key: StaticString, default: Value? = nil) { | ||
assert(key.hasPointerRepresentation) | ||
self.address = UnsafeRawPointer(key.utf8Start) | ||
self.default = `default` | ||
} | ||
|
||
/// Create an ObjC association key from a `Selector`. | ||
/// | ||
/// - parameters: | ||
/// - default: The default value, or `nil` to trap on undefined value. It is | ||
/// ignored if `Value` is an optional. | ||
init(_ key: Selector, default: Value? = nil) { | ||
self.address = UnsafeRawPointer(key.utf8Start) | ||
self.default = `default` | ||
} | ||
} | ||
|
||
internal struct Associations<Base: AnyObject> { | ||
fileprivate let base: Base | ||
|
||
init(_ base: Base) { | ||
self.base = base | ||
} | ||
} | ||
|
||
extension NSObjectProtocol { | ||
/// Retrieve the associated value for the specified key. If the value does not | ||
/// exist, `initial` would be called and the returned value would be | ||
/// associated subsequently. | ||
/// | ||
/// - parameters: | ||
/// - key: An optional key to differentiate different values. | ||
/// - initial: The action that supples an initial value. | ||
/// | ||
/// - returns: The associated value for the specified key. | ||
internal func associatedValue<T>( | ||
forKey key: StaticString = #function, | ||
initial: (Self) -> T | ||
) -> T { | ||
let key = AssociationKey<T?>(key) | ||
|
||
if let value = associations.value(forKey: key) { | ||
return value | ||
} | ||
|
||
let value = initial(self) | ||
associations.setValue(value, forKey: key) | ||
|
||
return value | ||
} | ||
} | ||
|
||
extension NSObjectProtocol { | ||
@nonobjc internal var associations: Associations<Self> { | ||
return Associations(self) | ||
} | ||
} | ||
|
||
extension Associations { | ||
/// Retrieve the associated value for the specified key. | ||
/// | ||
/// - parameters: | ||
/// - key: The key. | ||
/// | ||
/// - returns: The associated value, or the default value if no value has been | ||
/// associated with the key. | ||
internal func value<Value>( | ||
forKey key: AssociationKey<Value> | ||
) -> Value { | ||
return (objc_getAssociatedObject(base, key.address) as! Value?) ?? key.default | ||
} | ||
|
||
/// Retrieve the associated value for the specified key. | ||
/// | ||
/// - parameters: | ||
/// - key: The key. | ||
/// | ||
/// - returns: The associated value, or `nil` if no value is associated with | ||
/// the key. | ||
internal func value<Value>( | ||
forKey key: AssociationKey<Value?> | ||
) -> Value? { | ||
return objc_getAssociatedObject(base, key.address) as! Value? | ||
} | ||
|
||
/// Set the associated value for the specified key. | ||
/// | ||
/// - parameters: | ||
/// - value: The value to be associated. | ||
/// - key: The key. | ||
internal func setValue<Value>( | ||
_ value: Value, | ||
forKey key: AssociationKey<Value> | ||
) { | ||
objc_setAssociatedObject(base, key.address, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | ||
} | ||
|
||
/// Set the associated value for the specified key. | ||
/// | ||
/// - parameters: | ||
/// - value: The value to be associated. | ||
/// - key: The key. | ||
internal func setValue<Value>( | ||
_ value: Value?, | ||
forKey key: AssociationKey<Value?> | ||
) { | ||
objc_setAssociatedObject(base, key.address, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | ||
} | ||
} | ||
|
||
/// Set the associated value for the specified key. | ||
/// | ||
/// - parameters: | ||
/// - value: The value to be associated. | ||
/// - key: The key. | ||
/// - address: The address of the object. | ||
internal func unsafeSetAssociatedValue<Value>( | ||
_ value: Value?, | ||
forKey key: AssociationKey<Value>, | ||
forObjectAt address: UnsafeRawPointer | ||
) { | ||
_combinecocoa_objc_setAssociatedObject( | ||
address, | ||
key.address, | ||
value, | ||
.OBJC_ASSOCIATION_RETAIN_NONATOMIC | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This folder (Sources/CombineCocoa/Interception) is actually a port of ReactiveCocoa code, so maybe it is worth mentioning them in Readme/Credits/Acknowledgements