Skip to content

Commit

Permalink
rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
rodydavis committed Nov 21, 2024
1 parent b89ce27 commit 84690e4
Show file tree
Hide file tree
Showing 50 changed files with 1,103 additions and 1,393 deletions.
2 changes: 1 addition & 1 deletion examples/auth_flow/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Auth {
final api = ServerApi();

/// Current user signal
late final currentUser = api.userStream().toSignal();
late final currentUser = api.userStream().toStreamSignal();

late final settings = streamSignal(
() => api.todosStream(currentUser().value?.id ?? 0),
Expand Down
2 changes: 1 addition & 1 deletion examples/dart_examples/bin/async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() {
}

/// Sync on user emitted value with a default emission of 'guest'
final user = fetch().toSignal(initialValue: 'guest');
final user = fetch().toFutureSignal(initialValue: 'guest');

/// When user Future resolve
final greeting = computed(() => 'Hello, ${user.value.value}');
Expand Down
2 changes: 1 addition & 1 deletion examples/dart_mappable_example/lib/graph_signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:signals/signals.dart';

import 'graph.dart';

class GraphSignal extends Signal<Graph> with ValueSignalMixin implements Graph {
class GraphSignal extends Signal<Graph> implements Graph {
GraphSignal(
super.value, {
super.debugLabel,
Expand Down
2 changes: 1 addition & 1 deletion examples/drift_example/lib/screens/home/drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CategoriesDrawer extends StatefulWidget {
}

class _CategoriesDrawerState extends State<CategoriesDrawer> {
final stream = AppDatabase.instance().categoriesWithCount().toSignal();
final stream = AppDatabase.instance().categoriesWithCount().toStreamSignal();

@override
Widget build(BuildContext context) {
Expand Down
9 changes: 5 additions & 4 deletions examples/node_based_editor/lib/nodes/stepper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StepperNode extends NumberNode {
required Node<dynamic, Object> increment,
required Node<dynamic, Object> decrement,
}) : super.computed(inputs: [increment, decrement]) {
output = signal(initial);
output = trackedSignal(initial);
_cleanup.add(effect(() {
increment.output.value;
final out = output as Signal<num>;
Expand Down Expand Up @@ -66,14 +66,15 @@ class StepperNode extends NumberNode {
children: [
IconButton(
tooltip: 'Reset',
onPressed: () =>
(output as Signal<num>).value = output.initialValue,
onPressed: () => (output as Signal<num>).value =
(output as TrackedSignal<num>).initialValue,
icon: const Icon(Icons.restore),
),
IconButton(
tooltip: 'Undo',
onPressed: () => (output as Signal<num>).value =
output.previousValue ?? output.initialValue,
(output as TrackedSignal<num>).previousValue ??
(output as TrackedSignal<num>).initialValue,
icon: const Icon(Icons.refresh),
),
],
Expand Down
13 changes: 10 additions & 3 deletions packages/signals/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
## 6.0.0

- Switching to **preact_signals** package for core implementation
- Removing old deprecated methods
- Remove SignalStream controller
- Upgrade SignalsProvider/ComputedProvider/ReadonlySignalProvider to default imports
- Add FlutterSignal to extends ValueNotifier
- Add FlutterComputed/FlutterReadonlySignal to extends ValueListenable
- Removing deprecated methods
- Add more methods to SignalsMixin
- Add new mixins: EventSinkSignalMixin, SinkSignalMixin, StreamSignalMixin, ValueListenableSignalMixin, ValueNotifierSignalMixin
- Switching to **preact_signals** package for core implementation
- Removing old deprecated methods
- Add new mixins: EventSinkSignalMixin, SinkSignalMixin, StreamSignalMixin, SetSignalMixin, ListSignalMixin, MapSignalMixin, QueueSignalMixin, ChangeStackSignalMixin, IterableSignalMixin
- Update AsyncSignal to implement EventSink
- Removing previous/initial value from Signal and Computed in favor of TrackedSignal, TrackedSignalMixin and trackedSignal()
- Update SignalsObserver to include value for signal created (instead of peek())
- Updated examples
- Remove callback to signal
- Remove toSignal extension method (causing unintended casts) in favor of .$ for Object/Object?

## 5.5.0

Expand Down
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://dartsignals.dev/
documentation: https://dartsignals.dev/
version: 6.0.0-rc.1
version: 6.0.0-rc.2

environment:
sdk: ">=3.0.0 <4.0.0"
Expand All @@ -12,8 +12,8 @@ environment:
dependencies:
flutter:
sdk: flutter
signals_core: ^6.0.0-rc.1
signals_flutter: ^6.0.0-rc.1
signals_core: ^6.0.0-rc.2
signals_flutter: ^6.0.0-rc.2

dev_dependencies:
flutter_test:
Expand Down
7 changes: 6 additions & 1 deletion packages/signals_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

- Switching to **preact_signals** package for core implementation
- Removing old deprecated methods
- Add new mixins: EventSinkSignalMixin, SinkSignalMixin, StreamSignalMixin
- Add new mixins: EventSinkSignalMixin, SinkSignalMixin, StreamSignalMixin, SetSignalMixin, ListSignalMixin, MapSignalMixin, QueueSignalMixin, ChangeStackSignalMixin, IterableSignalMixin
- Update AsyncSignal to implement EventSink
- Removing previous/initial value from Signal and Computed in favor of TrackedSignal, TrackedSignalMixin and trackedSignal()
- Update SignalsObserver to include value for signal created (instead of peek())
- Updated examples
- Remove callback to signal
- Remove toSignal extension method (causing unintended casts) in favor of .$ for Object/Object?

## 5.5.0

Expand Down
11 changes: 9 additions & 2 deletions packages/signals_core/lib/signals_core.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// Core signals library
library signals_core;

export 'src/devtool.dart';
export 'src/async/connect.dart';
export 'src/extensions/future.dart';
export 'src/extensions/stream.dart';
export 'src/extensions/signal.dart';
export 'src/extensions/callback.dart';
export 'src/extensions/object.dart' hide OptionalSignalObjectUtils;
export 'src/utils/timer.dart';
export 'src/async/state.dart';
Expand All @@ -19,4 +19,11 @@ export 'src/core/signals.dart';
export 'src/mixins/event_sink.dart';
export 'src/mixins/sink.dart';
export 'src/mixins/stream.dart';
export 'src/devtool.dart';
export 'src/mixins/changestack.dart';
export 'src/mixins/iterable.dart';
export 'src/mixins/list.dart';
export 'src/mixins/set.dart';
export 'src/mixins/map.dart';
export 'src/mixins/queue.dart';
export 'src/mixins/tracked.dart';

21 changes: 12 additions & 9 deletions packages/signals_core/lib/src/core/computed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,26 @@ class Computed<T> extends signals.Computed<T>
fn = () => val;
flags = OUTDATED;
// _version = 0;
_initialValue = val;
_previousValue = null;
afterCreate(val);
return this;
}

@override
void afterCreate(T val) {
SignalsObserver.instance?.onComputedCreated(this);
}

@override
void beforeUpdate(T val) {
SignalsObserver.instance?.onComputedUpdated(this, val);
}

@override
final String? debugLabel;

/// Call the computed function and update the value
void recompute() {
value;
_previousValue = internalValue;
internalValue = fn();
flags |= OUTDATED | NOTIFIED;

Expand All @@ -390,10 +398,6 @@ class Computed<T> extends signals.Computed<T>
flags |= DISPOSED;
}

@override
T get initialValue => _initialValue;
late T _initialValue;

/// Returns a readonly signal
ReadonlySignal<T> readonly() => this;

Expand All @@ -418,9 +422,8 @@ class Computed<T> extends signals.Computed<T>

@override
set internalValue(T value) {
if (!isInitialized) _initialValue = value;
beforeUpdate(value);
super.internalValue = value;
SignalsObserver.instance?.onComputedUpdated(this, value);
}
}

Expand Down
28 changes: 14 additions & 14 deletions packages/signals_core/lib/src/core/observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ part of 'signals.dart';

/// {@template observer}
/// You can observe all signal values in the dart application by providing an implementation of `SignalsObserver`:
///
///
/// ```dart
/// abstract class SignalsObserver {
/// void onSignalCreated(Signal instance);
Expand All @@ -12,33 +12,33 @@ part of 'signals.dart';
/// static SignalsObserver? instance;
/// }
/// ```
///
///
/// > There is a prebuilt `LoggingSignalsObserver` for printing updates to the console.
///
///
/// To add the observer override the instance at the start of the application:
///
///
/// ```dart
/// void main() {
/// SignalsObserver.instance = LoggingSignalsObserver(); // or custom observer
/// ...
/// }
/// ```
///
///
/// This will have a slight performance hit since every update will be tracked via the observer. It is recommended to only set the `SignalsObserver.instance` in debug or profile mode.
/// @link https://dartsignals.dev/utilities/observer
/// {@endtemplate}
abstract class SignalsObserver {
/// Called when a signal is created.
void onSignalCreated(Signal instance);
void onSignalCreated<T>(Signal<T> instance, T value);

/// Called when a signal is updated.
void onSignalUpdated(Signal instance, dynamic value);
void onSignalUpdated<T>(Signal<T> instance, T value);

/// Called when a computed is created.
void onComputedCreated(Computed instance);
void onComputedCreated<T>(Computed<T> instance);

/// Called when a computed is updated.
void onComputedUpdated(Computed instance, dynamic value);
void onComputedUpdated<T>(Computed<T> instance, T value);

/// Called when a effect is created.
void onEffectCreated(Effect instance) {}
Expand All @@ -58,22 +58,22 @@ abstract class SignalsObserver {
/// Logs all signals and computed changes to the console.
class LoggingSignalsObserver extends SignalsObserver {
@override
void onComputedCreated(Computed instance) {
void onComputedCreated<T>(Computed<T> instance) {
log('computed created: [${instance.globalId}|${instance.debugLabel}]');
}

@override
void onComputedUpdated(Computed instance, value) {
void onComputedUpdated<T>(Computed<T> instance, T value) {
log('computed updated: [${instance.globalId}|${instance.debugLabel}] => $value');
}

@override
void onSignalCreated(Signal instance) {
log('signal created: [${instance.globalId}|${instance.debugLabel}] => ${instance.peek()}');
void onSignalCreated<T>(Signal<T> instance, T value) {
log('signal created: [${instance.globalId}|${instance.debugLabel}] => $value');
}

@override
void onSignalUpdated(Signal instance, value) {
void onSignalUpdated<T>(Signal<T> instance, T value) {
log('signal updated: [${instance.globalId}|${instance.debugLabel}] => $value');
}

Expand Down
18 changes: 11 additions & 7 deletions packages/signals_core/lib/src/core/readonly.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ abstract class ReadonlySignal<T> = signals.ReadonlySignal<T>

/// Readonly signal mixin for adding addition helper methods
mixin ReadonlySignalMixin<T> on signals.ReadonlySignal<T> {
/// Check if a signal value is set (does not subscribe)
bool get isInitialized;

/// Internal hook for after a signal is created
@internal
void afterCreate(T val);

/// Internal hook for after a signal is updated
@internal
void beforeUpdate(T val);

final _disposeCallbacks = <void Function()>{};

/// Add a cleanup function to be called when the signal is disposed
Expand All @@ -28,13 +39,6 @@ mixin ReadonlySignalMixin<T> on signals.ReadonlySignal<T> {
};
}

/// Value that the signal was created with
T get initialValue;

/// Previous value that was set before the current
T? get previousValue => _previousValue;
T? _previousValue;

// @override
// bool operator ==(Object other) {
// return other is ReadonlySignal<T> && value == other.value;
Expand Down
26 changes: 13 additions & 13 deletions packages/signals_core/lib/src/core/signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Signal<T> extends signals.Signal<T>
super.internalValue, {
this.debugLabel,
bool autoDispose = false,
}) : initialValue = internalValue {
}) {
this.autoDispose = autoDispose;
SignalsObserver.instance?.onSignalCreated(this);
afterCreate(super.internalValue);
}

/// Lazy signal that can be created with type T that
Expand All @@ -37,7 +37,15 @@ class Signal<T> extends signals.Signal<T>
}

@override
late T initialValue;
void afterCreate(T val) {
SignalsObserver.instance?.onSignalCreated(this, val);
isInitialized = true;
}

@override
void beforeUpdate(T val) {
SignalsObserver.instance?.onSignalUpdated(this, val);
}

@override
final String? debugLabel;
Expand All @@ -55,15 +63,8 @@ class Signal<T> extends signals.Signal<T>
throw SignalsWriteAfterDisposeError(this);
}
if (force || !isInitialized || !equalityCheck(val, internalValue)) {
final ready = isInitialized;
if (ready) _previousValue = internalValue;
beforeUpdate(val);
internalSetValue(val);
if (!ready) initialValue = val;
if (ready) {
SignalsObserver.instance?.onSignalUpdated(this, val);
} else {
SignalsObserver.instance?.onSignalCreated(this);
}
return true;
}
return false;
Expand Down Expand Up @@ -108,9 +109,8 @@ class Signal<T> extends signals.Signal<T>
/// ```
Signal<T> overrideWith(T val) {
version = 0;
afterCreate(val);
internalValue = val;
_previousValue = null;
isInitialized = true;
return this;
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/signals_core/lib/src/core/signals.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// ignore_for_file: constant_identifier_names
import 'package:meta/meta.dart';

import '../devtool.dart';
import '../utils/constants.dart';
import 'dart:developer' as developer;
Expand Down
Loading

0 comments on commit 84690e4

Please sign in to comment.