Skip to content

Commit

Permalink
Add branch awareness and tests for the type of 'alt'
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq committed Jan 8, 2021
1 parent 6885efd commit 06bc7c9
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
36 changes: 32 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,38 @@ export function after(duration: number): <R>(value: R) => Resolved<R>
export function and<L, R>(left: FutureInstance<L, R>): (right: FutureInstance<L, any>) => FutureInstance<L, R>

/** Logical or for Futures. See https://github.com/fluture-js/Fluture#alt */
export function alt<L, R>(left: FutureInstance<L, R>): (right: FutureInstance<L, R>) => FutureInstance<L, R>

/** Race two ConcurrentFutures. See https://github.com/fluture-js/Fluture#alt */
export function alt<L, R>(left: ConcurrentFutureInstance<L, R>): (right: ConcurrentFutureInstance<L, R>) => ConcurrentFutureInstance<L, R>
export const alt: {
<F extends AnyFuture, S extends AnyFuture>(second: F extends Never ? S : never): (first: F) => Never
<F extends AnyFuture, S extends AnyFuture>(second: F extends Rejected<unknown> ? S : never): (first: F) => S
<F extends AnyFuture, S extends AnyFuture>(second: F extends Resolved<unknown> ? S : never): (first: F) => F

<L>(second: Rejected<L>): {
(first: Never): Never
(first: Rejected<any>): Rejected<L>
<R>(first: Resolved<R>): Resolved<R>
<R>(first: Uncertain<any, R>): Uncertain<L, R>
}

<L, R>(second: Uncertain<L, R>): {
<T>(first: Resolved<T>): Resolved<T>
(first: Rejected<any>): Uncertain<L, R>
(first: Uncertain<any, R>): Uncertain<L, R>
}

(second: ConcurrentNever): <L, R>(first: ConcurrentUncertain<L, R>) => ConcurrentUncertain<L, R>

<L>(second: ConcurrentRejected<L>): {
<R>(first: ConcurrentResolved<R>): ConcurrentUncertain<L, R>
<R>(first: ConcurrentUncertain<L, R>): ConcurrentUncertain<L, R>
}

<R>(second: ConcurrentResolved<R>): {
<L>(first: ConcurrentRejected<L>): ConcurrentUncertain<L, R>
<L>(first: ConcurrentUncertain<L, R>): ConcurrentUncertain<L, R>
}

<L, R>(second: ConcurrentUncertain<L, R>): (first: ConcurrentUncertain<L, R>) => ConcurrentUncertain<L, R>
}

/** Apply the function in the right Future to the value in the left Future. See https://github.com/fluture-js/Fluture#ap */
export function ap<L, RA>(value: FutureInstance<L, RA>): <RB>(apply: FutureInstance<L, (value: RA) => RB>) => FutureInstance<L, RB>
Expand Down
56 changes: 56 additions & 0 deletions test/types/alt.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {expectType, expectError} from 'tsd';

import * as fl from '../../index.js';

const fsn: fl.FutureInstance<string, number> = fl.resolve (42);
const fns: fl.FutureInstance<number, string> = fl.resolve ('a');

// Standard usage on Future instances.
expectType<fl.Never> (fl.alt (fl.never) (fl.never));
expectType<fl.Never> (fl.alt (fl.reject ('a')) (fl.never));
expectType<fl.Never> (fl.alt (fl.resolve ('a')) (fl.never));
expectType<fl.Never> (fl.alt (fl.never) (fl.reject ('a')));
expectType<fl.Resolved<string>> (fl.alt (fl.never) (fl.resolve ('a')));
expectType<fl.Resolved<number>> (fl.alt (fl.reject ('a')) (fl.resolve (42)));
expectType<fl.Resolved<number>> (fl.alt (fl.resolve (42)) (fl.reject ('a')));
expectType<fl.Resolved<number>> (fl.alt (fl.resolve (42)) (fl.resolve (42)));
expectType<fl.Rejected<number>> (fl.alt (fl.reject (42)) (fl.reject (42)));
expectType<fl.Rejected<string>> (fl.alt (fl.reject ('a')) (fl.reject (42)));
expectType<fl.Uncertain<string, number>> (fl.alt (fsn) (fsn));
expectType<fl.Resolved<number>> (fl.alt (fl.resolve ('a')) (fl.resolve (42)));
expectError (fl.alt (fsn) (fns));

const x = fl.alt (fl.never) (fl.never)

// Usage with pipe on Future instances (https://git.io/JLx3F).
expectType<fl.Never> ((fl.never) .pipe (fl.alt (fl.never)));
expectType<fl.Never> ((fl.never) .pipe (fl.alt (fl.reject ('a'))));
expectType<fl.Never> ((fl.never) .pipe (fl.alt (fl.resolve ('a'))));
expectType<fl.Never> ((fl.reject ('a')) .pipe (fl.alt (fl.never)));
expectType<fl.Resolved<string>> ((fl.resolve ('a')) .pipe (fl.alt (fl.never)));
expectType<fl.Resolved<number>> ((fl.resolve (42)) .pipe (fl.alt (fl.reject ('a'))));
expectType<fl.Resolved<number>> ((fl.reject ('a')) .pipe (fl.alt (fl.resolve (42))));
expectType<fl.Resolved<number>> ((fl.resolve (42)) .pipe (fl.alt (fl.resolve (42))));
expectType<fl.Rejected<number>> ((fl.reject (42)) .pipe (fl.alt (fl.reject (42))));
expectType<fl.Rejected<string>> ((fl.reject (42)) .pipe (fl.alt (fl.reject ('a'))));
expectType<fl.Uncertain<string, number>> ((fsn) .pipe (fl.alt (fsn)));
expectType<fl.Resolved<number>> ((fl.resolve (42)) .pipe (fl.alt (fl.resolve ('a'))));
expectError ((fns) .pipe (fl.alt (fsn)));

const csn: fl.ConcurrentFutureInstance<string, number> = fl.Par (fl.resolve (42));
const cns: fl.ConcurrentFutureInstance<number, string> = fl.Par (fl.resolve ('a'));

// Standard usage on ConcurrentFuture instances.
expectType<fl.ConcurrentNever> (fl.alt (fl.Par (fl.never)) (fl.Par (fl.never)));
expectType<fl.ConcurrentRejected<string>> (fl.alt (fl.Par (fl.reject ('a'))) (fl.Par (fl.never)));
expectType<fl.ConcurrentResolved<string>> (fl.alt (fl.Par (fl.resolve ('a'))) (fl.Par (fl.never)));
expectType<fl.ConcurrentRejected<string>> (fl.alt (fl.Par (fl.never)) (fl.Par (fl.reject ('a'))));
expectType<fl.ConcurrentResolved<string>> (fl.alt (fl.Par (fl.never)) (fl.Par (fl.resolve ('a'))));
expectType<fl.ConcurrentFutureInstance<string, number>> (fl.alt (fl.Par (fl.reject ('a'))) (fl.Par (fl.resolve (42))));
expectType<fl.ConcurrentFutureInstance<string, number>> (fl.alt (fl.Par (fl.resolve (42))) (fl.Par (fl.reject ('a'))));
expectType<fl.ConcurrentResolved<number>> (fl.alt (fl.Par (fl.resolve (42))) (fl.Par (fl.resolve (42))));
expectType<fl.ConcurrentRejected<number>> (fl.alt (fl.Par (fl.reject (42))) (fl.Par (fl.reject (42))));
expectType<fl.ConcurrentUncertain<string, number>> (fl.alt (csn) (csn));
expectError (fl.alt (fl.Par (fl.resolve ('a'))) (fl.Par (fl.resolve (42))));
expectError (fl.alt (fl.Par (fl.reject ('a'))) (fl.Par (fl.reject (42))));
expectError (fl.alt (csn) (cns));

0 comments on commit 06bc7c9

Please sign in to comment.