Signal Dependencies and Injections #83
Replies: 4 comments 12 replies
-
@LeeMatthewHiggins I was also thinking about the necessity of a family. I totally agree with your idea. upvote 👍 |
Beta Was this translation helpful? Give feedback.
-
I get the concepts around the notion of Signal Group and Fork. It has started with Operators. |
Beta Was this translation helpful? Give feedback.
-
Maybe call it |
Beta Was this translation helpful? Give feedback.
-
Added import 'signals.dart';
/// Signal container used to create signals based on args
class SignalContainer<T, Arg> {
/// If true then signals will be cached when created
final bool cache;
final Map<Arg, ReadonlySignal<T>> _cache = {};
final ReadonlySignal<T> Function(Arg) _create;
/// Signal container used to create multiple signals via args
SignalContainer(
this._create, {
this.cache = false,
});
/// Create the signal with the given args
ReadonlySignal<T> call(Arg arg) {
if (cache) {
return _cache.putIfAbsent(arg, () => _create(arg));
} else {
return _create(arg);
}
}
/// Dispose of all created signals
void dispose() {
for (final signal in _cache.values) {
signal.dispose();
}
}
/// Clear the cache
void clear() {
_cache.clear();
}
}
/// Create a signal container used to instance signals based on args
///
/// ```dart
/// final container = signalContainer<Cache, String>((e) {
/// return signal(Cache(e));
/// });
///
/// final cacheA = container('cache-a');
/// final cacheB = container('cache-b');
/// final cacheC = container('cache-c');
/// ```
SignalContainer<T, Arg> signalContainer<T, Arg>(
ReadonlySignal<T> Function(Arg) create, {
bool cache = false,
}) {
return SignalContainer<T, Arg>(
create,
cache: cache,
);
} |
Beta Was this translation helpful? Give feedback.
-
I have been experimenting with using signals in place of riverpod, I have a signal for my services and repositories, which is pretty cool as you can swap out different implementations in real time (though I mainly am trying this to limit my dependencies).
I have found it useful to be able to have a family of signals that are related.
Similar to families in riverpod.
Here's something I´ve been playing with:
I would be interested in thoughts.
If anyone thinks it's useful, I will be happy to do a pull request.
Beta Was this translation helpful? Give feedback.
All reactions