2021-12-04 18:03:54 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
|
|
|
|
import 'observer_consumers.dart';
|
|
|
|
|
|
|
|
/// Provides a mobx store and disposes it if it implements [DisposableStore]
|
|
|
|
///
|
|
|
|
/// Important: this will not make [context.watch] react to changes
|
|
|
|
class MobxProvider<T extends Store> extends Provider<T> {
|
|
|
|
MobxProvider({
|
2022-05-11 22:23:18 +02:00
|
|
|
super.key,
|
|
|
|
required super.create,
|
|
|
|
super.lazy,
|
|
|
|
super.builder,
|
|
|
|
super.child,
|
2021-12-04 18:03:54 +01:00
|
|
|
}) : super(
|
|
|
|
dispose: (context, store) {
|
|
|
|
if (store is DisposableStore) store.dispose();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
/// will not dispose the store
|
|
|
|
MobxProvider.value({
|
2022-05-11 22:23:18 +02:00
|
|
|
super.key,
|
|
|
|
required super.value,
|
|
|
|
super.builder,
|
|
|
|
super.child,
|
|
|
|
}) : super.value();
|
2021-12-04 18:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// tracks reactions and disposes them in [DisposableStore.dispose]
|
|
|
|
mixin DisposableStore on Store {
|
|
|
|
final List<ReactionDisposer> _disposers = [];
|
|
|
|
|
|
|
|
@protected
|
|
|
|
void addReaction(ReactionDisposer reaction) => _disposers.add(reaction);
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
for (final disposer in _disposers) {
|
|
|
|
disposer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|