diff --git a/packages/rearch/lib/experimental.dart b/packages/rearch/lib/experimental.dart index 531244e..017f552 100644 --- a/packages/rearch/lib/experimental.dart +++ b/packages/rearch/lib/experimental.dart @@ -30,47 +30,4 @@ extension ExperimentalSideEffects on SideEffectRegistrar { return (() => state, (T newState) => state = newState); }); } - - /// A mechanism to persist changes made to some provided state. - /// Unlike [persist], [hydrate] allows you to pass in the state to persist, - /// if there is one, to enable easier composition with other side effects. - /// - /// Defines a way to interact with a storage provider of your choice - /// through the [read] and [write] parameters. - /// - If [newData] is [Some], then [newData] will be persisted and - /// overwrite any existing persisted data. - /// - If [newData] is [None], then no changes will be made to the currently - /// persisted value (for when you don't have state to persist yet). - /// - /// [read] is only called once; it is assumed that if [write] is successful, - /// then calling [read] again would reflect the new state that we already - /// have access to. Thus, calling [read] again is skipped as an optimization. - // NOTE: experimental because I am not sold on the Option parameter. - AsyncValue hydrate( - Option newData, { - required Future Function() read, - required Future Function(T) write, - }) { - final readFuture = use.callonce(read); - final readState = use.future(readFuture); - final (getPrevData, setPrevData) = - use.rawValueWrapper>(None.new); - final (getWriteFuture, setWriteFuture) = - use.rawValueWrapper?>(() => null); - - if (newData case Some(value: final data)) { - final dataChanged = - getPrevData().map((prevData) => data != prevData).unwrapOr(true); - if (dataChanged) { - setPrevData(Some(data)); - setWriteFuture(() async { - await write(data); - return data; - }()); - } - } - - final writeState = use.nullableFuture(getWriteFuture()); - return (writeState ?? readState).fillInPreviousData(readState.data); - } } diff --git a/packages/rearch/lib/src/side_effects.dart b/packages/rearch/lib/src/side_effects.dart index 8e2f969..7b2bc63 100644 --- a/packages/rearch/lib/src/side_effects.dart +++ b/packages/rearch/lib/src/side_effects.dart @@ -323,6 +323,43 @@ extension BuiltinSideEffects on SideEffectRegistrar { return (state, persist); } + /// A mechanism to persist changes made to some provided state. + /// Unlike [persist], [hydrate] allows you to pass in the state to persist, + /// if there is one, to enable easier composition with other side effects. + /// + /// Defines a way to interact with a storage provider of your choice + /// through the [read] and [write] parameters. + /// - If [newData] is [Some], then [newData] will be persisted and + /// overwrite any existing persisted data. + /// - If [newData] is [None], then no changes will be made to the currently + /// persisted value (for when you don't have state to persist yet). + /// + /// [read] is only called once; it is assumed that if [write] is successful, + /// then calling [read] again would reflect the new state that we already + /// have access to. Thus, calling [read] again is skipped as an optimization. + AsyncValue hydrate( + T? newData, { + required Future Function() read, + required Future Function(T) write, + }) { + final readFuture = use.callonce(read); + final readState = use.future(readFuture); + final (getPrevData, setPrevData) = use.rawValueWrapper(() => null); + final (getWriteFuture, setWriteFuture) = + use.rawValueWrapper?>(() => null); + + if (newData != null && newData != getPrevData()) { + setPrevData(newData); + setWriteFuture(() async { + await write(newData); + return newData; + }()); + } + + final writeState = use.nullableFuture(getWriteFuture()); + return (writeState ?? readState).fillInPreviousData(readState.data); + } + /// Allows you to trigger and watch [Future]s /// (called mutations, since they often mutate some state) /// from within the build function. diff --git a/packages/rearch/test/side_effects_test.dart b/packages/rearch/test/side_effects_test.dart index fabbcd4..b1e87c3 100644 --- a/packages/rearch/test/side_effects_test.dart +++ b/packages/rearch/test/side_effects_test.dart @@ -1,4 +1,3 @@ -import 'package:rearch/experimental.dart'; import 'package:rearch/rearch.dart'; import 'package:test/test.dart'; @@ -61,11 +60,7 @@ void main() { CapsuleHandle use, ) { final (state, setState) = use.state(null); - final hydrateState = use.hydrate( - state != null ? Some(state) : const None(), - read: read, - write: write, - ); + final hydrateState = use.hydrate(state, read: read, write: write); return (hydrateState, setState); }