alien_signals 0.0.3 alien_signals: ^0.0.3 copied to clipboard
The lightest signal library - Dart implementation of alien-signals.
Alien Signals for Dart #
The lightest signal library for Dart, ported from stackblitz/alien-signals.
Installation #
Add to your pubspec.yaml
:
dependencies:
alien_signals: latest
Basic Usage #
import 'package:alien_signals/alien_signals.dart';
void main() {
// Create a signal
final count = signal(0);
// Create a computed value
final doubled = computed((_) => count.get() * 2);
// Create an effect
effect(() {
print('Count: ${count.get()}, Doubled: ${doubled.get()}');
});
// Update the signal
count.value = 1; // Prints: Count: 1, Doubled: 2
}
Core Concepts #
Signals #
Signals are reactive values that notify subscribers when they change:
final name = signal('Alice');
print(name.get()); // Get value using call `get` fn.
name.set('Bob'); //Set value using call `set` fn.
// or
print(name()); // Get value using call syntax
name('Bob'); // Set value using call syntax
// or
print(name.value); // Get using property syntax
name.value = 'Bob'; // Set using property syntax
Computed Values #
Computed values automatically derive from other reactive values:
final firstName = signal('John');
final lastName = signal('Doe');
final fullName = computed((_) => '${firstName.get()} ${lastName.get()}');
effect(() => print(fullName())); // Prints: John Doe
lastName.value = 'Smith'; // Prints: John Smith
Effects #
Effects run automatically when their dependencies change:
final user = signal('guest');
final e = effect(() {
print('Current user: ${user()}');
});
// Cleanup when done
e.stop();
Effect Scopes #
Group and manage related effects:
final scope = effectScope();
scope.run(() {
// Effects created here are grouped
effect(() => print('Effect 1'));
effect(() => print('Effect 2'));
});
// Clean up all effects in scope
scope.stop();
Compat #
Allows you to manipulate signals using the .value
property:
import 'package:alien_signals/alien_signals.dart';
import 'package:alien_signals/compat.dart';
void main() {
final count = signal(0);
effect(() => print(count.value); // print 0
count.value++; // print 1
}
API Reference #
See the API documentation for detailed information about all available APIs.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Credits #
This is a Dart port of the excellent stackblitz/alien-signals library.