Skip to content

Commit

Permalink
2.0.1 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobaraujo7 committed Jul 1, 2024
1 parent 4796058 commit b1f5de0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [2.0.1] - 2024-06-26

- Fix: **atomAction** now returning **FutureOr**.
- Fix: propertie **when** in **useAtomState** hook;

## [2.0.0] - 2024-06-24

After extensive feedback, we are changing the API to make the standard more intuitive and user-friendly. We also added stricter limits for more complex projects and introduced a “predictability” system in state changes to improve tracking and debugging while reducing unwanted side effects. Some changes may seem drastic, but we believe everything will make more sense once you give this new API a chance.
Expand Down
33 changes: 21 additions & 12 deletions example/trivial_counter/lib/atoms/counter.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import 'package:asp/asp.dart';

// atom
final counterState = atom<int>(0);
final counterState = atom<int>(
key: 'counterState',
0,
);

// selectors
final parityState = selector((get) {
return get(counterState).isEven ? 'Even' : 'Odd';
});
final parityState = selector(
key: 'parityState',
(get) {
return get(counterState).isEven ? 'Even' : 'Odd';
},
);

// actions
final counterAction = atomAction1<String>((set, action) {
final state = counterState.state;
if (action == 'increment') {
set(counterState, state + 1);
} else if (action == 'decrement') {
set(counterState, state - 1);
}
});
final counterAction = atomAction1<String>(
key: 'counterAction',
(set, action) {
final state = counterState.state;
if (action == 'increment') {
set(counterState, state + 1);
} else if (action == 'decrement') {
set(counterState, state - 1);
}
},
);
4 changes: 4 additions & 0 deletions example/trivial_counter/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'package:asp/asp.dart';
import 'package:flutter/material.dart';

import 'pages/counter_page.dart';

void main() {
AtomObserver.changes((status) {
print(status);
});
runApp(MyApp());
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sealed class AtomAction {
AtomAction(this._scope, this._key);

/// Executes the action
void call() => _scope(SetState(_key));
FutureOr<void> call() => _scope(SetState(_key));
}

class _AtomAction extends AtomAction {
Expand Down
35 changes: 33 additions & 2 deletions lib/src/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ part of '../asp.dart';
extension AtomHookExtension on HookState {
/// Gets the state of the [Atom] and registers the Widget to be
/// rebuilt when the [Atom] is modified.
T useAtomState<T>(Atom<T> atom) {
return useListenable(atom).state;
/// [atom]: The [Atom] to be observed.
/// [when]: An optional function that determines whether the Widget should
T useAtomState<T>(Atom<T> atom, {bool Function(T oldState, T state)? when}) {
final hook = _AtomStateHook(atom, when);
return use(hook).atom.state;
}

/// Listens to the [Atom] and executes the [effect] whenever the [Atom]
Expand All @@ -27,6 +30,34 @@ extension AtomHookExtension on HookState {
}
}

class _AtomStateHook<T> extends Hook<T> {
final Atom<T> atom;
final Function(T _oldState, T state)? when;
late T _oldState;

_AtomStateHook(this.atom, [this.when]);

@override
void init() {
_oldState = atom.state;
atom.addListener(_listener);
}

void _listener() {
final state = atom.state;

if (when?.call(_oldState, state) ?? true) {
setState();
}
_oldState = state;
}

@override
void dispose() {
atom.removeListener(_listener);
}
}

class _AtomEffectHook<T> extends Hook<T> {
late RxDisposer disposer;
final T Function(GetState) body;
Expand Down

0 comments on commit b1f5de0

Please sign in to comment.