Skip to content

Commit

Permalink
Deferred as Effect
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 27, 2024
1 parent e4d3ad2 commit ac7f9e1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/fpdart/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ final option = Some(10);
final effect = Effect<Env, Error, Success>.gen(($) {
final eitherValue = $.sync(either);
final optionValue = $.sync(option);
final deferred = $.sync(Deferred<Error, Success>());
return eitherValue + optionValue;
});
12 changes: 9 additions & 3 deletions packages/fpdart/lib/src/deferred.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'effect.dart';

final class Deferred<L, R> extends IEffect<Null, L, R> {
final class Deferred<L, R> extends IEffect<Never, L, Option<R>> {
Option<Exit<L, R>> _value = None();

Completer<Exit<L, R>>? __completer;
Expand All @@ -17,8 +17,14 @@ final class Deferred<L, R> extends IEffect<Null, L, R> {
bool get unsafeCompleted => _value is Some<Exit<L, R>>;

@override
Effect<Null, L, R> get asEffect => Effect.from(
(_) => await<Null>()._unsafeRun(Context.env(null)),
Effect<Never, L, Option<R>> get asEffect => Effect.from(
(_) => switch (_value) {
None() => Right(None()),
Some(value: final exit) => switch (exit) {
Left() => Right(None()),
Right(value: final value) => Right(Some(value)),
},
},
);

Effect<E, L, R> await<E>() => Effect.from(
Expand Down
3 changes: 3 additions & 0 deletions packages/fpdart/lib/src/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
/// {@category delay}
Effect<E, L, R> timeout(Duration duration) =>
race(Effect<E, L, R>.failCause(const Interrupted()).delay(duration));

/// {@category interruption}
Effect<E, Never, R> interrupt() => Effect.failCause(const Interrupted());
}

extension ProvideNull<L, R> on Effect<Null, L, R> {
Expand Down

0 comments on commit ac7f9e1

Please sign in to comment.