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

Provide progress parameter via fold pending parameter #20

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions src/__tests__/remote-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,16 @@ describe('RemoteData', () => {
});
describe('fold', () => {
it('initial', () => {
expect(initialRD.fold(1, 2, () => 3, () => 4)).toBe(1);
expect(initialRD.fold(1, () => 2, () => 3, () => 4)).toBe(1);
});
it('pending', () => {
expect(pendingRD.fold(1, 2, () => 3, () => 4)).toBe(2);
expect(pendingRD.fold(1, () => 2, () => 3, () => 4)).toBe(2);
});
it('failure', () => {
expect(failureRD.fold(1, 2, () => 3, () => 4)).toBe(3);
expect(failureRD.fold(1, () => 2, () => 3, () => 4)).toBe(3);
});
it('success', () => {
expect(successRD.fold(1, 2, () => 3, () => 4)).toBe(4);
expect(successRD.fold(1, () => 2, () => 3, () => 4)).toBe(4);
});
});
describe('foldL', () => {
Expand Down
80 changes: 60 additions & 20 deletions src/remote-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ export class RemoteInitial<L, A> {
*
* `success(21).fold(foldInitial, foldPending, foldFailure, foldSuccess) will return 22`
*/
fold<B>(initial: B, pending: B, failure: Function1<L, B>, success: Function1<A, B>): B {
fold<B>(
initial: B,
pending: Function1<Option<RemoteProgress>, B>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a massive breaking change. Would foldL be enough for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I can split the changes and provide the breaking change in a separate pull request?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the problem is actually deeper. If we start processing pendings with functions then we should also change pending helper which immediately turns out to be the same as progress. Smth one should be left.
/cc @sutarmin @OliverJAsh

This may be a single PR for the next breaking 0.4 version together with #5

failure: Function1<L, B>,
success: Function1<A, B>,
): B {
return initial;
}

Expand All @@ -177,7 +182,12 @@ export class RemoteInitial<L, A> {
*
* rest of example is similar to `fold`
*/
foldL<B>(initial: Lazy<B>, pending: Lazy<B>, failure: Function1<L, B>, success: Function1<A, B>): B {
foldL<B>(
initial: Lazy<B>,
pending: Function1<Option<RemoteProgress>, B>,
failure: Function1<L, B>,
success: Function1<A, B>,
): B {
return initial();
}

Expand Down Expand Up @@ -426,7 +436,7 @@ export class RemoteFailure<L, A> {
}

ap<B>(fab: RemoteData<L, Function1<A, B>>): RemoteData<L, B> {
return fab.fold(initial, fab, () => fab as any, () => this); //tslint:disable-line no-use-before-declare
return fab.fold(initial, () => fab, () => fab as any, () => this); //tslint:disable-line no-use-before-declare
}

chain<B>(f: Function1<A, RemoteData<L, B>>): RemoteData<L, B> {
Expand All @@ -437,11 +447,21 @@ export class RemoteFailure<L, A> {
return this as any;
}

fold<B>(initial: B, pending: B, failure: Function1<L, B>, success: Function1<A, B>): B {
fold<B>(
initial: B,
pending: Function1<Option<RemoteProgress>, B>,
failure: Function1<L, B>,
success: Function1<A, B>,
): B {
return failure(this.error);
}

foldL<B>(initial: Lazy<B>, pending: Lazy<B>, failure: Function1<L, B>, success: Function1<A, B>): B {
foldL<B>(
initial: Lazy<B>,
pending: Function1<Option<RemoteProgress>, B>,
failure: Function1<L, B>,
success: Function1<A, B>,
): B {
return failure(this.error);
}

Expand Down Expand Up @@ -538,7 +558,7 @@ export class RemoteSuccess<L, A> {
}

ap<B>(fab: RemoteData<L, Function1<A, B>>): RemoteData<L, B> {
return fab.fold(initial, fab, () => fab as any, value => this.map(value)); //tslint:disable-line no-use-before-declare
return fab.fold(initial, () => fab, () => fab as any, value => this.map(value)); //tslint:disable-line no-use-before-declare
}

chain<B>(f: Function1<A, RemoteData<L, B>>): RemoteData<L, B> {
Expand All @@ -549,11 +569,21 @@ export class RemoteSuccess<L, A> {
return of(f(this)); //tslint:disable-line no-use-before-declare
}

fold<B>(initial: B, pending: B, failure: Function1<L, B>, success: Function1<A, B>): B {
fold<B>(
initial: B,
pending: Function1<Option<RemoteProgress>, B>,
failure: Function1<L, B>,
success: Function1<A, B>,
): B {
return success(this.value);
}

foldL<B>(initial: Lazy<B>, pending: Lazy<B>, failure: Function1<L, B>, success: Function1<A, B>): B {
foldL<B>(
initial: Lazy<B>,
pending: Function1<Option<RemoteProgress>, B>,
failure: Function1<L, B>,
success: Function1<A, B>,
): B {
return success(this.value);
}

Expand Down Expand Up @@ -652,7 +682,7 @@ export class RemotePending<L, A> {
ap<B>(fab: RemoteData<L, Function1<A, B>>): RemoteData<L, B> {
return fab.fold(
initial, //tslint:disable-line no-use-before-declare
fab.isPending() ? (concatPendings(this, fab as any) as any) : this,
() => (fab.isPending() ? (concatPendings(this, fab as any) as any) : this),
() => this,
() => this,
);
Expand All @@ -666,12 +696,22 @@ export class RemotePending<L, A> {
return pending; //tslint:disable-line no-use-before-declare
}

fold<B>(initial: B, pending: B, failure: Function1<L, B>, success: Function1<A, B>): B {
return pending;
fold<B>(
initial: B,
pending: Function1<Option<RemoteProgress>, B>,
failure: Function1<L, B>,
success: Function1<A, B>,
): B {
return pending(this.progress);
}

foldL<B>(initial: Lazy<B>, pending: Lazy<B>, failure: Function1<L, B>, success: Function1<A, B>): B {
return pending();
foldL<B>(
initial: Lazy<B>,
pending: Function1<Option<RemoteProgress>, B>,
failure: Function1<L, B>,
success: Function1<A, B>,
): B {
return pending(this.progress);
}

getOrElseL(f: Lazy<A>): A {
Expand Down Expand Up @@ -838,10 +878,10 @@ export const getOrd = <L, A>(OL: Ord<L>, OA: Ord<A>): Ord<RemoteData<L, A>> => {
compare: (x, y) =>
sign(
x.foldL(
() => y.fold(0, -1, () => -1, () => -1),
() => y.fold(1, 0, () => -1, () => -1),
xError => y.fold(1, 1, yError => OL.compare(xError, yError), () => -1),
xValue => y.fold(1, 1, () => 1, yValue => OA.compare(xValue, yValue)),
() => y.fold(0, () => -1, () => -1, () => -1),
() => y.fold(1, () => 0, () => -1, () => -1),
xError => y.fold(1, () => 1, yError => OL.compare(xError, yError), () => -1),
xValue => y.fold(1, () => 1, () => 1, yValue => OA.compare(xValue, yValue)),
),
),
};
Expand All @@ -852,11 +892,11 @@ export const getSemigroup = <L, A>(SL: Semigroup<L>, SA: Semigroup<A>): Semigrou
return {
concat: (x, y) => {
return x.foldL(
() => y.fold(y, y, () => y, () => y),
() => y.fold(y, () => y, () => y, () => y),
() => y.foldL(() => x, () => concatPendings(x as any, y as any), () => y, () => y),

xError => y.fold(x, x, yError => failure(SL.concat(xError, yError)), () => y),
xValue => y.fold(x, x, () => x, yValue => success(SA.concat(xValue, yValue))),
xError => y.fold(x, () => x, yError => failure(SL.concat(xError, yError)), () => y),
xValue => y.fold(x, () => x, () => x, yValue => success(SA.concat(xValue, yValue))),
);
},
};
Expand Down