11part of '../base_scope_container.dart' ;
22
3- typedef StateListener <S > = void Function (S scope);
3+ typedef StateListener <S > = void Function (S ? scope);
4+
5+ typedef ScopeStateListener <S > = void Function (ScopeState <S > state);
46
57typedef RemoveStateListener = void Function ();
68
79class ScopeStateHolder <Scope > {
810 final _listeners = LinkedList <Entry <Scope >>();
9- Scope _scope ;
11+ ScopeState < Scope > _state ;
1012
1113 bool _debugCanAddListeners = true ;
1214
13- ScopeStateHolder (Scope state) : _scope = state ;
15+ ScopeStateHolder (this ._state) ;
1416
15- Scope get scope => _scope;
17+ Scope ? get scope {
18+ final state = this .state;
19+ if (state is ScopeStateAvailable <Scope >) {
20+ return state.scope;
21+ }
22+ return null ;
23+ }
1624
17- void _setScope (Scope state) {
18- _scope = state;
25+ ScopeState <Scope > get state => _state;
26+
27+ void _updateState (ScopeState <Scope > state) {
28+ _state = state;
1929
2030 final errors = < Object > [];
2131 final stackTraces = < StackTrace > [];
@@ -50,6 +60,42 @@ class ScopeStateHolder<Scope> {
5060 RemoveStateListener listen (
5161 StateListener <Scope > listener, {
5262 bool emitImmediately = false ,
63+ }) =>
64+ _listen (
65+ (state) {
66+ if (state is ScopeStateAvailable <Scope >) {
67+ listener (state.scope);
68+ } else if (state is ScopeStateNone ) {
69+ listener (null );
70+ }
71+ },
72+ emitImmediately: emitImmediately,
73+ );
74+
75+ /// Subscribes to the state.
76+ ///
77+ /// The [listener] callback will be called immediately on addition and
78+ /// synchronously whenever [state] changes.
79+ ///
80+ /// Set [emitImmediately] to true if you want to an immediate execution
81+ /// of the [listener] with the current state.
82+ ///
83+ /// To remove this [listener] , call the function returned by [listen] .
84+ ///
85+ /// Listeners cannot add other listeners.
86+ /// Adding and removing listeners has a constant time-complexity.
87+ RemoveStateListener listenState (
88+ ScopeStateListener <Scope > listener, {
89+ bool emitImmediately = false ,
90+ }) =>
91+ _listen (
92+ (state) => listener (state),
93+ emitImmediately: emitImmediately,
94+ );
95+
96+ RemoveStateListener _listen (
97+ void Function (ScopeState <Scope > state) listener, {
98+ bool emitImmediately = false ,
5399 }) {
54100 assert (() {
55101 if (! _debugCanAddListeners) {
@@ -64,7 +110,7 @@ class ScopeStateHolder<Scope> {
64110 // Intentionally unsafe call of the listener before adding to the [_listeners]
65111 // so that if there is an exception — we throw it back to consumer
66112 // with an original stacktrace without adding to the [_listeners].
67- listener (scope );
113+ listener (state );
68114 } on Object catch (_) {
69115 rethrow ;
70116 } finally {
@@ -93,7 +139,7 @@ class ScopeStateHolder<Scope> {
93139class Entry <T > extends LinkedListEntry <Entry <T >> {
94140 Entry (this .listener);
95141
96- final StateListener <T > listener;
142+ final ScopeStateListener <T > listener;
97143}
98144
99145/// An error thrown when tried to update the state of a [ScopeStateHolder] ,
0 commit comments