Remove transitions; maybe work this back in in the future

This commit is contained in:
Greg Johnston 2022-10-15 07:59:37 -04:00
parent d0127ce30d
commit 2ed8a58894
6 changed files with 1 additions and 221 deletions

View File

@ -30,5 +30,4 @@ tokio-test = "0.4"
[features]
csr = ["dep:wasm-bindgen", "dep:wasm-bindgen-futures", "dep:web-sys"]
hydrate = ["dep:base64", "dep:js-sys", "dep:serde-wasm-bindgen", "dep:serde_json", "dep:wasm-bindgen", "dep:wasm-bindgen-futures", "dep:web-sys"]
ssr = ["dep:base64", "dep:serde_json", "dep:tokio"]
transition = []
ssr = ["dep:base64", "dep:serde_json", "dep:tokio"]

View File

@ -234,16 +234,6 @@ where
self.runtime.push_stack(Subscriber(id));
// actually run the effect
#[cfg(feature = "transition")]
if let Some(transition) = self.runtime.running_transition() && self.render_effect {
transition.effects.borrow_mut().push(id);
} else {
let curr = { self.value.borrow_mut().take() };
let v = { (self.f.borrow_mut())(curr) };
*self.value.borrow_mut() = Some(v);
}
#[cfg(not(feature = "transition"))]
{
let curr = { self.value.borrow_mut().take() };
let v = { (self.f.borrow_mut())(curr) };

View File

@ -1,5 +1,3 @@
#[cfg(feature = "transition")]
use crate::TransitionState;
use crate::{
debug_warn, AnyEffect, AnySignal, EffectId, ResourceId, ResourceState, Scope, ScopeDisposer,
ScopeId, ScopeState, SignalId, SignalState, StreamingResourceId, Subscriber,
@ -18,8 +16,6 @@ pub(crate) struct Runtime {
pub(crate) shared_context: RefCell<Option<SharedContext>>,
pub(crate) stack: RefCell<Vec<Subscriber>>,
pub(crate) scopes: RefCell<SlotMap<ScopeId, Rc<ScopeState>>>,
#[cfg(feature = "transition")]
pub(crate) transition: RefCell<Option<Rc<TransitionState>>>,
}
#[derive(Error, Debug)]
@ -142,22 +138,6 @@ impl Runtime {
self.stack.borrow().last().cloned()
}
#[cfg(feature = "transition")]
pub fn running_transition(&self) -> Option<Rc<TransitionState>> {
self.transition.borrow().as_ref().and_then(|t| {
if t.running.get() {
Some(Rc::clone(t))
} else {
None
}
})
}
#[cfg(feature = "transition")]
pub fn transition(&self) -> Option<Rc<TransitionState>> {
self.transition.borrow().as_ref().map(Rc::clone)
}
pub fn create_scope(
&'static self,
f: impl FnOnce(Scope),

View File

@ -73,11 +73,6 @@ impl Scope {
self.runtime.create_scope(f, Some(self))
}
#[cfg(feature = "transition")]
pub fn transition_pending(&self) -> bool {
self.runtime.transition().is_some()
}
pub fn untrack<T>(&self, f: impl FnOnce() -> T) -> T {
self.runtime.untrack(f)
}

View File

@ -172,31 +172,6 @@ where
});
}
// If transition is running, or contains this as a source, take from t_value
#[cfg(feature = "transition")]
if let Some(transition) = self.runtime.transition() {
self.runtime
.signal((self.scope, self.id), |signal_state: &SignalState<T>| {
if transition.running.get()
&& transition.signals.borrow().contains(&(self.scope, self.id))
{
f(signal_state
.t_value
.borrow()
.as_ref()
.expect("read ReadSignal under transition, without any t_value"))
} else {
f(&signal_state.value.borrow())
}
})
} else {
self.runtime
.signal((self.scope, self.id), |signal_state: &SignalState<T>| {
(f)(&signal_state.value.borrow())
})
}
#[cfg(not(feature = "transition"))]
self.runtime
.signal((self.scope, self.id), |signal_state: &SignalState<T>| {
(f)(&signal_state.value.borrow())
@ -325,29 +300,6 @@ where
self.runtime
.signal((self.scope, self.id), |signal_state: &SignalState<T>| {
// update value
#[cfg(feature = "transition")]
if let Some(transition) = self.runtime.running_transition() {
let mut t_value = signal_state.t_value.borrow_mut();
if let Some(t_value) = &mut *t_value {
(f)(t_value);
} else {
// fork reality, using the old value as the basis for the transitional value
let mut forked = (*signal_state.value.borrow()).clone();
(f)(&mut forked);
*t_value = Some(forked);
// track this signal
transition
.signals
.borrow_mut()
.insert((self.scope, self.id));
}
} else {
(f)(&mut *signal_state.value.borrow_mut());
}
#[cfg(not(feature = "transition"))]
(f)(&mut *signal_state.value.borrow_mut());
// notify subscribers
@ -417,8 +369,6 @@ pub(crate) struct SignalId(pub(crate) usize);
//#[derive(Debug)]
pub(crate) struct SignalState<T> {
value: RefCell<T>,
#[cfg(feature = "transition")]
t_value: RefCell<Option<T>>,
subscribers: RefCell<HashSet<Subscriber>>,
}
@ -426,16 +376,6 @@ impl<T> Debug for SignalState<T>
where
T: Debug,
{
#[cfg(feature = "transition")]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SignalState")
.field("value", &*self.value.borrow())
.field("t_value", &*self.t_value.borrow())
.field("subscribers", &*self.subscribers.borrow())
.finish()
}
#[cfg(not(feature = "transition"))]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SignalState")
.field("value", &*self.value.borrow())
@ -451,8 +391,6 @@ where
pub fn new(value: T) -> Self {
Self {
value: RefCell::new(value),
#[cfg(feature = "transition")]
t_value: Default::default(),
subscribers: Default::default(),
}
}
@ -462,9 +400,6 @@ pub(crate) trait AnySignal: Debug {
fn unsubscribe(&self, subscriber: Subscriber);
fn as_any(&self) -> &dyn Any;
#[cfg(feature = "transition")]
fn end_transition(&self, runtime: &'static Runtime);
}
impl<T> AnySignal for SignalState<T>
@ -478,27 +413,4 @@ where
fn as_any(&self) -> &dyn Any {
self
}
#[cfg(feature = "transition")]
fn end_transition(&self, runtime: &'static Runtime) {
let t_value = self.t_value.borrow_mut().take();
if let Some(value) = t_value {
*self.value.borrow_mut() = value;
let subs = { self.subscribers.borrow().clone() };
// run all its subscribers; if any of them are from scopes that have
// been disposed, unsubscribe them
let mut dropped_subs = Vec::new();
for subscriber in subs.iter() {
if subscriber.try_run(runtime).is_err() {
dropped_subs.push(subscriber);
}
}
for sub in dropped_subs {
self.subscribers.borrow_mut().remove(sub);
}
}
}
}

View File

@ -1,96 +0,0 @@
use std::{
cell::{Cell, RefCell},
collections::HashSet,
rc::Rc,
};
use crate::{
create_effect, create_signal, runtime::Runtime, spawn::queue_microtask, EffectId, ReadSignal,
Scope, ScopeId, SignalId, WriteSignal,
};
pub fn use_transition(cx: Scope) -> Transition {
let (pending, set_pending) = create_signal(cx, false);
Transition {
runtime: cx.runtime,
scope: cx,
pending,
set_pending,
}
}
#[derive(Copy, Clone)]
pub struct Transition {
runtime: &'static Runtime,
scope: Scope,
pending: ReadSignal<bool>,
set_pending: WriteSignal<bool>,
}
impl Transition {
pub fn start(&self, f: impl FnOnce()) {
if self.runtime.running_transition().is_some() {
f();
} else {
{
self.set_pending.update(|n| *n = true);
*self.runtime.transition.borrow_mut() = Some(Rc::new(TransitionState {
running: Cell::new(true),
resources: Default::default(),
signals: Default::default(),
effects: Default::default(),
}));
}
f();
if let Some(running_transition) = self.runtime.running_transition() {
running_transition.running.set(false);
let runtime = self.runtime;
let scope = self.scope;
let resources = running_transition.resources.clone();
let signals = running_transition.signals.clone();
let effects = running_transition.effects.clone();
let set_pending = self.set_pending;
// place this at end of task queue so it doesn't start at 0
queue_microtask(move || {
create_effect(scope, move |_| {
let pending = resources.borrow().iter().map(|p| p.get()).sum::<usize>();
if pending == 0 {
log::debug!("[Transition] transition complete");
for signal in signals.borrow().iter() {
log::debug!("[Transition] deferred signal");
runtime.any_signal(*signal, |signal| {
signal.end_transition(runtime);
});
}
for effect in effects.borrow().iter() {
log::debug!("[Transition] running deferred effect");
runtime.any_effect(*effect, |any_effect| {
any_effect.run(*effect);
});
}
log::debug!("[Transition] setting pending to false");
set_pending.update(|n| *n = false);
}
});
});
}
}
}
pub fn pending(&self) -> bool {
self.pending.get()
}
}
#[derive(Debug)]
pub(crate) struct TransitionState {
pub running: Cell<bool>,
pub resources: Rc<RefCell<HashSet<ReadSignal<usize>>>>,
pub signals: Rc<RefCell<HashSet<(ScopeId, SignalId)>>>,
pub effects: Rc<RefCell<Vec<(ScopeId, EffectId)>>>,
}