Skip to content

Commit

Permalink
factory constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroMaglione committed Mar 22, 2024
1 parent e82dba6 commit 05036f1
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion examples/fpdart_http/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void main() async {
Uri.https("pokeapi.co", "/api/v2/pokemon/10"),
)
.tap(
(response) => Effect.function(
(response) => Effect.functionSucceed(
() => print(response.body),
),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/poke_api/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void main() async {
final exit = await program("9722")
.map((pokemon) => print(pokemon))
.catchError<void>(
(error) => Effect.function(
(error) => Effect.functionSucceed(
() => print("No pokemon: $error"),
),
)
Expand Down
1 change: 1 addition & 0 deletions packages/fpdart/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include: package:lints/recommended.yaml
linter:
rules:
annotate_overrides: true
prefer_const_constructors: true

analyzer:
language:
Expand Down
46 changes: 29 additions & 17 deletions packages/fpdart/lib/src/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,8 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
///
/// 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;
final StackTrace? stackTrace;

static bool debugTracing = false;

const Effect._(
this._unsafeRun, {
this.stackTrace,
});
const Effect._(this._unsafeRun);

@override
Effect<E, L, R> get asEffect => this;
Expand All @@ -83,7 +77,7 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
R runSync(E env) {
final result = _unsafeRun(env);
if (result is Future) {
throw Die.current(result, stackTrace);
throw Die.current(result);
}

return switch (result) {
Expand Down Expand Up @@ -163,7 +157,12 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
);

/// {@category constructors}
factory Effect.function(FutureOr<R> Function() f) => Effect._(
factory Effect.functionFail(FutureOr<Cause<L>> Function() f) => Effect._(
(_) => f().then(Left.new),
);

/// {@category constructors}
factory Effect.functionSucceed(FutureOr<R> Function() f) => Effect._(
(_) => f().then(Right.new),
);

Expand All @@ -174,9 +173,9 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
factory Effect.succeed(R value) => Effect._((_) => Right(value));

/// {@category constructors}
static Effect<Never, Never, fpdart_unit.Unit> unit() => Effect._(
(_) => Right(fpdart_unit.unit),
);
static Effect<Never, Never, fpdart_unit.Unit> unit = Effect._(
(_) => const Right(fpdart_unit.unit),
);

/// {@category collecting}
static Effect<E, L, Iterable<R>> forEach<E, L, R, A>(
Expand All @@ -186,13 +185,13 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
Effect._(
(env) {
if (iterable.isEmpty) {
return Right([]);
return const Right([]);
}

return iterable
.mapWithIndex(f)
.fold<Effect<E, L, Iterable<R>>>(
Effect.succeed(Iterable.empty()),
Effect.succeed(const Iterable.empty()),
(acc, effect) => acc.zipWith(
effect,
(list, r) => list.append(r),
Expand Down Expand Up @@ -355,7 +354,7 @@ final class Effect<E, L, R> extends IEffect<E, L, R> {
Effect<E, Never, R> get orDie => Effect._(
(env) => _unsafeRun(env).then(
(exit) => switch (exit) {
Left(value: final cause) => Left(Die.current(cause, stackTrace)),
Left(value: final cause) => Left(Die.current(cause)),
Right(value: final value) =>
Effect<E, Never, R>.succeed(value)._unsafeRun(env),
},
Expand Down Expand Up @@ -424,7 +423,7 @@ extension ProvideNever<L, R> on Effect<Never, L, R> {
R runSyncNoEnv() {
final result = _unsafeRun(null);
if (result is Future) {
throw Die.current(result, stackTrace);
throw Die.current(result);
}

return switch (result) {
Expand All @@ -445,12 +444,25 @@ extension ProvideNever<L, R> on Effect<Never, L, R> {
/// {@category execution}
Future<R> runFutureNoEnv() async {
final result = await _unsafeRun(null);
if (result is! Future) {
print(
"You can use runSync instead of runFuture since the Effect is synchronous",
);
}
return switch (result) {
Left(value: final cause) => throw cause,
Right(value: final value) => value,
};
}

/// {@category execution}
Future<Exit<L, R>> runFutureExitNoEnv() async => _unsafeRun(null);
Future<Exit<L, R>> runFutureExitNoEnv() async {
final result = _unsafeRun(null);
if (result is! Future) {
print(
"You can use runSync instead of runFuture since the Effect is synchronous",
);
}
return result;
}
}
1 change: 0 additions & 1 deletion packages/fpdart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies:
dev_dependencies:
lints: ^2.0.1
test: ^1.23.1
glados: ^1.1.6
collection: ^1.17.2

screenshots:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
final main = Effect.all<dynamic, String, int>([
Effect.succeed(10),
Effect.fail("10"),
Effect.function(() => mutable += 1),
Effect.functionSucceed(() => mutable += 1),
Effect.fail("0"),
]);
final result = main.flip.runSync(null);
Expand Down
4 changes: 2 additions & 2 deletions packages/fpdart/test/src/effect/effect_constructors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void main() {
test('async succeed', () async {
final main = Effect.gen(($) async {
final value =
await $.async(Effect.function(() => Future.value(10)));
await $.async(Effect.functionSucceed(() => Future.value(10)));
return value;
});
final result = await main.runFutureNoEnv();
Expand All @@ -81,7 +81,7 @@ void main() {

test('fail when running async as sync', () async {
final main = Effect.gen(($) {
final value = $.sync(Effect.function(
final value = $.sync(Effect.functionSucceed(
() async => Future.value(10),
));
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
test('tap', () {
var mutable = 0;
final main = Effect.succeed(10).tap(
(_) => Effect.function(() {
(_) => Effect.functionSucceed(() {
mutable += 1;
}),
);
Expand Down

0 comments on commit 05036f1

Please sign in to comment.