Skip to content

Commit

Permalink
fix fail in Effect.gen
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 22, 2024
1 parent 0f91eeb commit f7cdc3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
24 changes: 14 additions & 10 deletions packages/fpdart/lib/src/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract interface class IEffect<E, L, R> {
}

final class Effect<E, L, R> extends IEffect<E, L, R> {
/// `E?` is optional to allow [Never] to work (`provideNever`).
/// `E?` is optional to allow [Never] to work.
///
/// In practice a user of the library should never be allowed to pass `null` as [E].
final FutureOr<Exit<L, R>> Function(E? env) _unsafeRun;
Expand Down Expand Up @@ -153,14 +153,18 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
/// {@category constructors}
factory Effect.gen(DoFunctionEffect<E, L, R> f) => Effect<E, L, R>._(
(env) {
return f(_effectGen<E, L>(env)).then(
Right.new,
onError: (dynamic error, dynamic stackTrack) {
if (error is _EffectThrow<L>) {
return Left<Cause<L>, R>(error.cause);
}
},
);
try {
return f(_effectGen<E, L>(env)).then(
Right.new,
onError: (dynamic error) {
if (error is _EffectThrow<L>) {
return Left<Cause<L>, R>(error.cause);
}
},
);
} on _EffectThrow<L> catch (genError) {
return Left(genError.cause);
}
},
);

Expand Down Expand Up @@ -297,7 +301,7 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
);

/// {@category mapping}
Effect<E, R, L> get flip => Effect._(
Effect<E, R, L> flip() => Effect._(
(env) => _unsafeRun(env).then(
(exit) => switch (exit) {
Left(value: final cause) => switch (cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {
Effect.functionSucceed(() => mutable += 1),
Effect.fail("0"),
]);
final result = main.flip.runSync(null);
final result = main.flip().runSync(null);
expect(mutable, 0);
expect(result, "10");
});
Expand Down
8 changes: 4 additions & 4 deletions packages/fpdart/test/src/effect/effect_constructors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {

test('fail', () {
final main = Effect.fail(10);
final result = main.flip.runSync(null);
final result = main.flip().runSync(null);
expect(result, 10);
});

Expand Down Expand Up @@ -62,11 +62,11 @@ void main() {

test('sync fail', () {
final main = Effect<Never, String, int>.gen(($) {
final value = $.sync(Effect.fail("10"));
final value = $.sync(Effect.fail("abc"));
return value;
});
final result = main.flip.runSyncNoEnv();
expect(result, "10");
final result = main.flip().runSyncNoEnv();
expect(result, "abc");
});

test('async succeed', () async {
Expand Down

0 comments on commit f7cdc3d

Please sign in to comment.