-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe8eb86
commit bd20449
Showing
12 changed files
with
855 additions
and
604 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
test_macos: | ||
if: | | ||
!contains(github.event.head_commit.message, '[ci skip]') && | ||
!contains(github.event.head_commit.message, '[ci skip test]') && | ||
!contains(github.event.head_commit.message, '[ci skip test_macos]') | ||
runs-on: macOS-13 | ||
timeout-minutes: 30 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Select Xcode 15.0.0 | ||
run: sudo xcode-select -s /Applications/Xcode_15.0.app | ||
- name: Run tests | ||
run: make test |
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 |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData | ||
Package.resolved |
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,5 @@ | ||
version: 1 | ||
builder: | ||
configs: | ||
- documentation_targets: [Capture] | ||
swift_version: 5.9 |
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,5 @@ | ||
test: | ||
@swift test | ||
|
||
preview_docs: | ||
@swift package --disable-sandbox preview-documentation --product Capture |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# swift-capture | ||
|
||
[![SwiftPM 5.3](https://img.shields.io/badge/📦_swiftpm-5.3-ED523F.svg?style=flat)](https://swift.org/download/) [![@maximkrouk](https://img.shields.io/badge/contact-@capturecontext-1DA1F2.svg?style=flat&logo=twitter)](https://twitter.com/capture_context) | ||
[![Test](https://github.com/capturecontext/swift-capture/actions/workflows/Test.yml/badge.svg) [![SwiftPM 5.9](https://img.shields.io/badge/📦_swiftpm-5.9-ED523F.svg?style=flat)](https://github.com/CaptureContext/swift-declarative-configuration/actions/workflows/Test.yml) ![Platforms](https://img.shields.io/badge/platforms-iOS_|_macOS_|_tvOS_|_watchOS_|_Catalyst-ED523F.svg?style=flat) [![@capture_context](https://img.shields.io/badge/contact-@capture__context-1DA1F2.svg?style=flat&logo=twitter)](https://twitter.com/capture_context) | ||
|
||
A mechanism for safe capturing & weakifying objects in Swift. | ||
A mechanism for ergonomic and safe capturing & weakifying objects in Swift. | ||
|
||
## Usage Examples | ||
|
||
|
@@ -19,7 +19,7 @@ With Capture | |
Default | ||
```swift | ||
{ [weak self] in | ||
guard let self = self else { return } | ||
guard let self else { return } | ||
/// ... | ||
} | ||
``` | ||
|
@@ -35,7 +35,7 @@ capture { _self in | |
Multiple parameters | ||
```swift | ||
{ [weak self] a, b, c in | ||
guard let self = self else { return } | ||
guard let self else { return } | ||
/// ... | ||
} | ||
``` | ||
|
@@ -58,7 +58,7 @@ Methods | |
``` | ||
|
||
```swift | ||
capture(Self.someMethod) | ||
capture(in: <#Type#>.someMethod) | ||
``` | ||
|
||
---- | ||
|
@@ -73,29 +73,14 @@ let object.dataSource = { [weak self] in | |
``` | ||
|
||
```swift | ||
let object.dataSource = capture(or: [], in: \.data) | ||
``` | ||
|
||
---- | ||
|
||
Weak assign | ||
|
||
```swift | ||
{ [weak self] value in | ||
self?.value = value | ||
} | ||
``` | ||
|
||
```swift | ||
captureAssign(to: \.value) | ||
captureAssign(to: \.value, removeDuplicates: ==) | ||
let object.dataSource = capture(orReturn: [], in: \.data) | ||
``` | ||
|
||
## Installation | ||
|
||
### Basic | ||
|
||
You can add `weak` to an Xcode project by adding it as a package dependency. | ||
You can add `swift-capture` to an Xcode project by adding it as a package dependency. | ||
|
||
1. From the **File** menu, select **Swift Packages › Add Package Dependency…** | ||
2. Enter [`"https://github.com/capturecontext/swift-capture"`](https://github.com/capturecontext/swift-capture) into the package repository URL text field | ||
|
@@ -107,17 +92,15 @@ If you use SwiftPM for your project, you can add `weak` to your package file. Al | |
|
||
```swift | ||
.package( | ||
name: "weak", | ||
url: "[email protected]:capturecontext/swift-capture.git", | ||
.upToNextMajor("2.0.0") | ||
.upToNextMajor("3.0.0") | ||
) | ||
``` | ||
|
||
Do not forget about target dependencies: | ||
|
||
```swift | ||
.product( | ||
name: "swift-capture", | ||
name: "Capture", | ||
package: "swift-capture" | ||
) | ||
|
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,88 @@ | ||
import Foundation | ||
|
||
// MARK: - Void result closures | ||
|
||
/// Weakly captures an object in non-parametrized void result closure. | ||
@inlinable | ||
public func capture<Object: AnyObject>( | ||
_ object: Object, | ||
in closure: @escaping (Object) -> Void | ||
) -> () -> Void { | ||
return Weak(object).capture(in: closure) | ||
} | ||
|
||
/// Weakly captures an object in non-parametrized lazy void result closure. | ||
@inlinable | ||
public func capture<Object: AnyObject>( | ||
_ object: Object, | ||
in closure: @escaping (Object) -> () -> Void | ||
) -> () -> Void { | ||
Weak(object).capture(in: closure) | ||
} | ||
|
||
/// Weakly captures an object in parametrized void result closure. | ||
public func capture<Object: AnyObject, each Arg>( | ||
_ object: Object, | ||
in closure: @escaping (Object, repeat each Arg) -> Void | ||
) -> (repeat each Arg) -> Void { | ||
Weak(object).capture(in: closure) | ||
} | ||
|
||
// MARK: - Non-void result closures | ||
|
||
/// Weakly captures an object in non-parametrized non-void result closure. | ||
@inlinable | ||
public func capture<Object: AnyObject, Output>( | ||
_ object: Object, | ||
orReturn defaultValue: @escaping @autoclosure () -> Output, | ||
in closure: @escaping (Object) -> Output | ||
) -> () -> Output { | ||
Weak(object).capture(orReturn: defaultValue(), in: closure) | ||
} | ||
|
||
/// Weakly captures an object in non-parametrized lazy non-void result closure. | ||
@inlinable | ||
public func capture<Object: AnyObject, Output>( | ||
_ object: Object, | ||
orReturn defaultValue: @escaping @autoclosure () -> Output, | ||
in closure: @escaping (Object) -> () -> Output | ||
) -> () -> Output { | ||
Weak(object).capture(orReturn: defaultValue(), in: closure) | ||
} | ||
|
||
/// Weakly captures an object in parametrized non-void result closure. | ||
public func capture<Object: AnyObject, each Arg, Output>( | ||
_ object: Object, | ||
orReturn defaultValue: @escaping @autoclosure () -> Output, | ||
in closure: @escaping (Object, repeat each Arg) -> Output | ||
) -> (repeat each Arg) -> Output { | ||
Weak(object).capture(orReturn: defaultValue(), in: closure) | ||
} | ||
|
||
// MARK: - Non-void optional result closures | ||
|
||
/// Weakly captures an object in non-parametrized non-void optional result closure. | ||
@inlinable | ||
public func capture<Object: AnyObject, Output>( | ||
_ object: Object, | ||
in closure: @escaping (Object) -> Output? | ||
) -> () -> Output? { | ||
Weak(object).capture(in: closure) | ||
} | ||
|
||
/// Weakly captures an object in non-parametrized lazy non-void optional result closure. | ||
@inlinable | ||
public func capture<Object: AnyObject, Output>( | ||
_ object: Object, | ||
in closure: @escaping (Object) -> () -> Output? | ||
) -> () -> Output? { | ||
Weak(object).capture(in: closure) | ||
} | ||
|
||
/// Weakly captures an object in parametrized non-void optional result closure. | ||
public func capture<Object: AnyObject, each Arg, Output>( | ||
_ object: Object, | ||
in closure: @escaping (Object, repeat each Arg) -> Output? | ||
) -> (repeat each Arg) -> Output? { | ||
Weak(object).capture(in: closure) | ||
} |
Oops, something went wrong.