Skip to content

👾 The lightest signal library - Dart implementation of alien-signals

License

Notifications You must be signed in to change notification settings

medz/alien-signals-dart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Alien Signals on pub.dev

Alien Signals for Dart

The lightest signal library for Dart, ported from stackblitz/alien-signals.

Tip

Alien Signals is the fastest signal library currently, as shown by experimental results from 👉 dart-reactivity-benchmark.

Installation

To install Alien Signals, add the following to your pubspec.yaml:

dependencies:
  alien_signals: latest

Alternatively, you can run the following command:

dart pub add alien_signals

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.set(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 `get` method.
name.set('Bob');   // Set value using `set` method.

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.get())); // Prints: John Doe
lastName.set('Smith'); // Prints: John Smith

Effects

Effects run automatically when their dependencies change:

final user = signal('guest');
final e = effect(() {
  print('Current user: ${user.get()}');
});

// 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();

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.

About

👾 The lightest signal library - Dart implementation of alien-signals

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages