Skip to content

Commit

Permalink
Merge pull request #146 from rodydavis/feat/use-flutter
Browse files Browse the repository at this point in the history
useSignal / useComputed / useSignalEffect
  • Loading branch information
rodydavis authored Apr 5, 2024
2 parents 12359e8 + 1b18174 commit 18735e9
Show file tree
Hide file tree
Showing 100 changed files with 5,758 additions and 1,731 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ jobs:
- uses: actions/checkout@v3
- uses: dart-lang/[email protected]

- name: Install melos
run: dart pub global activate melos

- uses: subosito/[email protected]
with:
channel: "stable"

- name: Install melos
run: dart pub global activate melos

- name: Install dependencies
run: melos exec --no-private flutter pub get

Expand Down
9 changes: 3 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ on:
jobs:
build:
runs-on: ubuntu-latest
concurrency:
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true

steps:
- uses: actions/checkout@v3
- uses: dart-lang/[email protected]

- name: Install melos
run: dart pub global activate melos

- uses: subosito/[email protected]
with:
channel: "stable"

- name: Install melos
run: dart pub global activate melos

- name: Install dependencies
run: melos exec --no-private flutter pub get

Expand Down
16 changes: 16 additions & 0 deletions packages/signals/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 5.0.0

- Adding `previousValue`/`initialValue` to `Signal`
- Removing `SignalEquality`
- Deprecating `ValueSignal` in favor of `Signal`
- Deprecating `.forceUpdate` in favor of `.set(..., force: true)`
- Removing old deprecated methods
- Adding more inline code documentation
- ListSignal/SetSignal now extends IterableSignal
- Fixing ChnageStackSignal getters for history/redos to return an iterable list
- Fixing async signal isCompleted race condition
- Adding `createSignal`/`createComputed`/`createEffect` for Flutter
- Adding `SignalsAutoDisposeMixin` for Flutter
- Update to `signals_core` 5.0.0
- Update to `signals_flutter` 5.0.0

## 4.5.1

- Fixing Watch in hot reload with auto dispose signals
Expand Down
6 changes: 6 additions & 0 deletions packages/signals/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[![Tests](https://github.com/rodydavis/signals.dart/actions/workflows/tests.yml/badge.svg)](https://github.com/rodydavis/signals.dart/actions/workflows/tests.yml)
[![Website](https://github.com/rodydavis/signals.dart/actions/workflows/website.yml/badge.svg)](https://github.com/rodydavis/signals.dart/actions/workflows/website.yml)
[![GitHub stars](https://img.shields.io/github/stars/rodydavis/signals.dart)](https://gitHub.com/rodydavis/signals.dart/stargazers/)
[![Coverage](https://codecov.io/gh/rodydavis/signals.dart/branch/main/graph/badge.svg?token=HvJYtaixiW)](https://codecov.io/gh/rodydavis/signals.dart)
[![GitHub issues](https://img.shields.io/github/issues/rodydavis/signals.dart)](https://github.com/rodydavis/signals.dart/issues/)
[![GitHub pull-requests](https://img.shields.io/github/issues-pr/rodydavis/signals.dart.svg)](https://gitHub.com/rodydavis/signals.dart/pull/)
[![melos](https://img.shields.io/badge/maintained%20with-melos-f700ff.svg?style=flat-square)](https://github.com/invertase/melos)

# Signals

Expand Down
25 changes: 14 additions & 11 deletions packages/signals/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,23 @@ packages:
path: ".."
relative: true
source: path
version: "4.4.0"
version: "4.5.1"
signals_core:
dependency: "direct overridden"
dependency: transitive
description:
path: "../../signals_core"
relative: true
source: path
version: "4.4.0"
name: signals_core
sha256: "29258212f4e89297e7a6abbc0a000fda934bfa6d8b5634c30ce31ec1033bf9e4"
url: "https://pub.dev"
source: hosted
version: "4.5.0"
signals_flutter:
dependency: "direct overridden"
dependency: transitive
description:
path: "../../signals_flutter"
relative: true
source: path
version: "4.4.0"
name: signals_flutter
sha256: dd4971b2a0165d1ef8de1a45d59e89cdcdd5fc1c8f7fa180d55e6d049be89e92
url: "https://pub.dev"
source: hosted
version: "4.5.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -231,3 +233,4 @@ packages:
version: "13.0.0"
sdks:
dart: ">=3.2.5 <4.0.0"
flutter: ">=1.17.0"
6 changes: 3 additions & 3 deletions packages/signals/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: "Reactivity made simple. Do more by doing less. Supports Flutter an
repository: https://github.com/rodydavis/signals.dart
homepage: https://rodydavis.github.io/signals.dart
documentation: https://rodydavis.github.io/signals.dart
version: 4.5.1
version: 5.0.0-beta.3

environment:
sdk: ">=3.0.0 <4.0.0"
Expand All @@ -12,8 +12,8 @@ environment:
dependencies:
flutter:
sdk: flutter
signals_core: ^4.5.0
signals_flutter: ^4.5.1
signals_core: ^5.0.0-beta.2
signals_flutter: ^5.0.0-beta.3

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions packages/signals_core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
# Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock

coverage/html/
coverage/coverage.json
12 changes: 12 additions & 0 deletions packages/signals_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
## 5.0.0

- Adding `previousValue`/`initialValue` to `Signal`
- Removing `SignalEquality`
- Deprecating `ValueSignal` in favor of `Signal`
- Deprecating `.forceUpdate` in favor of `.set(..., force: true)`
- Removing old deprecated methods
- Adding more inline code documentation
- ListSignal/SetSignal now extends IterableSignal
- Fixing ChnageStackSignal getters for history/redos to return an iterable list
- Fixing async signal isCompleted race condition

## 4.5.0

- Updating documentation comments
Expand Down
212 changes: 212 additions & 0 deletions packages/signals_core/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
[![Tests](https://github.com/rodydavis/signals.dart/actions/workflows/tests.yml/badge.svg)](https://github.com/rodydavis/signals.dart/actions/workflows/tests.yml)
[![Website](https://github.com/rodydavis/signals.dart/actions/workflows/website.yml/badge.svg)](https://github.com/rodydavis/signals.dart/actions/workflows/website.yml)
[![GitHub stars](https://img.shields.io/github/stars/rodydavis/signals.dart)](https://gitHub.com/rodydavis/signals.dart/stargazers/)
[![Coverage](https://codecov.io/gh/rodydavis/signals.dart/branch/main/graph/badge.svg?token=HvJYtaixiW)](https://codecov.io/gh/rodydavis/signals.dart)
[![GitHub issues](https://img.shields.io/github/issues/rodydavis/signals.dart)](https://github.com/rodydavis/signals.dart/issues/)
[![GitHub pull-requests](https://img.shields.io/github/issues-pr/rodydavis/signals.dart.svg)](https://gitHub.com/rodydavis/signals.dart/pull/)
[![melos](https://img.shields.io/badge/maintained%20with-melos-f700ff.svg?style=flat-square)](https://github.com/invertase/melos)

# Signals (Core)

Expand All @@ -7,3 +13,209 @@ Core library for the [signals](https://pub.dev/packages/signals) package.
## Documentation

Documentation is available [here](https://rodydavis.github.io/signals.dart/reference/overview/).

## Guide / API

The signals library exposes four functions which are the building blocks to model any business logic you can think of.

### `signal(initialValue)`

The `signal` function creates a new signal. A signal is a container for a value that can change over time. You can read a signal's value or subscribe to value updates by accessing its `.value` property.

```dart
import 'package:signals/signals.dart';
final counter = signal(0);
// Read value from signal, logs: 0
print(counter.value);
// Write to a signal
counter.value = 1;
```

Writing to a signal is done by setting its `.value` property. Changing a signal's value synchronously updates every [computed](#computedfn) and [effect](#effectfn) that depends on that signal, ensuring your app state is always consistent.

#### `signal.peek()`

In the rare instance that you have an effect that should write to another signal based on the previous value, but you _don't_ want the effect to be subscribed to that signal, you can read a signals's previous value via `signal.peek()`.

```dart
final counter = signal(0);
final effectCount = signal(0);
effect(() {
print(counter.value);
// Whenever this effect is triggered, increase `effectCount`.
// But we don't want this signal to react to `effectCount`
effectCount.value = effectCount.peek() + 1;
});
```

Note that you should only use `signal.peek()` if you really need it. Reading a signal's value via `signal.value` is the preferred way in most scenarios.

### `untracked(fn)`

In case when you're receiving a callback that can read some signals, but you don't want to subscribe to them, you can use `untracked` to prevent any subscriptions from happening.

```dart
final counter = signal(0);
final effectCount = signal(0);
final fn = () => effectCount.value + 1;
effect(() {
print(counter.value);
// Whenever this effect is triggered, run `fn` that gives new value
effectCount.value = untracked(fn);
});
```

### `computed(fn)`

Data is often derived from other pieces of existing data. The `computed` function lets you combine the values of multiple signals into a new signal that can be reacted to, or even used by additional computeds. When the signals accessed from within a computed callback change, the computed callback is re-executed and its new return value becomes the computed signal's value.

```dart
import 'package:signals/signals.dart';
final name = signal("Jane");
final surname = signal("Doe");
final fullName = computed(() => name.value + " " + surname.value);
// Logs: "Jane Doe"
print(fullName.value);
// Updates flow through computed, but only if someone
// subscribes to it. More on that later.
name.value = "John";
// Logs: "John Doe"
print(fullName.value);
```

Any signal that is accessed inside the `computed`'s callback function will be automatically subscribed to and tracked as a dependency of the computed signal.

### `effect(fn)`

The `effect` function is the last piece that makes everything reactive. When you access a signal inside an `effect`'s callback function, that signal and every dependency of said signal will be activated and subscribed to. In that regard it is very similar to [`computed(fn)`](#computedfn). By default all updates are lazy, so nothing will update until you access a signal inside `effect`.

```dart
import 'package:signals/signals.dart';
final name = signal("Jane");
final surname = signal("Doe");
final fullName = computed(() => name.value + " " + surname.value);
// Logs: "Jane Doe"
effect(() => print(fullName.value));
// Updating one of its dependencies will automatically trigger
// the effect above, and will print "John Doe" to the console.
name.value = "John";
```

You can destroy an effect and unsubscribe from all signals it was subscribed to, by calling the returned function.

```dart
import 'package:signals/signals.dart';
final name = signal("Jane");
final surname = signal("Doe");
final fullName = computed(() => name.value + " " + surname.value);
// Logs: "Jane Doe"
final dispose = effect(() => print(fullName.value));
// Destroy effect and subscriptions
dispose();
// Update does nothing, because no one is subscribed anymore.
// Even the computed `fullName` signal won't change, because it knows
// that no one listens to it.
surname.value = "Doe 2";
```

#### Warning Cycles

Mutating a signal inside an effect will cause an infinite loop, because the effect will be triggered again. To prevent this, you can use [`untracked(fn)`](#untrackedfn) to read a signal without subscribing to it.

```dart
import 'dart:async';
import 'package:signals/signals.dart';
Future<void> main() async {
final completer = Completer<void>();
final age = signal(0);
effect(() {
print('You are ${age.value} years old');
age.value++; // <-- This will throw a cycle error
});
await completer.future;
}
```

### `batch(fn)`

The `batch` function allows you to combine multiple signal writes into one single update that is triggered at the end when the callback completes.

```dart
import 'package:signals/signals.dart';
final name = signal("Jane");
final surname = signal("Doe");
final fullName = computed(() => name.value + " " + surname.value);
// Logs: "Jane Doe"
effect(() => print(fullName.value));
// Combines both signal writes into one update. Once the callback
// returns the `effect` will trigger and we'll log "Foo Bar"
batch(() {
name.value = "Foo";
surname.value = "Bar";
});
```

When you access a signal that you wrote to earlier inside the callback, or access a computed signal that was invalidated by another signal, we'll only update the necessary dependencies to get the current value for the signal you read from. All other invalidated signals will update at the end of the callback function.

```dart
import 'package:signals/signals.dart';
final counter = signal(0);
final _double = computed(() => counter.value * 2);
final _triple = computed(() => counter.value * 3);
effect(() => print(_double.value, _triple.value));
batch(() {
counter.value = 1;
// Logs: 2, despite being inside batch, but `triple`
// will only update once the callback is complete
print(_double.value);
});
// Now we reached the end of the batch and call the effect
```

Batches can be nested and updates will be flushed when the outermost batch call completes.

```dart
import 'package:signals/signals.dart';
final counter = signal(0);
effect(() => print(counter.value));
batch(() {
batch(() {
// Signal is invalidated, but update is not flushed because
// we're still inside another batch
counter.value = 1;
});
// Still not updated...
});
// Now the callback completed and we'll trigger the effect.
```
Loading

0 comments on commit 18735e9

Please sign in to comment.