Closed PackRuble closed 7 months ago
I'll have to figure out how to pass getOrNull(card) in the attach(...) method, because it's not available now.
Suppose we convert `WatcherImpl' like this:
class WatcherImpl implements Watcher {
WatcherImpl(this.get);
final _watchers = <Card, List<CbWatcher>>{};
final T Function<T extends Object>(Card<T> card) get;
// ... other methods
}
_watchers
.get
method? It is needed so that when we call the attach
method we return the actual value instead of card.defaultValue
.And here's the thing. Our `Cardoteka' now looks like this:
abstract class Cardoteka {
Cardoteka({
required Config config,
Watcher? watcher,
}) :
_watcher = watcher,
_config = config;
final Watcher? _watcher;
}
Let's try to pass a parameter:
class UserStore extends Cardoteka {
UserStore({super.watcher = Watcher(...), required super.config});
}
And at this step we encounter problem №1. We cannot pass a non-constant object as the default value.
If we try it this way:
class UserStore extends Cardoteka {
UserStore({required super.watcher, required super.config});
}
main(){
UserStore(
config: ...,
watcher: WatcherImpl(get), // get doesn't exist yet
);
}
Then we cannot pass the get
method because it belongs to an instance of a class that has not yet been created. The problem №2.
That's the kind of circular bail...
Which I've figured out at this point:
WatcherImpl
implementation will always be non-constant, since we have to implement mutable _watchers
where the card and the callbacks belonging to it are contained.watcher
to the Cardoteka
constructor looks familiar to many, but using mixin allows you to avoid looking for workarounds to access the getOrNull
method. Consider mixin as adding functionality to your CardotekaImpl
.WatcherImpl
mixin is currently fully customized and tested :)
We have to abandon the use of mixins to be able to make the class constant. Example - class
Watcher
We should probably use a simple composition.→ If mixin: Pro:
attach()
)Con:
→ If field (composition): Pro:
watcher
field can now be made privateCon:
attach()
method, where you have to throw anUnsupportedError
error when thenotifier == null
field.getOrNull(card)
in theattach(...)
method, because it's not available now.We could leave both ways, but an api that allows you to do the same thing in different ways is wrong in my opinion.