forked from pointfreeco/swift-snapshot-testing
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Async.swift
39 lines (36 loc) · 1.47 KB
/
Async.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// A wrapper around an asynchronous operation.
///
/// Snapshot strategies may utilize this type to create snapshots in an asynchronous fashion.
///
/// For example, WebKit's `WKWebView` offers a callback-based API for taking image snapshots (`takeSnapshot`). `Async` allows us to build a value that can pass its callback along to the scope in which the image has been created.
///
/// Async<UIImage> { callback in
/// webView.takeSnapshot(with: nil) { image, error in
/// callback(image!)
/// }
/// }
public struct Async<Value> {
public let run: (@escaping (Value) -> Void) -> Void
/// Creates an asynchronous operation.
///
/// - Parameters:
/// - run: A function that, when called, can hand a value to a callback.
/// - callback: A function that can be called with a value.
public init(run: @escaping (_ callback: @escaping (Value) -> Void) -> Void) {
self.run = run
}
/// Wraps a pure value in an asynchronous operation.
///
/// - Parameter value: A value to be wrapped in an asynchronous operation.
public init(value: Value) {
self.init { callback in callback(value) }
}
/// Transforms an Async<Value> into an Async<NewValue> with a function `(Value) -> NewValue`.
///
/// - Parameter f: A transformation to apply to the value wrapped by the async value.
public func map<NewValue>(_ f: @escaping (Value) -> NewValue) -> Async<NewValue> {
return .init { callback in
self.run { a in callback(f(a)) }
}
}
}