Skip to content

Commit

Permalink
provideEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 24, 2024
1 parent df5740d commit a3352b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/fpdart/lib/src/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
/// {@category do_notation}
Effect<Null, L, R> provide(E env) => Effect._((_) => _unsafeRun(env));

/// {@category do_notation}
Effect<V, L, R> provideEffect<V>(Effect<V, L, E> effect) => Effect._(
(env) => effect._unsafeRun(env).then(
(exit) => switch (exit) {
Left(value: final cause) => Left(cause),
Right(value: final value) => _unsafeRun(value),
},
),
);

/// {@category do_notation}
static Effect<E, L, E> env<E, L>() => Effect._(
(env) => Right(env),
Expand Down
24 changes: 24 additions & 0 deletions packages/fpdart/test/src/effect/effect_do_notation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ void main() {
});
});

group('provideEffect', () {
test('valid dependency', () {
final main = Effect<int, String, int>.gen(($) {
final env = $.sync(Effect.env());
return env + 1;
});

final program = main.provideEffect<Null>(Effect.succeed(10));
final result = program.runSyncVoid();
expect(result, 11);
});

test('invalid dependency', () {
final main = Effect<int, String, int>.gen(($) {
final env = $.sync(Effect.env());
return env + 1;
});

final program = main.provideEffect<Null>(Effect.fail("error"));
final result = program.flip().runSyncVoid();
expect(result, "error");
});
});

group('mapEnv', () {
test('adapt dependency from another program', () {
final subMain =
Expand Down

0 comments on commit a3352b5

Please sign in to comment.