Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/flutter_solidart/lib/src/widgets/signal_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'package:solidart/deps/preset.dart' as preset;
import 'package:solidart/deps/system.dart' as system;
import 'package:solidart/solidart.dart';

/// {@template signalbuilder}
Expand Down Expand Up @@ -83,9 +85,9 @@ class _SignalBuilderElement extends StatelessElement {

@override
Widget build() {
final prevSub = reactiveSystem.activeSub;
// ignore: invalid_use_of_protected_member
final node = reactiveSystem.activeSub = effect.subscriber;
final prevSub = preset.getActiveSub();
final node = effect;
preset.setActiveSub(node);

try {
final built = super.build();
Expand All @@ -98,10 +100,11 @@ You can disable this check by setting `SolidartConfig.assertSignalBuilderWithout
}
// ignore: invalid_use_of_internal_member
effect.setDependencies(node);
node.flags = system.ReactiveFlags.watching;

return built;
} finally {
reactiveSystem.activeSub = prevSub;
preset.setActiveSub(prevSub);
}
}
}
2 changes: 1 addition & 1 deletion packages/flutter_solidart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies:
flutter:
sdk: flutter
meta: ^1.11.0
solidart: ^2.8.2
solidart: ^2.8.3

dev_dependencies:
disco: ^1.0.0
Expand Down
2 changes: 1 addition & 1 deletion packages/solidart/example/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ignore_for_file: avoid_print

import 'package:solidart/solidart.dart';
import 'package:solidart/v3.dart';

void main() {
final count = Signal(0);
Expand Down
9 changes: 9 additions & 0 deletions packages/solidart/lib/advanced.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export 'src/v3.dart'
show
Configuration,
DisponsableMixin,
Disposable,
Identifier,
None,
Option,
Some;
1 change: 1 addition & 0 deletions packages/solidart/lib/deps/preset.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'package:alien_signals/preset.dart';
1 change: 1 addition & 0 deletions packages/solidart/lib/deps/system.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'package:alien_signals/system.dart';
75 changes: 11 additions & 64 deletions packages/solidart/lib/src/core/alien.dart
Original file line number Diff line number Diff line change
@@ -1,74 +1,21 @@
part of 'core.dart';

class _AlienComputed<T> extends alien.ReactiveNode implements _AlienUpdatable {
_AlienComputed(this.parent, this.getter)
: super(flags: 17 /* Mutable | Dirty */);
class _AlienComputed<T> extends preset.ComputedNode<T> {
_AlienComputed(this.parent, T Function(T? oldValue) getter)
: super(flags: system.ReactiveFlags.none, getter: getter);

final Computed<T> parent;
final T Function(T? oldValue) getter;

T? value;

void dispose() => reactiveSystem.stopEffect(this);

@override
bool update() {
final prevSub = reactiveSystem.setCurrentSub(this);
reactiveSystem.startTracking(this);
try {
final oldValue = value;
return oldValue != (value = getter(oldValue));
} finally {
reactiveSystem
..setCurrentSub(prevSub)
..endTracking(this);
}
}
}

class _AlienEffect extends alien.ReactiveNode {
_AlienEffect(this.parent, this.run, {bool? detach})
: detach = detach ?? SolidartConfig.detachEffects,
super(flags: 2 /* Watching */);

_AlienEffect? nextEffect;

final bool detach;
final Effect parent;
final void Function() run;

void dispose() => reactiveSystem.stopEffect(this);
void dispose() => preset.stop(this);
}

class _AlienSignal<T> extends alien.ReactiveNode implements _AlienUpdatable {
_AlienSignal(this.parent, this.value)
: previousValue = value,
super(flags: 1 /* Mutable */);
class _AlienSignal<T> extends preset.SignalNode<Option<T>> {
_AlienSignal(this.parent, Option<T> value)
: super(
flags: system.ReactiveFlags.mutable,
currentValue: value,
pendingValue: value,
);

final SignalBase<dynamic> parent;

Option<T> previousValue;
Option<T> value;

bool forceDirty = false;

@override
bool update() {
flags = 1 /* Mutable */;
if (forceDirty) {
forceDirty = false;
return true;
}
if (!parent._compare(previousValue.safeUnwrap(), value.safeUnwrap())) {
previousValue = value;
return true;
}

return false;
}
}

// ignore: one_member_abstracts
abstract interface class _AlienUpdatable {
bool update();
}
4 changes: 2 additions & 2 deletions packages/solidart/lib/src/core/batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ part of 'core.dart';
/// So when `x` changes, the effect is paused and you never see it printing:
/// "x = 11, y = 20".
T batch<T>(T Function() fn) {
reactiveSystem.startBatch();
preset.startBatch();
try {
return fn();
} finally {
reactiveSystem.endBatch();
preset.endBatch();
}
}
35 changes: 18 additions & 17 deletions packages/solidart/lib/src/core/computed.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
part of 'core.dart';
// ignore_for_file: unused_element

/// {@template computed}
/// A special Signal that notifies only whenever the selected
Expand Down Expand Up @@ -124,7 +125,7 @@ class Computed<T> extends ReadSignal<T> {
@override
bool get hasValue => true;

final _deps = <alien.ReactiveNode>{};
final _deps = <system.ReactiveNode>{};

@override
void dispose() {
Expand Down Expand Up @@ -152,7 +153,13 @@ class Computed<T> extends ReadSignal<T> {
return _untrackedValue;
}

final value = reactiveSystem.getComputedValue(_internalComputed);
if ((_internalComputed.flags & system.ReactiveFlags.pending) !=
system.ReactiveFlags.none &&
_internalComputed.deps == null) {
_internalComputed.flags &= ~system.ReactiveFlags.pending;
}

final value = _internalComputed.get();
if (autoDispose) {
_mayDispose();
}
Expand Down Expand Up @@ -223,27 +230,13 @@ class Computed<T> extends ReadSignal<T> {
_onDisposeCallbacks.add(cb);
}

// coverage:ignore-start
/// Indicates if the [oldValue] and the [newValue] are equal
@override
bool _compare(T? oldValue, T? newValue) {
// skip if the value are equals
if (equals) {
return oldValue == newValue;
}

// return the [comparator] result
return comparator(oldValue, newValue);
}
// coverage:ignore-end

/// Manually runs the computed to update its value.
/// This is usually not necessary, as the computed will automatically
/// update when its dependencies change.
/// However, in some cases, you may want to force an update.
void run() {
if (_disposed) return;
_internalComputed.update();
_internalComputed.didUpdate();
}

@override
Expand All @@ -254,4 +247,12 @@ class Computed<T> extends ReadSignal<T> {
value;
return '''Computed<$T>(value: $untrackedValue, previousValue: $untrackedPreviousValue)''';
}

@override
bool _compare(T? oldValue, T? newValue) {
if (equals) {
return oldValue == newValue;
}
return comparator(oldValue, newValue);
}
}
3 changes: 2 additions & 1 deletion packages/solidart/lib/src/core/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import 'dart:convert';
import 'dart:developer' as dev;
import 'dart:math';

import 'package:alien_signals/alien_signals.dart' as alien;
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:solidart/deps/preset.dart' as preset;
import 'package:solidart/deps/system.dart' as system;
import 'package:solidart/src/extensions/until.dart';
import 'package:solidart/src/utils.dart';

Expand Down
Loading