mobx library
MobX is a library for reactively managing the state of your applications.
Describe the state of your application as a graph of observable-values. Setup reactions that observe these observable values. When a dependent observable changes, the reactions are automatically triggered. The observables are treated as the implicit dependencies of the reaction. No explicit wiring is needed, besides just using it inside a reaction.
With these simple primitives, you can tackle a wide variety of state management use-cases in your Dart apps.
Consider the following example:
var x = Observable(10);
var y = Observable(20);
autorun((){
print(x.value + y.value); // just referencing the observable values is enough to start tracking
}); // prints 30
// When x or y changes
x.value = 20; // prints 40
y.value = 30; // prints 50
Notice there is no explicit subscription to any observable values. By simply referencing the observables, the reaction picks up the dependencies. When they change, the reaction is automatically triggered.
Classes
- Action
- ActionController
-
ActionController
is used to define the start/end boundaries of code which should be wrapped inside an action. This ensures all observable mutations are neatly encapsulated. - ActionRunInfo
- ActionSpyEvent
- AsyncAction
- AsyncAction uses a Zone to keep track of async operations like Future, timers and other kinds of micro-tasks.
- Atom
-
ChangeNotification<
T> -
Computed<
T> - ComputedMethod
-
Internal class only used for code-generation with
mobx_codegen
. - ComputedValueSpyEvent
- Derivation
- EndedSpyEvent
-
Interceptable<
T> -
Interceptors<
T> - Stores the intercept-handlers that have been attached to a specific Observable.
-
ListChange<
T> - Stores the change related information when items was modified, added or removed from list.
-
Listenable<
TNotification> -
Listeners<
TNotification> - Stores the handler functions that have been attached via Observable.observe method This is an internal class and should not be used directly.
- MakeObservable
-
Internal class only used for code-generation with
mobx_codegen
. -
MapChange<
K, V> - Stores the information related to changes happening in an ObservableMap. This is used when firing the change notifications to all the listeners
-
Observable<
T> -
ObservableFuture<
T> -
ObservableList<
T> - The ObservableList tracks the various read-methods (eg: List.first, List.last) and write-methods (eg: List.add, List.insert) making it easier to use it inside reactions.
-
ObservableMap<
K, V> - The ObservableMap tracks the various read-methods (eg: Map.length, Map.isEmpty) and write-methods (eg: Map.[]=, Map.clear) making it easier to use it inside reactions.
-
ObservableSet<
T> - ObservableSet provides a reactive set that notifies changes when a member is added or removed.
-
ObservableStream<
T> - Stream that tracks the emitted values of the provided stream and makes them available as a MobX observable value.
-
ObservableValue<
T> - ObservableValueSpyEvent
- Used for reporting value changes on an Observable
- Reaction
- ReactionDisposedSpyEvent
- ReactionDisposer
- A callable class that is used to dispose a reaction, autorun or when
- ReactionErrorSpyEvent
- ReactionSpyEvent
- ReactiveConfig
- Configuration used by ReactiveContext
- ReactiveContext
-
SetChange<
T> - Capture the change related information for an ObservableSet. This is used as the notification instance.
- SpyEvent
- StoreConfig
-
WillChangeNotification<
T>
Enums
- FutureStatus
- OperationType
- ReactiveReadPolicy
- Defines the behavior for observables read outside actions and reactions
- ReactiveWritePolicy
- Defines the behavior for observables mutated outside actions
- StreamStatus
Mixins
- Store
-
The
Store
mixin is primarily meant for code-generation and used as part of themobx_codegen
package.
Extensions
- AtomSpyReporter on Atom
- BoolExtension on bool
- ConditionalAction on ReactiveContext
- DoubleExtension on double
- IntExtension on int
-
ObservableBoolExtension
on Observable<
bool> -
ObservableFutureExtension
on Future<
T> - Turn the Future into an ObservableFuture.
-
ObservableListExtension
on List<
T> - Turn the List into an ObservableList.
-
ObservableMapExtension
on Map<
K, V> - Turn the Map into an ObservableMap.
-
ObservableSetExtension
on Set<
T> - Turn the Set into an ObservableSet.
-
ObservableStreamExtension
on Stream<
T> - Turn the Stream into an ObservableStream.
- StringExtension on String
Constants
- action → const MakeAction
-
Declares a method as an action. See the
Action
class for full documentation. - alwaysNotify → const MakeObservable
- Allows a reaction to be fired even if the value hasn't changed.
- computed → const ComputedMethod
-
Declares a method as a computed value. See the
Computed
class for full documentation. - observable → const MakeObservable
-
Declares a class field as an observable. See the
Observable
class for full documentation - readonly → const MakeObservable
-
Declares a class field as an observable. See the
Observable
class for full documentation. - version → const String
-
The current version as per
pubspec.yaml
.
Properties
- mainContext → ReactiveContext
-
The main context of MobX. All reactive operations and observations are happening
inside this context.
final
Functions
-
asyncWhen(
bool predicate(Reaction), {String? name, int? timeout, ReactiveContext? context}) → Future< void> -
A variant of when() which returns a Future. The Future completes when the
predicate()
turns true. Note that there is no effect function here. Typically you would await on the Future and execute the effect after that. -
autorun(
dynamic fn(Reaction), {String? name, int? delay, ReactiveContext? context, Timer scheduler(void ())?, void onError(Object, Reaction)?}) → ReactionDisposer -
Executes the specified
fn
, whenever the dependent observables change. It returns a disposer that can be used to dispose the autorun. -
createContext(
{ReactiveConfig? config}) → ReactiveContext - Create a new context for running actions and reactions.
-
observableAlwaysNotEqual(
dynamic _, dynamic __) → bool -
reaction<
T> (T fn(Reaction), void effect(T), {String? name, int? delay, bool? fireImmediately, EqualityComparer< T> ? equals, ReactiveContext? context, Timer scheduler(void ())?, void onError(Object, Reaction)?}) → ReactionDisposer -
Executes the
fn
function and tracks the observables used in it. Returns a function to dispose the reaction. -
runInAction<
T> (T fn(), {String? name, ReactiveContext? context}) → T -
Executes the mutation function
fn
within anAction
. This ensures that all change notifications are fired only at the end of theAction
block. Note that actions can be nested, in which case the notifications go out when the outermostAction
completes. -
transaction<
T> (T fn(), {ReactiveContext? context}) → T - During a transaction, no derivations (Reaction or Computed
-
untracked<
T> (T fn(), {ReactiveContext? context}) → T - Untracked ensures there is no tracking derivation while the given action runs. This is useful in cases where no observers should be linked to a running (tracking) derivation.
-
when(
bool predicate(Reaction), void effect(), {String? name, ReactiveContext? context, int? timeout, void onError(Object, Reaction)?}) → ReactionDisposer -
A one-time reaction that auto-disposes when the
predicate
becomes true. It also executes theeffect
when the predicate turns true.
Typedefs
- Dispose = void Function()
-
EqualityComparer<
T> = bool Function(T?, T?) -
Interceptor<
T> = WillChangeNotification< T> ? Function(WillChangeNotification<T> ) -
ListChangeListener<
TNotification> = void Function(ListChange< TNotification> ) -
Listener<
TNotification> = void Function(TNotification) -
MapChangeListener<
K, V> = void Function(MapChange< K, V> ) - ReactionErrorHandler = void Function(Object error, Reaction reaction)
-
SetChangeListener<
T> = void Function(SetChange< T> ) - SpyListener = void Function(SpyEvent event)
Exceptions / Errors
- MobXCaughtException
- This captures the stack trace when user-land code throws an exception
- MobXCyclicReactionException
- This exception would be fired when an reaction has a cycle and does not stabilize in ReactiveConfig.maxIterations iterations
- MobXException
- An Exception class to capture MobX specific exceptions