-
Notifications
You must be signed in to change notification settings - Fork 126
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
Introduce Scheduler.dependencies() #86
Open
kabiroberai
wants to merge
3
commits into
pointfreeco:main
Choose a base branch
from
kabiroberai:kabir/scheduler-dependencies
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
3 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
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,25 @@ | ||
#if canImport(Combine) | ||
import Combine | ||
|
||
extension Scheduler { | ||
/// Propagates dependencies across the scheduler boundary. | ||
/// | ||
/// - Parameter update: Enables transforming the propagated dependencies. No-ops by default. | ||
/// - Returns: A version of the scheduler that propagates dependencies. | ||
public func dependencies( | ||
_ update: @escaping (inout DependencyValues) -> Void = { _ in } | ||
) -> AnySchedulerOf<Self> { | ||
func forward(_ action: @escaping () -> Void) -> () -> Void { | ||
let continuation = withDependencies(update) { withEscapedDependencies { $0 } } | ||
return { continuation.yield(action) } | ||
} | ||
return AnyScheduler( | ||
minimumTolerance: { self.minimumTolerance }, | ||
now: { self.now }, | ||
scheduleImmediately: { self.schedule(options: $0, forward($1)) }, | ||
delayed: { self.schedule(after: $0, tolerance: $1, options: $2, forward($3)) }, | ||
interval: { self.schedule(after: $0, interval: $1, tolerance: $2, options: $3, forward($4)) } | ||
) | ||
} | ||
} | ||
#endif |
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,53 @@ | ||
#if canImport(Combine) | ||
import Dependencies | ||
import Dispatch | ||
import XCTest | ||
|
||
final class SchedulerTests: XCTestCase { | ||
func testDependencyPropagation() { | ||
// we have to use live schedulers here because a test scheduler would | ||
// propagate dependencies anyway, since it's immediate. | ||
let queue = DispatchQueue.global(qos: .userInteractive) | ||
let scheduler1 = queue.dependencies() | ||
let scheduler2 = queue.dependencies { $0.int = 7 } | ||
|
||
var value1a, value1b, value2: Int? | ||
let expectation = self.expectation(description: "schedulers") | ||
expectation.expectedFulfillmentCount = 3 | ||
|
||
@Dependency(\.int) var int | ||
scheduler1.schedule { | ||
value1a = int | ||
expectation.fulfill() | ||
} | ||
withDependencies { | ||
$0.int = 5 | ||
} operation: { | ||
scheduler1.schedule { | ||
value1b = int | ||
expectation.fulfill() | ||
} | ||
scheduler2.schedule { | ||
value2 = int | ||
expectation.fulfill() | ||
} | ||
} | ||
|
||
self.wait(for: [expectation], timeout: 1) | ||
XCTAssertEqual(value1a, 42) | ||
XCTAssertEqual(value1b, 5) | ||
XCTAssertEqual(value2, 7) | ||
} | ||
} | ||
|
||
extension DependencyValues { | ||
fileprivate var int: Int { | ||
get { self[IntKey.self] } | ||
set { self[IntKey.self] = newValue } | ||
} | ||
} | ||
|
||
private enum IntKey: TestDependencyKey { | ||
static let testValue = 42 | ||
} | ||
#endif |
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.
I was worried that users would explicitly need to use
dependencies()
in the test value, but then I realized that test schedulers already forward dependencies since they invoke the closure immediately (inheriting task locals). Though I'm not sure if this applies to the delay/interval APIs; dunno if we need to add explicit tests for those.