Skip to content

Commit

Permalink
adding fireImmediately for FutureSignal/StreamSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
rodydavis committed Nov 27, 2023
1 parent 47ea9dc commit c3e0557
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
4 changes: 4 additions & 0 deletions packages/preact_signals/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.3

- Adding `fireImmediately` for `FutureSignal`, `StreamSignal`

## 0.3.2

- Fixing generics for `FutureSignal`, `StreamSignal` via `SignalState`
Expand Down
26 changes: 23 additions & 3 deletions packages/preact_signals/lib/src/future_signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,31 @@ class FutureSignal<T> extends Signal<SignalState<T>> {
/// Future [Duration] to wait before timing out
final Duration? timeout;

/// If true then the future will be called immediately
final bool fireImmediately;

/// Creates a [FutureSignal] that wraps a [Future]
FutureSignal(this._getFuture, {this.timeout}) : super(SignalLoading<T>()) {
_init();
FutureSignal(
this._getFuture, {
this.timeout,
this.fireImmediately = true,
}) : super(SignalLoading<T>()) {
if (fireImmediately) _init();
_stale = !fireImmediately;
}

final Future<T> Function() _getFuture;
bool _stale = false;

/// Resets the signal by calling the [Future] again
void reset() {
_init();
_stale = true;
if (fireImmediately) _init();
}

void _init() {
if (!_stale) return;
_stale = false;
if (peek() is! SignalLoading<T>) {
value = SignalLoading<T>();
}
Expand All @@ -50,15 +62,23 @@ class FutureSignal<T> extends Signal<SignalState<T>> {
}
});
}

@override
SignalState<T> get value {
_init();
return super.value;
}
}

/// Create a [FutureSignal] from a [Future]
FutureSignal<T> futureSignal<T>(
Future<T> Function() compute, {
Duration? timeout,
bool fireImmediately = true,
}) {
return FutureSignal<T>(
compute,
timeout: timeout,
fireImmediately: fireImmediately,
);
}
21 changes: 19 additions & 2 deletions packages/preact_signals/lib/src/stream_signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@ class StreamSignal<T> extends Signal<SignalState<T>> {
/// If true then the stream will be cancelled on error
final bool? cancelOnError;

/// If true then the future will be called immediately
final bool fireImmediately;

/// Creates a [StreamSignal] that wraps a [Stream]
StreamSignal(
this._getStream, {
this.cancelOnError,
this.fireImmediately = true,
}) : super(SignalLoading<T>()) {
_init();
if (fireImmediately) _init();
_stale = !fireImmediately;
}

final Stream<T> Function() _getStream;
StreamSubscription<T>? _subscription;
bool _stale = false;

/// Resets the signal by calling the [Stream] again
void reset() {
_subscription?.cancel();
_init();
_stale = true;
if (fireImmediately) _init();
}

/// Stop stream subscription
Expand All @@ -42,6 +49,8 @@ class StreamSignal<T> extends Signal<SignalState<T>> {
}

void _init() {
if (!_stale) return;
_stale = false;
if (peek() is! SignalLoading<T>) {
value = SignalLoading<T>();
}
Expand All @@ -60,15 +69,23 @@ class StreamSignal<T> extends Signal<SignalState<T>> {
cancelOnError: cancelOnError,
);
}

@override
SignalState<T> get value {
_init();
return super.value;
}
}

/// Create a [StreamSignal] from a [Stream]
StreamSignal<T> streamSignal<T>(
Stream<T> Function() stream, {
bool? cancelOnError,
bool fireImmediately = true,
}) {
return StreamSignal<T>(
stream,
cancelOnError: cancelOnError,
fireImmediately: fireImmediately,
);
}
2 changes: 1 addition & 1 deletion packages/preact_signals/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: preact_signals
description: Signal, Computed and Effect built with dart
version: 0.3.2
version: 0.3.3
repository: https://github.com/rodydavis/preact_signals.dart

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

- Updating `preact_signals` to 0.3.3

## 1.0.0

- Updating `preact_signals` to 0.3.2
Expand Down
4 changes: 2 additions & 2 deletions packages/signals/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: signals
description: "signal, computed and effect built with dart and extensions for flutter"
repository: https://github.com/rodydavis/preact_signals.dart
version: 1.0.0
version: 1.0.1

environment:
sdk: ">=3.0.0 <4.0.0"
Expand All @@ -11,7 +11,7 @@ dependencies:
flutter:
sdk: flutter
flutter_preact_signals: ^0.3.2
preact_signals: ^0.3.2
preact_signals: ^0.3.3

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit c3e0557

Please sign in to comment.