diff --git a/packages/fpdart/lib/src/effect.dart b/packages/fpdart/lib/src/effect.dart index eae791d..8d968ca 100644 --- a/packages/fpdart/lib/src/effect.dart +++ b/packages/fpdart/lib/src/effect.dart @@ -71,81 +71,6 @@ final class Effect extends IEffect { 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 { - rethrow; - } catch (error, stackTrace) { - throw Die(error, stackTrace); - } - } - - /// {@category execution} - Exit 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 catch (cause) { - return Left(cause); - } catch (error, stackTrace) { - return Left(Die(error, stackTrace)); - } - } - - /// {@category execution} - Future 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 { - rethrow; - } catch (error, stackTrace) { - throw Die(error, stackTrace); - } - } - - /// {@category execution} - Future> runFutureExit(E env) async { - try { - final result = _unsafeRun(env); - if (result is! Future) { - return result; - } - return result; - } on Cause catch (cause) { - return Left(cause); - } catch (error, stackTrace) { - return Left(Die(error, stackTrace)); - } - } - /// {@category constructors} factory Effect.gen(DoFunctionEffect f) => Effect._( (env) { @@ -610,14 +535,77 @@ extension ProvideNull on Effect { ); /// {@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 { + rethrow; + } catch (error, stackTrace) { + throw Die(error, stackTrace); + } + } /// {@category execution} - Future runFutureVoid() => runFuture(null); + Exit runSyncExit() { + try { + final result = _unsafeRun(null); + if (result is Future) { + return Left(Die.current( + Exception("runSyncExit cannot execute async Effect"), + )); + } + return result; + } on Cause catch (cause) { + return Left(cause); + } catch (error, stackTrace) { + return Left(Die(error, stackTrace)); + } + } /// {@category execution} - Either, R> runSyncExitVoid() => runSyncExit(null); + Future 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 { + rethrow; + } catch (error, stackTrace) { + throw Die(error, stackTrace); + } + } /// {@category execution} - Future, R>> runFutureExitVoid() => runFutureExit(null); + Future> runFutureExit() async { + try { + final result = _unsafeRun(null); + if (result is! Future) { + return result; + } + return result; + } on Cause catch (cause) { + return Left(cause); + } catch (error, stackTrace) { + return Left(Die(error, stackTrace)); + } + } } diff --git a/packages/fpdart/test/src/effect/effect_alternatives_test.dart b/packages/fpdart/test/src/effect/effect_alternatives_test.dart index c4884a0..73dde6f 100644 --- a/packages/fpdart/test/src/effect/effect_alternatives_test.dart +++ b/packages/fpdart/test/src/effect/effect_alternatives_test.dart @@ -9,27 +9,29 @@ void main() { () { group('orDie', () { test('succeed', () { - final main = Effect.succeed(10).orDie; - final result = main.runSync(null); + final main = Effect.succeed(10).orDie; + final result = main.runSync(); expect(result, 10); }); test('fail', () { - final main = Effect.fail(10).orDie; - expect(() => main.runSync(null), throwsA(isA())); + final main = Effect.fail("error").orDie; + expect(() => main.runSync(), throwsA(isA())); }); }); group('orDieWith', () { test('succeed', () { - final main = Effect.succeed(10).orDieWith((_) => CustomError()); - final result = main.runSync(null); + final main = Effect.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())); + final main = Effect.fail("error") + .orDieWith((_) => CustomError()); + expect(() => main.runSync(), throwsA(isA())); }); }); }, diff --git a/packages/fpdart/test/src/effect/effect_collecting_test.dart b/packages/fpdart/test/src/effect/effect_collecting_test.dart index a15c0f4..84db780 100644 --- a/packages/fpdart/test/src/effect/effect_collecting_test.dart +++ b/packages/fpdart/test/src/effect/effect_collecting_test.dart @@ -7,23 +7,23 @@ void main() { () { group('all', () { test('succeeded all', () { - final main = Effect.all([ + final main = Effect.all([ 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([ + final main = Effect.all([ 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"); }); diff --git a/packages/fpdart/test/src/effect/effect_constructors_test.dart b/packages/fpdart/test/src/effect/effect_constructors_test.dart index e61d6ba..0df5ab4 100644 --- a/packages/fpdart/test/src/effect/effect_constructors_test.dart +++ b/packages/fpdart/test/src/effect/effect_constructors_test.dart @@ -6,21 +6,21 @@ void main() { "Effect constructors", () { test('succeed', () { - final main = Effect.succeed(10); - final result = main.runSync(null); + final main = Effect.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.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.tryCatch( execute: () { mutable += 1; return 10; @@ -28,25 +28,25 @@ void main() { onError: (error, stackTrace) {}, ); - main.runSync(null); + main.runSync(); expect(mutable, 1); }); test('async', () async { - final main = Effect.tryCatch( + final main = Effect.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.tryCatch( execute: () => 10, onError: (error, stackTrace) {}, ); - final result = main.runSync(null); + final result = main.runSync(); expect(result, 10); }); }); @@ -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); }); @@ -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"); }); @@ -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); }); @@ -88,7 +88,7 @@ void main() { return value; }); - expect(() => main.runSync(null), throwsA(isA())); + expect(() => main.runSync(), throwsA(isA())); }); }); }, diff --git a/packages/fpdart/test/src/effect/effect_do_notation_test.dart b/packages/fpdart/test/src/effect/effect_do_notation_test.dart index bbb4daf..d3ff7d1 100644 --- a/packages/fpdart/test/src/effect/effect_do_notation_test.dart +++ b/packages/fpdart/test/src/effect/effect_do_notation_test.dart @@ -13,7 +13,7 @@ void main() { }); final program = main.provide("abc"); - final result = program.runSyncVoid(); + final result = program.runSync(); expect(result, 3); }); }); @@ -27,7 +27,7 @@ void main() { final program = main.provideEffect(Effect.succeed(10)); - final result = program.runSyncVoid(); + final result = program.runSync(); expect(result, 11); }); @@ -39,7 +39,7 @@ void main() { final program = main.provideEffect(Effect.fail("error")); - final result = program.flip().runSyncVoid(); + final result = program.flip().runSync(); expect(result, "error"); }); }); @@ -53,7 +53,7 @@ void main() { return value; }); - final result = main.runSync("abc"); + final result = main.provide("abc").runSync(); expect(result, 4); }); }); diff --git a/packages/fpdart/test/src/effect/effect_sequencing_test.dart b/packages/fpdart/test/src/effect/effect_sequencing_test.dart index 0480bd1..ee6c273 100644 --- a/packages/fpdart/test/src/effect/effect_sequencing_test.dart +++ b/packages/fpdart/test/src/effect/effect_sequencing_test.dart @@ -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.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.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.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); });