Skip to content

Commit

Permalink
refactor: no longer use activeTrackId, activeScopeTrackId
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jan 10, 2025
1 parent 0ad4d54 commit 4f20f1f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 93 deletions.
19 changes: 6 additions & 13 deletions pub/alien_signals/lib/src/computed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class Computed<T> implements IComputed, ISignal<T> {
@override
SubscriberFlags flags = SubscriberFlags.dirty;

@override
int? lastTrackedId = 0;

@override
Link? subs;

Expand All @@ -60,13 +57,9 @@ class Computed<T> implements IComputed, ISignal<T> {
shallowPropagate(subs);
}

if (activeTrackId != 0) {
if (lastTrackedId != activeTrackId) {
lastTrackedId = activeTrackId;
link(this, activeSub!);
}
} else if (activeScopeTrackId != 0 && lastTrackedId != activeScopeTrackId) {
lastTrackedId = activeScopeTrackId;
if (activeSub != null) {
link(this, activeSub!);
} else if (activeEffectScope != null) {
link(this, activeEffectScope!);
}

Expand All @@ -76,8 +69,8 @@ class Computed<T> implements IComputed, ISignal<T> {
@override
bool update() {
final prevSub = activeSub;
final prevTrackId = activeTrackId;
setActiveSub(this, nextTrackId());

setActiveSub(this);
startTrack(this);

try {
Expand All @@ -90,7 +83,7 @@ class Computed<T> implements IComputed, ISignal<T> {

return false;
} finally {
setActiveSub(prevSub, prevTrackId);
setActiveSub(prevSub);
endTrack(this);
}
}
Expand Down
41 changes: 9 additions & 32 deletions pub/alien_signals/lib/src/effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,13 @@ import 'system.dart';
/// The currently active subscriber.
Subscriber? activeSub;

/// The ID of the currently active track.
int activeTrackId = 0;

/// The ID of the last track that was used.
int lastTrackId = 0;

/// Sets the currently active subscriber and track ID.
/// Sets the currently active subscriber.
///
/// This function updates the global `activeSub` and `activeTrackId` variables
/// to the provided `sub` and `trackId` respectively.
/// This function updates the global `activeSub` variable to the provided `sub`.
///
/// @param sub The subscriber to set as active.
/// @param trackId The track ID to set as active.
void setActiveSub(Subscriber? sub, int trackId) {
void setActiveSub(Subscriber? sub) {
activeSub = sub;
activeTrackId = trackId;
}

/// Generates the next track ID.
///
/// This function increments the global `lastTrackId` by one and returns the new value.
///
/// @returns The next track ID.
int nextTrackId() {
return ++lastTrackId;
}

/// Executes a function without tracking dependencies.
Expand All @@ -48,12 +30,11 @@ int nextTrackId() {
/// ```
T untrack<T>(T Function() fn) {
final prevSub = activeSub;
final prevTrackId = activeTrackId;
setActiveSub(null, 0);
setActiveSub(null);
try {
return fn();
} finally {
setActiveSub(prevSub, prevTrackId);
setActiveSub(prevSub);
}
}

Expand Down Expand Up @@ -87,9 +68,9 @@ Effect<T> effect<T>(T Function() fn) {
class Effect<T> implements IEffect, Dependency {
/// {@macro alien_signals.effect}
Effect(this.fn) {
if (activeTrackId != 0) {
if (activeSub != null) {
link(this, activeSub!);
} else if (activeScopeTrackId != 0) {
} else if (activeEffectScope != null) {
link(this, activeEffectScope!);
}
}
Expand All @@ -106,9 +87,6 @@ class Effect<T> implements IEffect, Dependency {
@override
SubscriberFlags flags = SubscriberFlags.dirty;

@override
int? lastTrackedId;

@override
IEffect? nextNotify;

Expand Down Expand Up @@ -142,13 +120,12 @@ class Effect<T> implements IEffect, Dependency {
/// @returns The result of the effect function.
T run() {
final prevSub = activeSub;
final prevTrackId = activeTrackId;
setActiveSub(this, nextTrackId());
setActiveSub(this);
startTrack(this);
try {
return fn();
} finally {
setActiveSub(prevSub, prevTrackId);
setActiveSub(prevSub);
endTrack(this);
}
}
Expand Down
37 changes: 11 additions & 26 deletions pub/alien_signals/lib/src/effect_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,30 @@ import 'system.dart';
/// The currently active effect scope, if any.
EffectScope? activeEffectScope;

/// The track ID associated with the currently active effect scope.
int activeScopeTrackId = 0;

/// Sets the currently active effect scope and its associated track ID.
/// Sets the currently active effect scope.
///
/// This function updates the global variables [activeEffectScope] and
/// [activeScopeTrackId] with the provided [scope] and [trackId] respectively.
/// This function updates the global variable [activeEffectScope] with the provided [scope].
///
/// - Parameters:
/// - scope: The effect scope to set as active.
/// - trackId: The track ID associated with the effect scope.
void setActiveScope(EffectScope? scope, int trackId) {
/// - Parameter scope: The effect scope to set as active.
void setActiveScope(EffectScope? scope) {
activeEffectScope = scope;
activeScopeTrackId = trackId;
}

/// Executes a function without tracking the current effect scope.
///
/// This function temporarily sets the [activeEffectScope] and [activeScopeTrackId]
/// to `null` and `0` respectively, executes the provided function [fn], and then
/// restores the previous values of [activeEffectScope] and [activeScopeTrackId].
/// This function temporarily disables scope tracking by setting [activeEffectScope]
/// to `null`, executes the provided function [fn], and restores the previous
/// effect scope afterwards.
///
/// - Parameter fn: The function to execute without tracking the current effect scope.
/// - Parameter [fn]: The function to execute without tracking the current effect scope.
/// - Returns: The result of the executed function [fn].
T untrackScope<T>(T Function() fn) {
final prevSub = activeEffectScope;
final prevTrackId = activeScopeTrackId;
setActiveScope(null, 0);
setActiveScope(null);
try {
return fn();
} finally {
setActiveScope(prevSub, prevTrackId);
setActiveScope(prevSub);
}
}

Expand Down Expand Up @@ -80,11 +72,6 @@ class EffectScope implements IEffect {
@override
IEffect? nextNotify;

/// The track ID associated with this effect scope.
///
/// This ID is used to uniquely identify the effect scope within the system.
int trackId = nextTrackId();

@override
void notify() {
if ((flags & SubscriberFlags.innerEffectsPending) != 0) {
Expand All @@ -95,14 +82,12 @@ class EffectScope implements IEffect {

T run<T>(T Function() fn) {
final prevSub = activeEffectScope;
final prevTrackId = activeScopeTrackId;
activeEffectScope = this;
activeScopeTrackId = trackId;

try {
return fn();
} finally {
activeEffectScope = prevSub;
activeScopeTrackId = prevTrackId;
}
}

Expand Down
6 changes: 1 addition & 5 deletions pub/alien_signals/lib/src/signal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class Signal<T> implements Dependency, IWritableSignal<T> {
/// The current value held by the signal.
T currentValue;

@override
int? lastTrackedId = 0;

@override
Link? subs;

Expand All @@ -42,8 +39,7 @@ class Signal<T> implements Dependency, IWritableSignal<T> {

@override
T get() {
if (activeTrackId != 0 && lastTrackedId != activeTrackId) {
lastTrackedId = activeTrackId;
if (activeSub != null) {
link(this, activeSub!);
}

Expand Down
42 changes: 25 additions & 17 deletions pub/alien_signals/lib/src/system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ abstract interface class Dependency {

/// The tail of the linked list of subscribers.
Link? subsTail;

/// The ID of the last tracked dependency.
int? lastTrackedId;
}

/// [Subscriber] flags type def.
Expand Down Expand Up @@ -174,13 +171,24 @@ void _drainQueuedEffects() {
/// Create or reuse a link between a dependency and subscriber
void link(Dependency dep, Subscriber sub) {
final currentDep = sub.depsTail;
final nextDep = currentDep != null ? currentDep.nextDep : sub.deps;
if (currentDep != null && currentDep.dep == dep) {
return;
}

final nextDep = currentDep != null ? currentDep.nextDep : sub.deps;
if (nextDep != null && nextDep.dep == dep) {
sub.depsTail = nextDep;
} else {
_linkNewDep(dep, sub, nextDep, currentDep);
return;
}

final depLastSub = dep.subsTail;
if (depLastSub != null &&
depLastSub.sub == sub &&
_isValidLink(depLastSub, sub)) {
return;
}

_linkNewDep(dep, sub, nextDep, currentDep);
}

void _linkNewDep(
Expand Down Expand Up @@ -451,21 +459,25 @@ bool checkDirty(Link? link) {
/// Start tracking dependencies for a subscriber
void startTrack(Subscriber sub) {
sub.depsTail = null;
sub.flags = SubscriberFlags.tracking;
sub.flags =
(sub.flags & ~(SubscriberFlags.recursed | SubscriberFlags.notified)) |
SubscriberFlags.tracking;
}

/// End tracking dependencies for a subscriber
void endTrack(Subscriber sub) {
final depsTail = sub.depsTail;
if (depsTail != null) {
if (depsTail.nextDep != null) {
_clearTrack(depsTail.nextDep);
final nextDep = depsTail.nextDep;
if (nextDep != null) {
_clearTrack(nextDep);
depsTail.nextDep = null;
}
} else if (sub.deps != null) {
_clearTrack(sub.deps);
sub.deps = null;
}

sub.flags &= ~SubscriberFlags.tracking;
}

Expand All @@ -481,7 +493,6 @@ void _clearTrack(Link? link) {
link.nextSub = null;
} else {
dep.subsTail = prevSub;
dep.lastTrackedId = 0;
}

if (prevSub != null) {
Expand All @@ -496,14 +507,11 @@ void _clearTrack(Link? link) {
_linkPool = link;

if (dep.subs == null && dep is Subscriber) {
if (dep is IEffect) {
(dep as Subscriber).flags = SubscriberFlags.none;
} else {
final depFlags = (dep as Subscriber).flags;
if ((depFlags & SubscriberFlags.dirty) == 0) {
(dep as Subscriber).flags = depFlags | SubscriberFlags.dirty;
}
final depFlags = (dep as Subscriber).flags;
if (depFlags & SubscriberFlags.dirty == 0) {
(dep as Subscriber).flags = depFlags | SubscriberFlags.dirty;
}

final depDeps = (dep as Subscriber).deps;
if (depDeps != null) {
link = depDeps;
Expand Down

0 comments on commit 4f20f1f

Please sign in to comment.