Skip to content

Commit

Permalink
Support throwing functions in Once#wrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 29, 2019
1 parent ab1b942 commit 5e5aee2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Gifski/Vendor/DockProgress.swift
@@ -1,5 +1,5 @@
// Vendored from: https://github.com/sindresorhus/DockProgress
/// TODO: Use Carthage and frameworks again when targeting Swift 5 as it should be ABI stable
// TODO: Use Carthage and frameworks again when targeting Swift 5 as it should be ABI stable
import Cocoa

public final class DockProgress {
Expand Down
2 changes: 1 addition & 1 deletion Gifski/VideoDropView.swift
Expand Up @@ -103,7 +103,7 @@ class DropView: SSView {
}

final class VideoDropView: DropView {
/// TODO: Any way to make this generic so we can have it in DropView instead?
// TODO: Any way to make this generic so we can have it in DropView instead?
var onComplete: (([URL]) -> Void)?

override var highlightColor: NSColor {
Expand Down
32 changes: 26 additions & 6 deletions Gifski/util.swift
Expand Up @@ -1171,10 +1171,9 @@ extension CGRect {
}
}


// TODO: Remove when using Swift 5
/// Polyfill for Swift 5
/// https://github.com/moiseev/swift/blob/47740c012943020aa89df93129b4fc2f33618c00/stdlib/public/core/Result.swift
/// TODO: Remove when using Swift 5
///
/// A value that represents either a success or a failure, including an
/// associated value in each case.
Expand Down Expand Up @@ -1374,7 +1373,7 @@ extension Result {
}
}


// TODO: Find a way to reduce the number of overloads for `wrap()`.
final class Once {
private var lock = os_unfair_lock()
private var hasRun = false
Expand Down Expand Up @@ -1435,9 +1434,8 @@ final class Once {
return returnValue
}

// TODO: Make it support `rethrows` so that if the input `function` is `throws` then the wrapped function becomes throwing too.
// TODO: Support any number of arguments when Swift supports variadics.
// Wraps a single-argument function.
/// Wraps a single-argument function.
func wrap<T, U>(_ function: @escaping ((T) -> U)) -> ((T) -> U) {
return { parameter in
self.run {
Expand All @@ -1446,7 +1444,7 @@ final class Once {
}
}

// Wraps an optional single-argument function.
/// Wraps an optional single-argument function.
func wrap<T, U>(_ function: ((T) -> U)?) -> ((T) -> U)? {
guard let function = function else {
return nil
Expand All @@ -1458,4 +1456,26 @@ final class Once {
}
}
}

/// Wraps a single-argument throwing function.
func wrap<T, U>(_ function: @escaping ((T) throws -> U)) -> ((T) throws -> U) {
return { parameter in
try self.run {
try function(parameter)
}
}
}

/// Wraps an optional single-argument throwing function.
func wrap<T, U>(_ function: ((T) throws -> U)?) -> ((T) throws -> U)? {
guard let function = function else {
return nil
}

return { parameter in
try self.run {
try function(parameter)
}
}
}
}

0 comments on commit 5e5aee2

Please sign in to comment.