Skip to content
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

Makes sendable future callbacks #34

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Sources/FunctionalKit/Future.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ private enum FutureState<T> {
// sourcery: testConstruct = "init { $0(x) }"
// sourcery: testNeedsCommand = "start()"
public final class Future<Parameter> {
private var continuation: ((@escaping (Parameter) -> ()) -> ())?
public init(_ continuation: @escaping (@escaping (Parameter) -> ()) -> ()) {
private var continuation: ((@Sendable @escaping (Parameter) -> ()) -> ())?
public init(_ continuation: @escaping (@Sendable @escaping (Parameter) -> ()) -> ()) {
self.continuation = continuation
}

Expand All @@ -41,7 +41,7 @@ public final class Future<Parameter> {
}

@discardableResult
public func run(_ callback: @escaping (Parameter) -> ()) -> Future {
public func run(_ callback: @Sendable @escaping (Parameter) -> ()) -> Future {
switch currentState {
case .idle:
callbacks.append(callback)
Expand Down Expand Up @@ -87,23 +87,23 @@ extension Future: PureConstructible {


public extension Future {
func map <A> (_ transform: @escaping (ParameterType) -> A) -> Future<A> {
func map <A> (_ transform: @Sendable @escaping (ParameterType) -> A) -> Future<A> {
return Generic.init { done in
self.run { value in done(transform(value)) }
}
}

static func lift<A>(_ function: @escaping (ParameterType) -> A) -> (Future) -> Future<A> {
static func lift<A>(_ function: @Sendable @escaping (ParameterType) -> A) -> (Future) -> Future<A> {
return { $0.map(function) }
}

static func lift<A,B>(_ function: @escaping (A, B) -> ParameterType) -> (Future<A>, Future<B>) -> Future {
static func lift<A,B>(_ function: @Sendable @escaping (A, B) -> ParameterType) -> (Future<A>, Future<B>) -> Future {
return { (ap1, ap2) in
Generic.pure(f.curry(function)) <*> ap1 <*> ap2
}
}

static func lift<A,B,C>(_ function: @escaping (A, B, C) -> ParameterType) -> (Future<A>, Future<B>, Future<C>) -> Future {
static func lift<A,B,C>(_ function: @Sendable @escaping (A, B, C) -> ParameterType) -> (Future<A>, Future<B>, Future<C>) -> Future {
return { (ap1, ap2, ap3) in
Generic.pure(f.curry(function)) <*> ap1 <*> ap2 <*> ap3
}
Expand All @@ -114,13 +114,13 @@ public extension Future {
static func zipParallel <A,B> (_ first: Future<A>, _ second: Future<B>) -> Future<(A,B)> where ParameterType == (A,B) {
return Generic.init { done in
var tuple: (A?,B?) = (nil,nil)

first.run { value in
tuple.0 = value
guard let first = tuple.0, let second = tuple.1 else { return }
done((first,second))
}

second.run { value in
tuple.1 = value
guard let first = tuple.0, let second = tuple.1 else { return }
Expand Down Expand Up @@ -165,7 +165,7 @@ public extension Future {
return Generic.init { done in self.run { $0.run(done) } }
}

func flatMap <A> (_ transform: @escaping (ParameterType) -> Future<A>) -> Future<A> {
func flatMap <A> (_ transform: @Sendable @escaping (ParameterType) -> Future<A>) -> Future<A> {
return map(transform).joined()
}
}
Expand All @@ -179,7 +179,7 @@ public extension Future {
}
}

static func after(_ delay: Double, call: @escaping () -> ParameterType) -> Future {
static func after(_ delay: Double, call: @Sendable @escaping () -> ParameterType) -> Future {
return Future.init { done in
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
done(call())
Expand Down
6 changes: 3 additions & 3 deletions Tests/FunctionalKitTests/FutureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Abstract
class FutureTests: XCTestCase {

func testLiftOneArg() {
let id: (Int) -> Int = {$0}
let id: @Sendable (Int) -> Int = {$0}
let ap1 = Future.pure(1)

expecting("testLiftOneArg") { (fulfill) in
Expand All @@ -21,7 +21,7 @@ class FutureTests: XCTestCase {
}

func testLiftTwoArgs() {
let sum: (Int,Int) -> Int = {$0 + $1}
let sum: @Sendable (Int,Int) -> Int = {$0 + $1}
let ap1 = Future.pure(1)
let ap2 = Future.pure(2)

Expand All @@ -34,7 +34,7 @@ class FutureTests: XCTestCase {
}

func testLiftThreeArgs(){
let sum: (Int,Int, Int) -> Int = {$0 + $1 + $2}
let sum: @Sendable (Int,Int, Int) -> Int = {$0 + $1 + $2}
let ap1 = Future.pure(1)
let ap2 = Future.pure(2)
let ap3 = Future.pure(3)
Expand Down