Functions

The following functions are available globally.

  • Makes completed future

    Declaration

    Swift

    public func future<Success>(value: Fallible<Success>) -> Future<Success>

    Parameters

    value

    value to complete future with

    Return Value

    completed future

  • Makes completed future

    Declaration

    Swift

    public func future<Success>(completion: Fallible<Success>) -> Future<Success>

    Parameters

    completion

    value to complete future with

    Return Value

    completed future

  • Makes succeeded future

    Declaration

    Swift

    public func future<Success>(success: Success) -> Future<Success>

    Parameters

    success

    success value to complete future with

    Return Value

    succeeded future

  • Makes failed future

    Declaration

    Swift

    public func future<Success>(failure: Swift.Error) -> Future<Success>

    Parameters

    failure

    failure value (Error) to complete future with

    Return Value

    failed future

  • Makes cancelled future (shorthand to future(failure: AsyncNinjaError.cancelled))

    Declaration

    Swift

    public func cancelledFuture<Success>() -> Future<Success>

    Return Value

    cancelled future

  • Binds two event streams bidirectionally.

    Declaration

    Swift

    public func doubleBind<T: EventSource&EventDestination, U: EventSource&EventDestination>(
      _ majorStream: T,
      transform: @escaping (T.Update) -> U.Update,
      _ minorStream: U,
      reverseTransform: @escaping (U.Update) -> T.Update)

    Parameters

    majorStream

    a stream to bind to. This stream has a priority during initial synchronization

    transform

    for T.Update -> U.Update

    minorStream

    a stream to bind to.

    reverseTransform

    for U.Update -> T.Update

  • Binds two event streams bidirectionally.

    Declaration

    Swift

    public func doubleBind<T: EventSource&EventDestination, U: EventSource&EventDestination>(
      _ majorStream: T,
      _ minorStream: U
      ) where T.Update == U.Update

    Parameters

    majorStream

    a stream to bind to. This stream has a priority during initial synchronization

    minorStream

    a stream to bind to.

  • Executes specified block and wraps a returned value or a thrown error with Fallible

    Declaration

    Swift

    public func fallible<T>(block: () throws -> T) -> Fallible<T>

    Parameters

    block

    to execute. A returned value will become success value of retured Fallible. A thrown error will become failure value of returned Fallible.

    Return Value

    fallible constructed of a returned value or a thrown error with Fallible

  • Executes specified block and returns returned Fallible or wraps a thrown error with Fallible

    Declaration

    Swift

    public func flatFallible<T>(block: () throws -> Fallible<T>) -> Fallible<T>

    Parameters

    block

    to execute. A returned value will returned from method. A thrown error will become failure value of returned Fallible.

    Return Value

    returned Fallible or a thrown error wrapped with Fallible

  • Combines successes of two failables or returns fallible with first error

    Declaration

    Swift

    public func zip<A, B>(
      _ a: Fallible<A>,
      _ b: Fallible<B>
      ) -> Fallible<(A, B)>
  • Combines successes of tree failables or returns fallible with first error

    Declaration

    Swift

    public func zip<A, B, C>(
      _ a: Fallible<A>,
      _ b: Fallible<B>,
      _ c: Fallible<C>
      ) -> Fallible<(A, B, C)>
  • Makes future that will complete depending on block’s return/throw

    Declaration

    Swift

    public func future<T>(
      executor: Executor = .primary,
      _ block: @escaping () throws -> T) -> Future<T>

    Parameters

    executor

    is Executor to execute block on

    block

    is block to perform. Return from block will cause returned future to complete successfuly. Throw from block will returned future to complete with failure

    Return Value

    future

  • Makes future that will complete depending on block’s return/throw

    Declaration

    Swift

    public func flatFuture<T>(
      executor: Executor = .primary,
      _ block: @escaping () throws -> Future<T>) -> Future<T>

    Parameters

    executor

    is Executor to execute block on

    block

    is block to perform. Return from block will cause returned future to complete with future. Throw from block will returned future to complete with failure

    Return Value

    future

  • Makes future that will complete depending on block’s return/throw

    Declaration

    Swift

    public func future<T, U: ExecutionContext>(
      context: U,
      executor: Executor? = nil,
      _ block: @escaping (_ strongContext: U) throws -> T) -> Future<T>

    Parameters

    context

    is ExecutionContext to perform transform on. Instance of context will be passed as the first argument to the block. Block will not be executed if executor was deallocated before execution, returned future will fail with AsyncNinjaError.contextDeallocated error

    executor

    is Executor to override executor provided by context

    block

    is block to perform. Return from block will cause returned future to complete successfuly. Throw from block will returned future to complete with failure

    strongContext

    is ExecutionContext restored from a weak reference of context passed to method

    Return Value

    future

  • Makes future that will complete depending on block’s return/throw

    Declaration

    Swift

    public func flatFuture<T, C: ExecutionContext>(
      context: C,
      executor: Executor? = nil,
      _ block: @escaping (_ strongContext: C) throws -> Future<T>) -> Future<T>

    Parameters

    context

    is ExecutionContext to perform transform on. Instance of context will be passed as the first argument to the block. Block will not be executed if executor was deallocated before execution, returned future will fail with AsyncNinjaError.contextDeallocated error

    executor

    is Executor to override executor provided by context

    block

    is block to perform. Return from block will cause returned future to complete with future. Throw from block will complete returned future with failure.

    strongContext

    is ExecutionContext restored from weak reference of context passed to method

    Return Value

    future

  • Asynchrounously executes block after timeout on executor and wraps returned value into future Makes future that will complete depending on block’s return/throw

    Declaration

    Swift

    public func future<T>(
      executor: Executor = .primary,
      after timeout: Double,
      cancellationToken: CancellationToken? = nil,
      _ block: @escaping () throws -> T
      ) -> Future<T>

    Parameters

    executor

    is Executor to execute block on

    after

    is Double timeout in seconds to schedule block after

    cancellationToken

    is optional CancellationToken able to cancel execution of block and fail returned future with AsyncNinjaError.cancelled

    block

    is block to perform. Return from block will cause returned future to complete successfuly. Throw from block will returned future to complete with failure

    Return Value

    future

  • Asynchrounously executes block after timeout on executor and wraps returned value into future Makes future that will complete depending on block’s return/throw

    Declaration

    Swift

    public func flatFuture<T>(
      executor: Executor = .primary,
      after timeout: Double,
      cancellationToken: CancellationToken? = nil,
      _ block: @escaping () throws -> Future<T>
      ) -> Future<T>

    Parameters

    executor

    is Executor to execute block on

    after

    is Double timeout in seconds to schedule block after

    cancellationToken

    is optional CancellationToken able to cancel execution of block and fail returned future with AsyncNinjaError.cancelled

    block

    is block to perform. Return from block will cause returned future to complete with future. Throw from block will returned future to complete with failure

    Return Value

    future

  • Makes future that will complete depending on block’s return/throw

    Declaration

    Swift

    public func future<T, C: ExecutionContext>(
      context: C,
      executor: Executor? = nil,
      after timeout: Double,
      cancellationToken: CancellationToken? = nil,
      _ block: @escaping (_ strongContext: C) throws -> T
      ) -> Future<T>

    Parameters

    context

    is ExecutionContext to perform transform on. Instance of context will be passed as the first argument to the block. Block will not be executed if executor was deallocated before execution, returned future will fail with AsyncNinjaError.contextDeallocated error

    executor

    is Executor to override executor provided by context

    after

    is Double timeout in seconds to schedule block after

    cancellationToken

    is optional CancellationToken able to cancel execution of block and fail returned future with AsyncNinjaError.cancelled

    block

    is block to perform. Return from block will cause returned future to complete successfuly. Throw from block will returned future to complete with failure

    strongContext

    is ExecutionContext restored from weak reference of context passed to method

    Return Value

    future

  • Makes future that will complete depending on block’s return/throw

    Declaration

    Swift

    public func flatFuture<T, C: ExecutionContext>(
      context: C,
      executor: Executor? = nil,
      after timeout: Double,
      cancellationToken: CancellationToken? = nil,
      _ block: @escaping (_ strongContext: C) throws -> Future<T>
      ) -> Future<T>

    Parameters

    context

    is ExecutionContext to perform transform on. Instance of context will be passed as the first argument to the block. Block will not be executed if executor was deallocated before execution, returned future will fail with AsyncNinjaError.contextDeallocated error

    executor

    is Executor to override executor provided by context

    after

    is Double timeout in seconds to schedule block after

    cancellationToken

    is optional CancellationToken able to cancel execution of block and fail returned future with AsyncNinjaError.cancelled

    block

    is block to perform. Return from block will cause returned future to complete successfuly. Throw from block will returned future to complete with failure

    strongContext

    is ExecutionContext restored from weak reference of context passed to method

    Return Value

    future

  • Makes a platform independent Locking

    Declaration

    Swift

    public func makeLocking(isFair: Bool = false) -> Locking

    Parameters

    isFair

    determines if locking is fair

    Return Value

    constructed Locking

  • Makes or returns cached channel that reports of reachability changes

    Declaration

    Swift

    public func networkReachability(
      localAddress: NetworkAddress?,
      remoteAddress: NetworkAddress?
      ) -> Channel<SCNetworkReachabilityFlags, Void>

    Parameters

    localAddress

    local address associated with a network connection. If nil, only the remote address is of interest

    remoteAddress

    remote address associated with a network connection. If nil, only the local address is of interest

    Return Value

    channel that reports of reachability changes

  • Makes or returns cached channel that reports of reachability changes The channel will report of a internet connection availibility.

    Declaration

    Swift

    public func networkReachabilityForInternetConnection() -> Channel<SCNetworkReachabilityFlags, Void>

    Return Value

    channel that reports of reachability changes

  • Makes or returns cached channel that reports of reachability changes

    Declaration

    Swift

    public func networkReachability(address: NetworkAddress) -> Channel<SCNetworkReachabilityFlags, Void>

    Parameters

    address

    associated with a network connection.

    Return Value

    channel that reports of reachability changes

  • Makes or returns cached channel that reports of reachability changes

    Declaration

    Swift

    public func networkReachability(hostName: String) -> Channel<SCNetworkReachabilityFlags, Void>

    Parameters

    hostName

    associated with a network connection.

    Return Value

    channel that reports of reachability changes

  • Executes provided block multiple times until validation block returns true

    Declaration

    Swift

    public func tryExecute<T>(
      executor: Executor = .primary,
      validate: @escaping (_ completion: Fallible<T>) -> Bool,
      _ block: @escaping () throws -> T) -> Future<T>

    Parameters

    executor

    is Executor to execute block on

    validate

    returns true if completion is fine or false if it is not and block must be called again

    block

    to call. Value returned from the block will be validated

    Return Value

    future of validated value

  • Executes provided block and flattens result multiple times until validation block returns true

    Declaration

    Swift

    public func tryFlatExecute<T>(
      executor: Executor = .primary,
      validate: @escaping (_ completion: Fallible<T>) -> Bool,
      _ block: @escaping () throws -> Future<T>) -> Future<T>

    Parameters

    executor

    is Executor to execute block on

    validate

    returns true if completion is fine or false if it is not and block must be called again

    block

    to call. Value returned from the block will be flattented and validated

    Return Value

    future of validated value

  • Executes provided block multiple times until validation block returns true

    Declaration

    Swift

    public func tryExecute<T, C: ExecutionContext>(
      context: C,
      executor: Executor? = nil,
      validate: @escaping (_ strongContext: C, _ completion: Fallible<T>) -> Bool,
      _ block: @escaping (_ strongContext: C) throws -> T) -> Future<T>

    Parameters

    context

    is ExecutionContext to perform transform on. Instance of context will be passed as the first argument to the block. Block will not be executed if executor was deallocated before execution, returned future will fail with AsyncNinjaError.contextDeallocated error

    executor

    is Executor to override executor provided by context

    validate

    returns true if completion is fine or false if it is not and block must be called again

    block

    to call. Value returned from the block will be validated

    Return Value

    future of validated value

  • Executes provided block and flattens result multiple times until validation block returns true

    Declaration

    Swift

    public func tryFlatExecute<T, C: ExecutionContext>(
      context: C,
      executor: Executor? = nil,
      validate: @escaping (_ strongContext: C, _ completion: Fallible<T>) -> Bool,
      _ block: @escaping (_ strongContext: C) throws -> Future<T>) -> Future<T>

    Parameters

    context

    is ExecutionContext to perform transform on. Instance of context will be passed as the first argument to the block. Block will not be executed if executor was deallocated before execution, returned future will fail with AsyncNinjaError.contextDeallocated error

    executor

    is Executor to override executor provided by context

    validate

    returns true if completion is fine or false if it is not and block must be called again

    block

    to call. Value returned from the block will be flattented and validated

    Return Value

    future of validated value

  • Executes provided block specified amount of times or until block returns value (not throws an error)

    Declaration

    Swift

    public func tryExecute<T>(
      executor: Executor = .primary,
      times: Int,
      _ block: @escaping () throws -> T) -> Future<T>

    Parameters

    executor

    is Executor to execute block on

    times

    maximum amount of times the block will be executed

    block

    to call. Value returned from the block will be validated

    Return Value

    future of validated value

  • Executes provided block specified amount of times or until block returns future that completes successfully

    Declaration

    Swift

    public func tryFlatExecute<T>(
      executor: Executor = .primary,
      times: Int,
      _ block: @escaping () throws -> Future<T>) -> Future<T>

    Parameters

    executor

    is Executor to execute block on

    times

    maximum amount of times the block will be executed

    block

    to call. Value returned from the block will be validated

    Return Value

    future of validated value

  • Executes provided block specified amount of times or until block returns value (not throws an error)

    Declaration

    Swift

    public func tryExecute<T, C: ExecutionContext>(
      context: C,
      executor: Executor? = nil,
      times: Int,
      _ block: @escaping (_ strongContext: C) throws -> T) -> Future<T>

    Parameters

    context

    is ExecutionContext to perform transform on. Instance of context will be passed as the first argument to the block. Block will not be executed if executor was deallocated before execution, returned future will fail with AsyncNinjaError.contextDeallocated error

    executor

    is Executor to override executor provided by context

    times

    maximum amount of times the block will be executed

    block

    to call. Value returned from the block will be validated

    Return Value

    future of validated value

  • Executes provided block specified amount of times or until block returns future that completes successfully

    Declaration

    Swift

    public func tryFlatExecute<T, C: ExecutionContext>(
      context: C,
      executor: Executor? = nil,
      times: Int,
      _ block: @escaping (_ strongContext: C) throws -> Future<T>) -> Future<T>

    Parameters

    context

    is ExecutionContext to perform transform on. Instance of context will be passed as the first argument to the block. Block will not be executed if executor was deallocated before execution, returned future will fail with AsyncNinjaError.contextDeallocated error

    executor

    is Executor to override executor provided by context

    times

    maximum amount of times the block will be executed

    block

    to call. Value returned from the block will be validated

    Return Value

    future of validated value

  • Combines two futures

    Declaration

    Swift

    public func zip<A, B>(
      _ futureA: Future<A>,
      _ futureB: Future<B>
      ) -> Future<(A, B)>

    Parameters

    futureA

    first

    futureB

    second

    Return Value

    future of combined results. The future will complete right after completion of both futureA and futureB

  • Combines future and value

    Declaration

    Swift

    public func zip<A, B>(_ futureA: Future<A>, _ valueB: B) -> Future<(A, B)>

    Parameters

    futureA

    first

    valueB

    second

    Return Value

    future of combined results. The future will complete right after completion of futureA.

  • Combines three futures

    Declaration

    Swift

    public func zip<A, B, C>(
      _ futureA: Future<A>,
      _ futureB: Future<B>,
      _ futureC: Future<C>) -> Future<(A, B, C)>

    Parameters

    futureA

    first

    futureB

    second

    futureC

    third

    Return Value

    future of combined results. The future will complete right after completion of futureA, futureB, futureC

  • Combines two futures and one value

    Declaration

    Swift

    public func zip<A, B, C>(
      _ futureA: Future<A>,
      _ futureB: Future<B>,
      _ valueC: C
      ) -> Future<(A, B, C)>

    Parameters

    futureA

    first

    futureB

    second

    valueC

    third

    Return Value

    future of combined results. The future will complete right after completion of futureA and futureB.

  • Combines one future and two value

    Declaration

    Swift

    public func zip<A, B, C>(
      _ futureA: Future<A>,
      _ valueB: B,
      _ valueC: C
      ) -> Future<(A, B, C)>

    Parameters

    futureA

    first

    valueB

    second

    valueC

    third

    Return Value

    future of combined results. The future will complete right after completion of futureA.

  • Binds two event streams bidirectionally.

    Declaration

    Swift

    public func doubleBind<T: EventSource&EventDestination, U: EventSource&EventDestination>(
      _ majorStream: T,
      _ minorStream: U,
      valueTransformer: ValueTransformer)

    Parameters

    majorStream

    a stream to bind to. This stream has a priority during initial synchronization

    minorStream

    a stream to bind to.

    valueTransformer

    ValueTransformer to use to transform from T.Update to U.Update and reverse