Skip to content

Commit

Permalink
refactor: move batch logic out of system
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jan 10, 2025
1 parent 4f20f1f commit d67dcdc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
1 change: 1 addition & 0 deletions pub/alien_signals/lib/alien_signals.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'src/batch.dart';
export 'src/computed.dart';
export 'src/effect.dart';
export 'src/effect_scope.dart';
Expand Down
39 changes: 39 additions & 0 deletions pub/alien_signals/lib/src/batch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'system.dart';

int batchDepth = 0;

/// Start a new batch of updates.
///
/// This function increments the batch depth counter, indicating the start of a new batch of updates.
/// Batching updates can help optimize performance by reducing the number of times the system processes changes.
///
/// {@template alien_signals.batch.example}
/// ```Example
/// final a = signal(0);
/// final b = signal(0);
///
/// effect(() {
/// print('effect run');
/// });
///
/// startBatch();
/// a.set(1);
/// b.set(1);
/// endBatch();
/// ```
/// {@endtemplate}
void startBatch() {
++batchDepth;
}

/// End the current batch of updates.
///
/// This function decrements the batch depth counter. If the batch depth reaches zero, it triggers the processing
/// of any queued effects that were accumulated during the batch.
///
/// {@macro alien_signals.batch.example}
void endBatch() {
if ((--batchDepth) == 0) {
drainQueuedEffects();
}
}
4 changes: 4 additions & 0 deletions pub/alien_signals/lib/src/signal.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'batch.dart';
import 'effect.dart';
import 'system.dart';
import 'types.dart';
Expand Down Expand Up @@ -52,6 +53,9 @@ class Signal<T> implements Dependency, IWritableSignal<T> {
currentValue = value;
if (subs != null) {
propagate(subs);
if (batchDepth == 0) {
drainQueuedEffects();
}
}
}
}
Expand Down
43 changes: 1 addition & 42 deletions pub/alien_signals/lib/src/system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,48 +112,11 @@ class Link {
Link? nextDep;
}

int _batchDepth = 0;
IEffect? _queuedEffects;
IEffect? _queuedEffectsTail;
Link? _linkPool;

/// Start a new batch of updates.
///
/// This function increments the batch depth counter, indicating the start of a new batch of updates.
/// Batching updates can help optimize performance by reducing the number of times the system processes changes.
///
/// {@template alien_signals.batch.example}
/// ```Example
/// final a = signal(0);
/// final b = signal(0);
///
/// effect(() {
/// print('effect run');
/// });
///
/// startBatch();
/// a.set(1);
/// b.set(1);
/// endBatch();
/// ```
/// {@endtemplate}
void startBatch() {
++_batchDepth;
}

/// End the current batch of updates.
///
/// This function decrements the batch depth counter. If the batch depth reaches zero, it triggers the processing
/// of any queued effects that were accumulated during the batch.
///
/// {@macro alien_signals.batch.example}
void endBatch() {
if ((--_batchDepth) == 0) {
_drainQueuedEffects();
}
}

void _drainQueuedEffects() {
void drainQueuedEffects() {
while (_queuedEffects != null) {
final effect = _queuedEffects!;
final queuedNext = effect.nextNotify;
Expand Down Expand Up @@ -329,10 +292,6 @@ void propagate(Link? link) {

break;
} while (true);

if (_batchDepth == 0) {
_drainQueuedEffects();
}
}

/// Propagate changes through the dependency graph shallowly
Expand Down

0 comments on commit d67dcdc

Please sign in to comment.