Skip to content

Commit

Permalink
run only when env is Null
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 25, 2024
1 parent d8144dd commit dcb9861
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 116 deletions.
146 changes: 67 additions & 79 deletions packages/fpdart/lib/src/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,81 +71,6 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
return "Effect(${_unsafeRun.runtimeType})";
}

/// {@category execution}
R runSync(E env) {
try {
final result = _unsafeRun(env);
if (result is Future) {
throw Die.current(
Exception("runSync cannot execute async Effect"),
);
}

return switch (result) {
Left(value: final cause) => throw cause,
Right(value: final value) => value,
};
} on Cause<L> {
rethrow;
} catch (error, stackTrace) {
throw Die(error, stackTrace);
}
}

/// {@category execution}
Exit<L, R> runSyncExit(E env) {
try {
final result = _unsafeRun(env);
if (result is Future) {
return Left(Die.current(
Exception("runSyncExit cannot execute async Effect"),
));
}
return result;
} on Cause<L> catch (cause) {
return Left(cause);
} catch (error, stackTrace) {
return Left(Die(error, stackTrace));
}
}

/// {@category execution}
Future<R> runFuture(E env) async {
try {
final result = _unsafeRun(env);
if (result is! Future) {
return switch (result) {
Left(value: final cause) => throw cause,
Right(value: final value) => value,
};
}

return switch (await result) {
Left(value: final cause) => throw cause,
Right(value: final value) => value,
};
} on Cause<L> {
rethrow;
} catch (error, stackTrace) {
throw Die(error, stackTrace);
}
}

/// {@category execution}
Future<Exit<L, R>> runFutureExit(E env) async {
try {
final result = _unsafeRun(env);
if (result is! Future) {
return result;
}
return result;
} on Cause<L> catch (cause) {
return Left(cause);
} catch (error, stackTrace) {
return Left(Die(error, stackTrace));
}
}

/// {@category constructors}
factory Effect.gen(DoFunctionEffect<E, L, R> f) => Effect<E, L, R>._(
(env) {
Expand Down Expand Up @@ -610,14 +535,77 @@ extension ProvideNull<L, R> on Effect<Null, L, R> {
);

/// {@category execution}
R runSyncVoid() => runSync(null);
R runSync() {
try {
final result = _unsafeRun(null);
if (result is Future) {
throw Die.current(
Exception("runSync cannot execute async Effect"),
);
}

return switch (result) {
Left(value: final cause) => throw cause,
Right(value: final value) => value,
};
} on Cause<L> {
rethrow;
} catch (error, stackTrace) {
throw Die(error, stackTrace);
}
}

/// {@category execution}
Future<R> runFutureVoid() => runFuture(null);
Exit<L, R> runSyncExit() {
try {
final result = _unsafeRun(null);
if (result is Future) {
return Left(Die.current(
Exception("runSyncExit cannot execute async Effect"),
));
}
return result;
} on Cause<L> catch (cause) {
return Left(cause);
} catch (error, stackTrace) {
return Left(Die(error, stackTrace));
}
}

/// {@category execution}
Either<Cause<L>, R> runSyncExitVoid() => runSyncExit(null);
Future<R> runFuture() async {
try {
final result = _unsafeRun(null);
if (result is! Future) {
return switch (result) {
Left(value: final cause) => throw cause,
Right(value: final value) => value,
};
}

return switch (await result) {
Left(value: final cause) => throw cause,
Right(value: final value) => value,
};
} on Cause<L> {
rethrow;
} catch (error, stackTrace) {
throw Die(error, stackTrace);
}
}

/// {@category execution}
Future<Either<Cause<L>, R>> runFutureExitVoid() => runFutureExit(null);
Future<Exit<L, R>> runFutureExit() async {
try {
final result = _unsafeRun(null);
if (result is! Future) {
return result;
}
return result;
} on Cause<L> catch (cause) {
return Left(cause);
} catch (error, stackTrace) {
return Left(Die(error, stackTrace));
}
}
}
18 changes: 10 additions & 8 deletions packages/fpdart/test/src/effect/effect_alternatives_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@ void main() {
() {
group('orDie', () {
test('succeed', () {
final main = Effect.succeed(10).orDie;
final result = main.runSync(null);
final main = Effect<Null, String, int>.succeed(10).orDie;
final result = main.runSync();
expect(result, 10);
});

test('fail', () {
final main = Effect.fail(10).orDie;
expect(() => main.runSync(null), throwsA(isA<Die>()));
final main = Effect<Null, String, int>.fail("error").orDie;
expect(() => main.runSync(), throwsA(isA<Die>()));
});
});

group('orDieWith', () {
test('succeed', () {
final main = Effect.succeed(10).orDieWith((_) => CustomError());
final result = main.runSync(null);
final main = Effect<Null, String, int>.succeed(10)
.orDieWith((_) => CustomError());
final result = main.runSync();
expect(result, 10);
});

test('fail', () {
final main = Effect.fail(10).orDieWith((_) => CustomError());
expect(() => main.runSync(null), throwsA(isA<Die>()));
final main = Effect<Null, String, int>.fail("error")
.orDieWith((_) => CustomError());
expect(() => main.runSync(), throwsA(isA<Die>()));
});
});
},
Expand Down
8 changes: 4 additions & 4 deletions packages/fpdart/test/src/effect/effect_collecting_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ void main() {
() {
group('all', () {
test('succeeded all', () {
final main = Effect.all([
final main = Effect.all<Null, String, int>([
Effect.succeed(10),
Effect.succeed(20),
]);
final result = main.runSync(null);
final result = main.runSync();
expect(result, [10, 20]);
});

test('fail and stop execution', () {
var mutable = 0;
final main = Effect.all<dynamic, String, int>([
final main = Effect.all<Null, String, int>([
Effect.succeed(10),
Effect.fail("10"),
Effect.functionSucceed(() => mutable += 1),
Effect.fail("0"),
]);
final result = main.flip().runSync(null);
final result = main.flip().runSync();
expect(mutable, 0);
expect(result, "10");
});
Expand Down
30 changes: 15 additions & 15 deletions packages/fpdart/test/src/effect/effect_constructors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ void main() {
"Effect constructors",
() {
test('succeed', () {
final main = Effect.succeed(10);
final result = main.runSync(null);
final main = Effect<Null, String, int>.succeed(10);
final result = main.runSync();
expect(result, 10);
});

test('fail', () {
final main = Effect.fail(10);
final result = main.flip().runSync(null);
expect(result, 10);
final main = Effect<Null, String, int>.fail("error");
final result = main.flip().runSync();
expect(result, "error");
});

group('tryCatch', () {
test('executes once', () {
var mutable = 0;
final main = Effect.tryCatch(
final main = Effect<Null, void, int>.tryCatch(
execute: () {
mutable += 1;
return 10;
},
onError: (error, stackTrace) {},
);

main.runSync(null);
main.runSync();
expect(mutable, 1);
});

test('async', () async {
final main = Effect.tryCatch(
final main = Effect<Null, void, int>.tryCatch(
execute: () async => 10,
onError: (error, stackTrace) {},
);
final result = await main.runFuture(null);
final result = await main.runFuture();
expect(result, 10);
});

test('sync', () {
final main = Effect.tryCatch(
final main = Effect<Null, void, int>.tryCatch(
execute: () => 10,
onError: (error, stackTrace) {},
);
final result = main.runSync(null);
final result = main.runSync();
expect(result, 10);
});
});
Expand All @@ -57,7 +57,7 @@ void main() {
final value = $.sync(Effect.succeed(10));
return value;
});
final result = main.runSync(null);
final result = main.runSync();
expect(result, 10);
});

Expand All @@ -66,7 +66,7 @@ void main() {
final value = $.sync(Effect.fail("abc"));
return value;
});
final result = main.flip().runSync(null);
final result = main.flip().runSync();
expect(result, "abc");
});

Expand All @@ -76,7 +76,7 @@ void main() {
await $.async(Effect.functionSucceed(() => Future.value(10)));
return value;
});
final result = await main.runFuture(null);
final result = await main.runFuture();
expect(result, 10);
});

Expand All @@ -88,7 +88,7 @@ void main() {
return value;
});

expect(() => main.runSync(null), throwsA(isA<Die>()));
expect(() => main.runSync(), throwsA(isA<Die>()));
});
});
},
Expand Down
8 changes: 4 additions & 4 deletions packages/fpdart/test/src/effect/effect_do_notation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
});

final program = main.provide("abc");
final result = program.runSyncVoid();
final result = program.runSync();
expect(result, 3);
});
});
Expand All @@ -27,7 +27,7 @@ void main() {

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

Expand All @@ -39,7 +39,7 @@ void main() {

final program =
main.provideEffect(Effect<Null, String, int>.fail("error"));
final result = program.flip().runSyncVoid();
final result = program.flip().runSync();
expect(result, "error");
});
});
Expand All @@ -53,7 +53,7 @@ void main() {
return value;
});

final result = main.runSync("abc");
final result = main.provide("abc").runSync();
expect(result, 4);
});
});
Expand Down
14 changes: 8 additions & 6 deletions packages/fpdart/test/src/effect/effect_sequencing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ void main() {
"Effect constructors",
() {
test('zipLeft', () {
final main = Effect.succeed(10).zipLeft(Effect.succeed("10"));
final result = main.runSync(null);
final main =
Effect<Null, String, int>.succeed(10).zipLeft(Effect.succeed("10"));
final result = main.runSync();
expect(result, 10);
});

test('zipRight', () {
final main = Effect.succeed(10).zipRight(Effect.succeed("10"));
final result = main.runSync(null);
final main = Effect<Null, String, int>.succeed(10)
.zipRight(Effect.succeed("10"));
final result = main.runSync();
expect(result, "10");
});

test('tap', () {
var mutable = 0;
final main = Effect.succeed(10).tap(
final main = Effect<Null, String, int>.succeed(10).tap(
(_) => Effect.functionSucceed(() {
mutable += 1;
}),
);

expect(mutable, 0);
final result = main.runSync(null);
final result = main.runSync();
expect(result, 10);
expect(mutable, 1);
});
Expand Down

0 comments on commit dcb9861

Please sign in to comment.