-
Notifications
You must be signed in to change notification settings - Fork 307
Reload a file when other files within the same module or a .swiftmodule
file has been changed
#1180
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
Merged
ahoppen
merged 1 commit into
swiftlang:main
from
ahoppen:refresh-file-on-updated-dependencies
Apr 24, 2024
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,81 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Debounces calls to a function/closure. If multiple calls to the closure are made, it allows aggregating the | ||
/// parameters. | ||
public actor Debouncer<Parameter> { | ||
/// How long to wait for further `scheduleCall` calls before committing to actually calling `makeCall`. | ||
private let debounceDuration: Duration | ||
|
||
/// When `scheduleCall` is called while another `scheduleCall` was waiting to commit its call, combines the parameters | ||
/// of those two calls. | ||
/// | ||
/// ### Example | ||
/// | ||
/// Two `scheduleCall` calls that are made within a time period shorter than `debounceDuration` like the following | ||
/// ```swift | ||
/// debouncer.scheduleCall(5) | ||
/// debouncer.scheduleCall(10) | ||
/// ``` | ||
/// will call `combineParameters(5, 10)` | ||
private let combineParameters: (Parameter, Parameter) -> Parameter | ||
|
||
/// After the debounce duration has elapsed, commit the call. | ||
private let makeCall: (Parameter) async -> Void | ||
|
||
/// In the time between the call to `scheduleCall` and the call actually being committed (ie. in the time that the | ||
/// call can be debounced), the task that would commit the call (unless cancelled) and the parameter with which this | ||
/// call should be made. | ||
private var inProgressData: (Parameter, Task<Void, Never>)? | ||
|
||
public init( | ||
debounceDuration: Duration, | ||
combineResults: @escaping (Parameter, Parameter) -> Parameter, | ||
_ makeCall: @escaping (Parameter) async -> Void | ||
) { | ||
self.debounceDuration = debounceDuration | ||
self.combineParameters = combineResults | ||
self.makeCall = makeCall | ||
} | ||
|
||
/// Schedule a debounced call. If `scheduleCall` is called within `debounceDuration`, the parameters of the two | ||
/// `scheduleCall` calls will be combined using `combineParameters` and the new debounced call will be scheduled | ||
/// `debounceDuration` after the second `scheduleCall` call. | ||
public func scheduleCall(_ parameter: Parameter) { | ||
var parameter = parameter | ||
if let (inProgressParameter, inProgressTask) = inProgressData { | ||
inProgressTask.cancel() | ||
parameter = combineParameters(inProgressParameter, parameter) | ||
} | ||
let task = Task { | ||
do { | ||
try await Task.sleep(for: debounceDuration) | ||
try Task.checkCancellation() | ||
} catch { | ||
return | ||
} | ||
inProgressData = nil | ||
await makeCall(parameter) | ||
} | ||
inProgressData = (parameter, task) | ||
} | ||
} | ||
|
||
extension Debouncer<Void> { | ||
public init(debounceDuration: Duration, _ makeCall: @escaping () async -> Void) { | ||
self.init(debounceDuration: debounceDuration, combineResults: { _, _ in }, makeCall) | ||
} | ||
|
||
public func scheduleCall() { | ||
self.scheduleCall(()) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,27 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
extension FileManager { | ||
/// Returns the URLs of all files with the given file extension in the given directory (recursively). | ||
public func findFiles(withExtension extensionName: String, in directory: URL) -> [URL] { | ||
var result: [URL] = [] | ||
let enumerator = self.enumerator(at: directory, includingPropertiesForKeys: nil) | ||
while let url = enumerator?.nextObject() as? URL { | ||
if url.pathExtension == extensionName { | ||
result.append(url) | ||
} | ||
} | ||
return result | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SKCore | ||
import XCTest | ||
|
||
final class DebouncerTests: XCTestCase { | ||
func testDebouncerDebounces() async throws { | ||
let expectation = self.expectation(description: "makeCallCalled") | ||
expectation.assertForOverFulfill = true | ||
let debouncer = Debouncer<Void>(debounceDuration: .seconds(0.1)) { | ||
expectation.fulfill() | ||
} | ||
await debouncer.scheduleCall() | ||
await debouncer.scheduleCall() | ||
try await self.fulfillmentOfOrThrow([expectation]) | ||
// Sleep for 0.2s to make sure the debouncer actually debounces and doesn't fulfill the expectation twice. | ||
try await Task.sleep(for: .seconds(0.2)) | ||
} | ||
|
||
func testDebouncerCombinesParameters() async throws { | ||
let expectation = self.expectation(description: "makeCallCalled") | ||
expectation.assertForOverFulfill = true | ||
let debouncer = Debouncer<Int>(debounceDuration: .seconds(0.1), combineResults: { $0 + $1 }) { param in | ||
XCTAssertEqual(param, 3) | ||
expectation.fulfill() | ||
} | ||
await debouncer.scheduleCall(1) | ||
await debouncer.scheduleCall(2) | ||
try await self.fulfillmentOfOrThrow([expectation]) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.