lemmur-app-android/lib/util/mobx_provider.dart

44 lines
1.0 KiB
Dart
Raw Normal View History

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,
}) : 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();
}
/// 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();
}
}
}