SignalContainer#
Kind:
class & function |
Package: package:signals_core
Class: SignalContainer#
Signal container used to create signals based on args
final container = readonlySignalContainer<Cache, String>((e) {
return signal(Cache(e));
});
final cacheA = container('cache-a');
final cacheB = container('cache-b');
final cacheC = container('cache-c');
Example of settings and SharedPreferences:
class Settings {
final SharedPreferences prefs;
EffectCleanup? _cleanup;
Settings(this.prefs) {
_cleanup = effect(() {
for (final entry in setting.store.entries) {
final value = entry.value.peek();
if (prefs.getString(entry.key.$1) != value) {
prefs.setString(entry.key.$1, value).ignore();
}
}
});
}
late final setting = signalContainer<String, (String, String)>(
(val) => signal(prefs.getString(val.$1) ?? val.$2),
cache: true,
);
Signal<String> get darkMode => setting(('dark-mode', 'false'));
void dispose() {
_cleanup?.call();
setting.dispose();
}
}
void main() {
// Load or find instance
late final SharedPreferences prefs = ...;
// Create settings
final settings = Settings(prefs);
// Get value
print('dark mode: ${settings.darkMode}');
// Update value
settings.darkMode.value = 'true';
}
Members of SignalContainer#
| Member | Type | Signature | Description |
|---|---|---|---|
| cache | field |
dart bool cache |
If true then signals will be cached when created |
| onEvict | field |
dart void Function(Arg key, S signal)? onEvict |
Optional callback when a signal is removed/evicted from the cache |
| store | field |
dart store |
Store of created signals (if cache is true) |
| SignalContainer | constructor |
dart SignalContainer(this._create, {this.cache = false, this.onEvict}) |
Signal container used to create multiple signals via args |
| call | method |
dart S call(Arg arg) |
Create the signal with the given args |
| remove | method |
dart S? remove(Arg arg) |
Remove a signal from the cache |
| containsKey | method |
dart bool containsKey(Arg arg) |
Check if signal is currently stored in the cache |
| clear | method |
dart void clear() |
Clear the cache |
| dispose | method |
dart void dispose() |
Dispose of all created signals |
| length | method |
dart int length |
Returns the number of cached signals. |
| isEmpty | method |
dart bool isEmpty |
Returns true if the cache is empty. |
| isNotEmpty | method |
dart bool isNotEmpty |
Returns true if the cache is not empty. |
| keys | method |
dart Iterable |
Returns all currently cached keys. |
| values | method |
dart Iterable |
Returns all currently cached signals. |
| entries | method |
dart Iterable<MapEntry<Arg, S>> entries |
Returns all currently cached entries. |
| lookup | method |
dart S? lookup(Arg arg) |
Retrieve the cached signal for arg if it exists, without creating a new one if it is missing. |
| removeWhere | method |
dart void removeWhere(bool Function(Arg key, S signal) test) |
Filter and remove matching cached signals. |
Function: signalContainer#
SignalContainer<T, Arg, Signal<T>> signalContainer(Signal<T> Function(Arg) create, {bool cache = false, void Function(Arg key, Signal<T> signal)? onEvict})
Create a signal container used to instance signals based on args
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');
cacheA.value = 'a';
cacheB.value = 'b';
cacheC.value = 'c';
References#
The SignalContainer type is referenced and used in the following pages:
- SignalContainer (signals_flutter/utilities)
- signals_flutter
- SignalContainer (signals_core/utilities)
- signals_core
- SignalContainer (signals/utilities)
- signals