Skip to content

Commit

Permalink
bugfix/signal autodispose observation (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
nank1ro authored Dec 19, 2023
1 parent f46b91d commit 0dd5e2c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 34 deletions.
24 changes: 5 additions & 19 deletions docs/advanced/automatic-disposal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ description: Learn how the automatic disposal works

# Automatic Disposal

> TL;DR: Always dispose Effects and Observations, the rest will be disposed automatically.
By default when you create a `Signal`, `Computed`, `Resource` or `Effect` the library will dispose them automatically.

You can customize this behaviour for a specific trackable Object using the options, for example for a `Signal` you have to create it with `autoDispose` set to `false`.
Expand All @@ -19,25 +21,7 @@ If you want to disable it globally instead, use
SolidartConfig.autoDispose = false;
```

The automatic disposal happens automatically when there are no longer subscribers (for `Signal`, `Computed`, `Resource`) and when the __currently__ tracked dependencies are all disposed (for `Effect`).

You don't need to dispose manually an `Observation` unless you want to stop it before the `Signal` is disposed.
```dart
final counter = Signal(0);
final disposeObservation = counter.observe((previousValue, value) {
// do something
});
```

No need to call `disposeObservation()` if you need the observation to be called until the `counter` is alive.
In fact you don't need to store the `disposeObservation` variable:

```dart
final counter = Signal(0);
counter.observe((previousValue, value) {
// do something
});
```
The automatic disposal happens automatically when there are no longer subscribers and listeners (for `Signal`, `Computed`, `Resource`) and when the __currently__ tracked dependencies are all disposed (for `Effect`).

There is a single case that the automatic disposal won't cover:

Expand Down Expand Up @@ -112,6 +96,8 @@ void dispose() {
As you can see both the `count` and `name` signals needs to be disposed in order for the `Effect` to dispose.
This is the reason why I suggest to always dispose the `Effect`.

<Info>The same behavior applies to `Observation`</Info>

In any case, don't worry to call `dispose()` yourself. It won't produce any error if it's already disposed. It just skips the operation.
In fact in the source code the operation is skipped if the object is already disposed:

Expand Down
4 changes: 4 additions & 0 deletions packages/solidart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.5.3

- **BUGFIX**: Fix an auto dispose issue of Signals that have some active observations.

## 1.5.2

- **BUGFIX**: Fix DevTools extension with null signal name.
Expand Down
10 changes: 4 additions & 6 deletions packages/solidart/lib/src/core/atom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ part of 'core.dart';
@internal
class Atom {
/// {@macro atom}
Atom({
required bool canAutoDispose,
required this.name,
}) : _canAutoDispose = canAutoDispose;
Atom({required this.name});

final ReactiveContext _context = ReactiveContext.main;

Expand All @@ -29,8 +26,6 @@ class Atom {

bool disposed = false;

final bool _canAutoDispose;

final Set<Derivation> _observers = {};

bool get hasObservers => _observers.isNotEmpty;
Expand Down Expand Up @@ -58,10 +53,13 @@ class Atom {
_observers.remove(d);
if (_observers.isEmpty) {
_context.enqueueForUnobservation(this);
_mayDispose();
}
}

// coverage:ignore-start
void dispose() {}

void _mayDispose() {}
// coverage:ignore-end
}
3 changes: 1 addition & 2 deletions packages/solidart/lib/src/core/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ DisposeEffect createEffect(
ErrorCallback? onError,
EffectOptions? options,
}) {
return Effect(callback, onError: onError, options: options ?? EffectOptions())
.dispose;
return Effect(callback, onError: onError, options: options).dispose;
}
// coverage:ignore-end

Expand Down
1 change: 0 additions & 1 deletion packages/solidart/lib/src/core/reactive_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class ReactiveContext {
// if this observable had reactive observers, trigger the hooks
ob._isBeingObserved = false;
}
if (ob._canAutoDispose) ob.dispose();
}
}

Expand Down
10 changes: 5 additions & 5 deletions packages/solidart/lib/src/core/read_signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ class ReadSignal<T> extends Atom implements SignalBase<T> {
required T initialValue,
required super.name,
required this.options,
}) : _value = initialValue,
super(
canAutoDispose: options.autoDispose,
) {
}) : _value = initialValue {
_notifySignalCreation();
}

Expand Down Expand Up @@ -178,9 +175,12 @@ class ReadSignal<T> extends Atom implements SignalBase<T> {
};
}

@override
void _mayDispose() {
if (!options.autoDispose) return;
if (_listeners.isEmpty && _observers.isEmpty) dispose();
if (_listeners.isEmpty && _observers.isEmpty) {
dispose();
}
}

@override
Expand Down
2 changes: 1 addition & 1 deletion packages/solidart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: solidart
description: A simple State Management solution for Dart applications inspired by SolidJS
version: 1.5.2
version: 1.5.3
repository: https://github.com/nank1ro/solidart
documentation: https://docs.page/nank1ro/solidart~dev
topics:
Expand Down

0 comments on commit 0dd5e2c

Please sign in to comment.