fix selector effect id
This commit is contained in:
1 parent
4e19f8f2be
commit
e77c158636
3 files changed
+19
-2
No files matched your search
@@ -51,12 +51,15 @@ pub(crate) fn get_effect_ref() -> EffectStackRef {
|
||||
Some(rt) => rt,
|
||||
None => {
|
||||
let (sender, mut receiver) = futures_channel::mpsc::unbounded();
|
||||
spawn(async move {
|
||||
spawn_forever(async move {
|
||||
while let Some(id) = receiver.next().await {
|
||||
EFFECT_STACK.with(|stack| {
|
||||
let effect_mapping = stack.effect_mapping.read();
|
||||
if let Some(effect) = effect_mapping.get(&id) {
|
||||
tracing::trace!("Rerunning effect: {:?}", id);
|
||||
effect.try_run();
|
||||
} else {
|
||||
tracing::trace!("Effect not found: {:?}", id);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -125,6 +128,7 @@ impl EffectInner {
|
||||
impl Drop for EffectInner {
|
||||
fn drop(&mut self) {
|
||||
EFFECT_STACK.with(|stack| {
|
||||
tracing::trace!("Dropping effect: {:?}", self.id);
|
||||
stack.effect_mapping.write().remove(&self.id);
|
||||
});
|
||||
}
|
||||
@@ -156,6 +160,7 @@ impl Effect {
|
||||
.write()
|
||||
.insert(myself.inner.id(), myself);
|
||||
});
|
||||
tracing::trace!("Created effect: {:?}", myself);
|
||||
|
||||
myself.try_run();
|
||||
|
||||
@@ -164,6 +169,7 @@ impl Effect {
|
||||
|
||||
/// Run the effect callback immediately. Returns `true` if the effect was run. Returns `false` is the effect is dead.
|
||||
pub fn try_run(&self) {
|
||||
tracing::trace!("Running effect: {:?}", self);
|
||||
if let Some(mut inner) = self.inner.try_write() {
|
||||
{
|
||||
EFFECT_STACK.with(|stack| {
|
||||
@@ -178,4 +184,9 @@ impl Effect {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the id of this effect.
|
||||
pub fn id(&self) -> GenerationalBoxId {
|
||||
self.inner.id()
|
||||
}
|
||||
}
|
||||
@@ -160,7 +160,8 @@ pub fn maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||
EFFECT_STACK.with(|stack| stack.effects.write().pop());
|
||||
}
|
||||
|
||||
let invalid_id = state.inner.value.id();
|
||||
let invalid_id = effect.id();
|
||||
tracing::trace!("Creating effect: {:?}", invalid_id);
|
||||
effect.inner.value.set(EffectInner {
|
||||
callback: Box::new(move || {
|
||||
let value = f();
|
||||
@@ -174,6 +175,9 @@ pub fn maybe_sync_selector<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||
}),
|
||||
id: invalid_id,
|
||||
});
|
||||
{
|
||||
EFFECT_STACK.with(|stack| stack.effect_mapping.write().insert(invalid_id, effect));
|
||||
}
|
||||
|
||||
ReadOnlySignal::new_maybe_sync(state)
|
||||
}
|
||||
@@ -201,6 +201,7 @@ impl<T: 'static> Signal<T> {
|
||||
|
||||
impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
/// Creates a new Signal. Signals are a Copy state management solution with automatic dependency tracking.
|
||||
#[tracing::instrument(skip(value))]
|
||||
pub fn new_maybe_sync(value: T) -> Self {
|
||||
Self {
|
||||
inner: CopyValue::<SignalData<T>, S>::new_maybe_sync(SignalData {
|
||||
@@ -213,6 +214,7 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
||||
}
|
||||
|
||||
/// Create a new signal with a custom owner scope. The signal will be dropped when the owner scope is dropped instead of the current scope.
|
||||
#[tracing::instrument(skip(value))]
|
||||
pub fn new_maybe_sync_in_scope(value: T, owner: ScopeId) -> Self {
|
||||
Self {
|
||||
inner: CopyValue::<SignalData<T>, S>::new_maybe_sync_in_scope(
|
||||
|
||||
Reference in new issue
Block a user