@@ -110,33 +110,24 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
110110 String ? finder,
111111 DataRequestLabel ? label,
112112 }) {
113- final maybeFinder = _internalHolder? .finders[finder]? .call (this );
114- final finderFn = maybeFinder is DataFinderAll <T > ? maybeFinder : findAll;
115-
116113 // we can't use `findAll`'s default internal label
117114 // because we need a label to handle events
118115 label ?? = DataRequestLabel ('watchAll' , type: internalType);
119-
120116 log (label, 'initializing' );
121117
122- // closure to get latest models
123- List <T > _getUpdatedModels () {
124- return findAllLocal ();
125- }
126-
127118 final notifier = DataStateNotifier <List <T >>(
128- data: DataState (_getUpdatedModels (), isLoading: remote),
119+ data: DataState (findAllLocal (), isLoading: remote),
129120 );
130121
131122 notifier._reloadFn = () async {
132- if (! notifier.mounted) {
133- // TODO || remote = false
123+ if (! notifier.mounted || remote == false ) {
134124 return ;
135125 }
136126
137- if (remote) {
138- notifier.updateWith (isLoading: true );
139- }
127+ notifier.updateWith (isLoading: true );
128+
129+ final _finderFn = _internalHolder? .finders[finder]? .call (this );
130+ final finderFn = _finderFn is DataFinderAll <T > ? _finderFn : findAll;
140131
141132 await finderFn (
142133 remote: remote,
@@ -158,10 +149,9 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
158149 return [];
159150 },
160151 );
161- if (remote) {
162- // trigger doneLoading to ensure state is updated with isLoading=false
163- core._notify ([label.toString ()], type: DataGraphEventType .doneLoading);
164- }
152+
153+ // trigger doneLoading to ensure state is updated with isLoading=false
154+ core._notify ([label.toString ()], type: DataGraphEventType .doneLoading);
165155 };
166156
167157 // kick off
@@ -178,30 +168,30 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
178168 }
179169
180170 for (final event in events) {
181- final keysRecord = (event.keys.first, event.keys.last);
171+ final keypair = (event.keys.first, event.keys.last);
172+
182173 // handle done loading
183174 if (notifier.data.isLoading &&
184- keysRecord .$2 == label.toString () &&
175+ keypair .$2 == label.toString () &&
185176 event.type == DataGraphEventType .doneLoading) {
186- final models = _getUpdatedModels ();
187- states. add ( DataState (models , isLoading: false , exception: null ));
177+ states. add (
178+ DataState (findAllLocal () , isLoading: false , exception: null ));
188179 }
189180
190181 if (notifier.data.isLoading == false &&
191182 event.type.isNode &&
192- keysRecord.$1.startsWith (internalType)) {
193- final models = _getUpdatedModels ();
183+ keypair.$1.startsWith (internalType)) {
194184 log (label! , 'updated models' , logLevel: 2 );
195185 states.add (DataState (
196- models ,
186+ findAllLocal () ,
197187 isLoading: notifier.data.isLoading,
198188 exception: notifier.data.exception,
199189 stackTrace: notifier.data.stackTrace,
200190 ));
201191 }
202192
203193 if (event.type == DataGraphEventType .clear &&
204- keysRecord .$1.startsWith (internalType)) {
194+ keypair .$1.startsWith (internalType)) {
205195 log (label! , 'clear local storage' , logLevel: 2 );
206196 states.add (DataState ([], isLoading: false , exception: null ));
207197 }
@@ -228,10 +218,6 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
228218 DataRequestLabel ? label,
229219 }) {
230220 final id = core.getIdForKey (key);
231- // print('_won got $key / id $id');
232-
233- final maybeFinder = _internalHolder? .finders[finder]? .call (this );
234- final finderFn = maybeFinder is DataFinderOne <T > ? maybeFinder : findOne;
235221
236222 // we can't use `findOne`'s default internal label
237223 // because we need a label to handle events
@@ -273,15 +259,17 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
273259 .whereType <RelationshipMeta >()
274260 .map ((m) => m.name) ??
275261 {};
262+
276263 log (label,
277264 'initializing${alsoWatchNames .isNotEmpty ? ' (and also watching: ${alsoWatchNames .join (', ' )})' : '' }' );
278265
279266 notifier._reloadFn = () async {
280- if (! notifier.mounted || id == null ) return ;
267+ if (! notifier.mounted || id == null || remote == false ) return ;
281268
282- if (remote) {
283- notifier.updateWith (isLoading: true );
284- }
269+ notifier.updateWith (isLoading: true );
270+
271+ final _finderFn = _internalHolder? .finders[finder]? .call (this );
272+ final finderFn = _finderFn is DataFinderOne <T > ? _finderFn : findOne;
285273
286274 final model = await finderFn (
287275 id,
@@ -303,10 +291,10 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
303291 return null ;
304292 },
305293 );
294+
306295 // trigger doneLoading to ensure state is updated with isLoading=false
307- final modelKey = model? ._key;
308- if (remote && modelKey != null ) {
309- core._notify ([modelKey, label.toString ()],
296+ if (model? ._key != null ) {
297+ core._notify ([model! ._key! , label.toString ()],
310298 type: DataGraphEventType .doneLoading);
311299 }
312300 };
@@ -335,11 +323,12 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
335323 key = bufferModel? ._key ?? key;
336324
337325 for (final event in events) {
338- final keysRecord = (event.keys.first, event.keys.last);
339- if (keysRecord.contains (key)) {
326+ final keypair = (event.keys.first, event.keys.last);
327+
328+ if (keypair.contains (key)) {
340329 // handle done loading
341330 if (notifier.data.isLoading &&
342- keysRecord .$2 == label.toString () &&
331+ keypair .$2 == label.toString () &&
343332 event.type == DataGraphEventType .doneLoading) {
344333 states
345334 .add (DataState (bufferModel, isLoading: false , exception: null ));
@@ -348,7 +337,7 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
348337 // add/update
349338 if (event.type == DataGraphEventType .updateNode) {
350339 if (notifier.data.isLoading == false ) {
351- log (label! , 'added/updated node $keysRecord ' , logLevel: 2 );
340+ log (label! , 'added/updated node $keypair ' , logLevel: 2 );
352341 states.add (DataState (
353342 bufferModel,
354343 isLoading: notifier.data.isLoading,
@@ -361,16 +350,16 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
361350 // temporarily restore removed pair so that watchedRelationshipUpdate
362351 // has a chance to apply the update
363352 if (event.type == DataGraphEventType .removeEdge &&
364- ! keysRecord .$1.startsWith ('id:' )) {
365- alsoWatchPairs.add (keysRecord );
353+ ! keypair .$1.startsWith ('id:' )) {
354+ alsoWatchPairs.add (keypair );
366355 }
367356 }
368357
369358 // handle deletion
370359 if ([DataGraphEventType .removeNode, DataGraphEventType .clear]
371360 .contains (event.type) &&
372361 bufferModel == null ) {
373- log (label! , 'removed node $keysRecord ' , logLevel: 2 );
362+ log (label! , 'removed node $keypair ' , logLevel: 2 );
374363 states.add (DataState (
375364 null ,
376365 isLoading: notifier.data.isLoading,
@@ -382,19 +371,19 @@ mixin _WatchAdapter<T extends DataModelMixin<T>> on _RemoteAdapter<T> {
382371 // updates on watched relationships condition
383372 final watchedRelationshipUpdate = event.type.isEdge &&
384373 alsoWatchPairs
385- .where ((pair) => pair.unorderedEquals (keysRecord ))
374+ .where ((pair) => pair.unorderedEquals (keypair ))
386375 .isNotEmpty;
387376
388377 // updates on watched models (of relationships) condition
389378 final watchedModelUpdate = event.type.isNode &&
390379 alsoWatchPairs
391- .where ((pair) => pair.contains (keysRecord .$1))
380+ .where ((pair) => pair.contains (keypair .$1))
392381 .isNotEmpty;
393382
394383 // if model is loaded and any condition passes, notify update
395384 if (notifier.data.isLoading == false &&
396385 (watchedRelationshipUpdate || watchedModelUpdate)) {
397- log (label! , 'relationship update $keysRecord ' , logLevel: 2 );
386+ log (label! , 'relationship update $keypair ' , logLevel: 2 );
398387 states.add (DataState (
399388 bufferModel,
400389 isLoading: notifier.data.isLoading,
0 commit comments