Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "main"

Index

Type aliases

Dataway

Dataway<E, A>: NotAsked | Loading | Failure<E> | Success<A>

This type represents the four possible states of a remote datasource fetching result.

  1. The data was not asked yet but eventually will be
  2. The data is being loaded, it can take time
  3. The data fetching ended up in error at any time during the loading or processing of it
  4. The data has been successfully retrieved.

The order of transformation should look like this NotAsked -> Loading -> Error<E> | Success<A>

Type parameters

  • E

    the expected error type

  • A

    the expected value type

Functions

Const append

  • if both argument are Success return a tuple containing both values wrapped in a Success

    if not, the leftmost argument with the highest priority as defined bellow will be returned

    Failure being the highest priority, Loading being the lowest

    Failure -> NotAsked -> Loading

    import { append, failure, success } from  'dataway';
    
    append(success('a'), success('b')) === success(['a', 'b']);
    append(failure('a'), success('b')) === failure('a');
    append(failure('a'), failure('b')) === failure('a');

    Type parameters

    • E

    • A

    • B

    Parameters

    Returns Dataway<E, [A, B]>

failure

  • failure<E, A>(failure: E): Dataway<E, A>
  • Type parameters

    • E

    • A

    Parameters

    • failure: E

    Returns Dataway<E, A>

Const fold

  • fold<E, A, B>(onNotAsked: function, onLoading: function, onFailure: function, onSuccess: function, monadA: Dataway<E, A>): B
  • Fold is the only way to attempt to safely extract the data, because it force you to consider the four variations of the dataway state. The following example demonstrate how useful this is when trying to render a search result. We may want to notify the user why no result did show up on his screen

    1. The search was not initiated by the user (he didn't click on the search button).
    2. The search could be initiated but not done yet (search can take time), a loading visual hint could be displayed.
    3. The search could end up in error (timeout, connectivity issue or plain server crash), an error message can be shown or a message to try again.
    4. The search can success and we want to display the result to the user.
    import { dataway, fold } from "dataway";
    
    const searchResult = dataway.of(["result1", "result2", "result3", "result4"]);
    
    fold(
      () => "<span>use the Search button for nice code snippet</span>",
      () => "<span>Search initiated, awesome code snippets incomings !</span>",
      error => `<span>ho no ! ${error} did happen, please retry</span>`,
      results => `<ul>${results.map(result => `<li>${result}</li>`)}</ul>`,
      searchResult
    );

    Type parameters

    • E

      the error type you expect

    • A

      the success type you expect

    • B

      the type of value that you want to produce

    Parameters

    • onNotAsked: function
        • (): B
        • Returns B

    • onLoading: function
        • (): B
        • Returns B

    • onFailure: function
        • (failure: E): B
        • Parameters

          • failure: E

          Returns B

    • onSuccess: function
        • (success: A): B
        • Parameters

          • success: A

          Returns B

    • monadA: Dataway<E, A>

    Returns B

Const fromEither

  • Constructs a new Dataway from a fp-ts Either type. If the value is a Left value it returns Failure value otherwise it returns the Right value wrapped in a Success

    parameter Result
    Left error Failure error
    Right foo Success foo
    import { failure, success, fromEither } from  'dataway';
    import { left, right } from 'fp-ts/lib/Either';
    
    
    fromEither(left(foo)) === failure(foo);
    fromEither(right(bar)) === success(bar);

    Type parameters

    • E

      type of the provided Left wrapped value

    • A

      type of the provided Right wrapped value

    Parameters

    • either: Either<E, A>

    Returns NotAsked | Loading | Failure<E> | Success<A>

fromNullable

  • fromNullable<E, A>(successValue: A | null | undefined): Dataway<E, A>
  • Constructs a new Dataway from a nullable type. If the value is null or undefined it returns NotAsked, otherwise it returns the value wrapped in a Success

    parameter Result
    null NotAsked
    undefined NotAsked
    foo Success foo
    import { notAsked, success, fromNullable } from  'dataway';
    
    fromNullable(null) === notAsked;
    fromNullable('foo') === success('foo');

    Type parameters

    • E

    • A

      type of the provided value

    Parameters

    • successValue: A | null | undefined

    Returns Dataway<E, A>

Const fromOption

  • fromOption<E>(defaultFailure: E): (Anonymous function)
  • Constructs a new Dataway from a fp-ts Option type. If the value is a None it returns NotAsked otherwise it returns the Some value wrapped in a Success

    parameter Result
    None NotAsked
    Some foo Success foo
    import { failure, success, fromEither } from  'dataway';
    import { none, some } from 'fp-ts/lib/Option';
    
    
    fromEither(none) === notAsked;
    fromEither(some(foo)) === success(foo);

    Type parameters

    • E

    Parameters

    • defaultFailure: E

    Returns (Anonymous function)

Const getEq

  • getEq<E, A>(EE: Eq<E>, EA: Eq<A>): Eq<Dataway<E, A>>
  • Compare two dataway

    import { getEq } from "dataway";
    
    const E = getEq(eqNumber, eqString);
    
    E.equals(notAsked, notAsked) // true
    E.equals(loading, loading) // true
    E.equals(failure(1), failure(2)) // false
    E.equals(failure(1), failure(1)) // true
    E.equals(success('hello'), success('olleh')) // false
    E.equals(success('hello'), success('hello')) // true
    

    Type parameters

    • E

    • A

    Parameters

    • EE: Eq<E>

      Eq instance to compare failure values

    • EA: Eq<A>

      Eq instance to compare success values

    Returns Eq<Dataway<E, A>>

isFailure

  • isFailure<E, A>(monadA: Dataway<E, A>): boolean
  • Type parameters

    • E

    • A

    Parameters

    Returns boolean

isLoading

  • isLoading<E, A>(monadA: Dataway<E, A>): boolean
  • Type parameters

    • E

    • A

    Parameters

    Returns boolean

isNotAsked

  • isNotAsked<E, A>(monadA: Dataway<E, A>): boolean
  • Type parameters

    • E

    • A

    Parameters

    Returns boolean

isSuccess

  • isSuccess<E, A>(monadA: Dataway<E, A>): boolean
  • Type parameters

    • E

    • A

    Parameters

    Returns boolean

Const map2

  • apply the function f if both arguments are Success

    if not, the leftmost argument with the highest priority as defined bellow will be returned

    Failure being the highest priority, Loading being the lowest

    Failure -> NotAsked -> Loading

    import { map2, failure, success } from  'dataway';
    
    const add = val1 => val2 => val1 + val2;
    
    map2(add, success(3), success(5)) === success(8);
    map2(add, failure('a'), success(5)) === failure('a');
    map2(add, failure('a'), failure('b')) === failure('a');

    Type parameters

    • E

    • A

    • B

    • C

    Parameters

    • f: function
        • (a: A): function
        • Parameters

          • a: A

          Returns function

            • (b: B): C
            • Parameters

              • b: B

              Returns C

    • functorA: Dataway<E, A>
    • functorB: Dataway<E, B>

    Returns Dataway<E, C>

Const map3

  • apply the function f if all arguments are Success

    if not, the leftmost argument with the highest priority as defined bellow will be returned

    Failure being the highest priority, Loading being the lowest

    Failure -> NotAsked -> Loading

    import { map3, failure, success } from  'dataway';
    
    const cubic = val1 => val2 => val3 => val1 * val2 * val3;
    
    map3(cubic, success(3), success(5), success(2)) === success(16);
    map3(cubic, failure('a'), success(5), loading) === failure('a');
    map3(cubic, success(3), loading, notAsked) === notAsked;

    Type parameters

    • E

    • A

    • B

    • C

    • D

    Parameters

    • f: function
        • (a: A): function
        • Parameters

          • a: A

          Returns function

            • (b: B): function
            • Parameters

              • b: B

              Returns function

                • (c: C): D
                • Parameters

                  • c: C

                  Returns D

    • functorA: Dataway<E, A>
    • functorB: Dataway<E, B>
    • functorC: Dataway<E, C>

    Returns Dataway<E, D>

success

  • success<E, A>(success: A): Dataway<E, A>
  • Type parameters

    • E

    • A

    Parameters

    • success: A

    Returns Dataway<E, A>

Object literals

Const dataway

dataway: object

of

of: success = success

ap

  • apply MonadA value to MonadAtoB function if both are Success to procude a MonadB

    following table illustrate the dataway ap combinations

    Monad(a -> b) Monad(a) Result
    success(a -> b) succes(a) success(b)
    success(a -> b) notAsked notAsked
    success(a -> b) loading loading
    success(a -> b) failure(c) failure(c)
    notasked failure(c) failure(c)
    loading failure(c) failure(c)
    failure(d) failure(c) failure(d)

    Parameters

    Returns NotAsked | Loading | Failure<E> | Success<B>

chain

  • Allow to turn function a -> Dataway b into a Dataway a -> Dataway b Where Dataway b can use a different constructor than Dataway a

    f(a -> Dataway b) Monad(a) Result
    f(a -> Success b) success(a) success(b)
    f(a -> Failure b) success(a) failure(b)
    f(a -> Dataway b) success(a) Dataway b

    this allow us to chain function that can produce different variance of Dataway

    import { dataway, success, failure, loading } from "dataway";
    
    const jsonParse = (json: string) => {
    try {
    return success(JSON.parse(json));
    } catch (error) {
    return failure(error);
    }
    };
    
    const checkPayload = payload => {
    if (payload.hasOwnProperty("failure")) {
    return failure(payload.failure);
    }
    return success(payload);
    };
    
    const validate = data =>
    dataway.chain(dataway.chain(data, jsonParse), checkPayload);
    
    validate(success("{ \"articles\" : []}"));
    // => Success Object
    validate(success("{ : []}")));
    // => Failure SyntaxError
    validate(success("{ \"failure\" : \"server returned 403\"}")));
    // => Failure "server returned 403"
    validate(loading)
    // => Loading;

    Parameters

    Returns NotAsked | Loading | Failure<E> | Success<B>

map

  • Allow to turn function a -> b into a Dataway a -> Dataway b Where Dataway b use the same constructor value as Dataway a

    f(a -> b) Functor(a) Result
    f(a -> b) success(a) success(b)
    f(a -> b) notAsked notAsked
    f(a -> b) loading loading
    f(a -> b) failure(c) failure(c)

    Parameters

    Returns NotAsked | Loading | Failure<E> | Success<B>

Const loading

loading: object

_tag

_tag: "Loading" = "Loading"

Const notAsked

notAsked: object

_tag

_tag: "NotAsked" = "NotAsked"

Generated using TypeDoc