Options
All
  • Public
  • Public/Protected
  • All
Menu

A write interface for Future to use when implementing producers.

This would be the equivalent of the now deprecated Deferred data type in JavaScript.

Example:

import { Future, FutureMaker, Scheduler, Success } from "funfix"

const ec = Scheduler.global.get()
const m = FutureMaker.empty<number>()

// The producer
ec.scheduleOnce(1000, () => m.complete(Success(1)))

// The future that will eventually complete when
// `m.complete` gets called
const f: Future<number> = maker.future()

Type parameters

  • A

Hierarchy

  • FutureMaker

Index

Methods

chainTo

  • Chains this to target such that any subsequent operations on this future maker is reflected on target.

    const main = FutureMaker.empty<number>()
    const child = FutureMaker.empty<number>()
    
    // Now all operations on `child` will be redirected to `main`
    child.chainTo(main)
    
    // Completing `child` will complete `main`
    child.complete(Success(1))
    
    main.future() //=> Yields 1
    child.future() //=> Yields 1
    

    The purpose of this method is the same as with ChainedCancelable, to be used in pieces of logic where the chaining of onComplete calls creates a memory leaks, chaining being used to get rid of such chains.

    This method is being used in the implementation of Future.flatMap for example to make it memory safe.

    CREDITS: this was inspired by Scala's scala.concurrent.Scala implementation.

    Parameters

    Returns void

complete

  • complete(r: Try<A>): void
  • Completes this FutureMaker either with a successful value or with a failure, but throws an exception if this maker was already completed.

    Due to throwing exceptions, this function is recommended for usage in cases where there's a guarantee that the completion of the FutureMaker is attempted only once.

    const m = FutureMaker.empty<number>()
    
    m.complete(Success(1))
    
    m.complete(Success(2)) //=> throws IllegalStateError
    

    In case you have a concurrent race, see tryComplete for a version that does not throw exceptions.

    Parameters

    • r: Try<A>

    Returns void

failure

  • failure(error: Throwable): void
  • Alias for complete(Failure(value)).

    See complete.

    Parameters

    • error: Throwable

    Returns void

future

  • Creates and returns a Future that will complete when this future maker is completed.

    const m = FutureMaker.empty<number>()
    
    // Creates a simple future, no cancellation logic:
    m.future()
    
    // Creates a future with baked in cancellation logic:
    const cRef = Cancelable.of(() => console.log("Cancelled!"))
    m.future(cRef)
    

    Parameters

    • Optional cancelable: ICancelable

      is an optional reference that can indicate cancellation logic to be baked into the created future

    Returns Future<A>

success

  • success(value: A): void
  • Alias for complete(Success(value)).

    See complete.

    Parameters

    • value: A

    Returns void

tryComplete

  • tryComplete(r: Try<A>): boolean
  • Tries to complete this future builder either with a successful value or with a failure.

    This function can be used in concurrent races where multiple actors compete for completing the same FutureMaker.

    const m = FutureMaker.empty<number>()
    
    m.tryComplete(Success(1)) //=> true
    m.tryComplete(Success(2)) //=> false
    
    m.future() //=> Yields 1
    

    In case you have a guarantee that the completion only happens once, then usage of complete is recommended.

    Parameters

    • r: Try<A>

    Returns boolean

    false in case the FutureMaker has been already completed, or true otherwise

tryFailure

  • tryFailure(error: Throwable): boolean
  • Alias for tryComplete(Failure(error)).

    See tryComplete.

    Parameters

    • error: Throwable

    Returns boolean

trySuccess

  • trySuccess(value: A): boolean

withScheduler

  • Returns a new FutureMaker that mirrors the state of the source, but that uses the given Scheduler reference for managing the required async boundaries.

    The given Scheduler reference is used for inserting async boundaries when the registered listeners are triggered when .complete is called or for data transformations executed on the future references returned by .future.

    See Future.withScheduler.

    Parameters

    Returns FutureMaker<A>

Static completed

  • Returns an already completed FutureMaker reference.

    Example:

    const m = FutureMaker.completed(Success(1))
    
    m.future() // Yields 1
    
    m.complete(Success(2)) // Throws IllegalStateError
    

    If all you need is a Future, then use Future.fromTry instead.

    Type parameters

    • A

    Parameters

    • value: Try<A>
    • Default value ec: Scheduler = Scheduler.global.get()

    Returns FutureMaker<A>

Static empty

  • Returns an empty FutureMaker reference awaiting completion.

    This is the builder that one should use for building FutureMaker instances, since the default constructor is not exposed due to it exposing internal state.

    Type parameters

    • A

    Parameters

    • Default value ec: Scheduler = Scheduler.global.get()

    Returns FutureMaker<A>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc