Skip to content

Commit

Permalink
refactor(core): 💡 Removed unnecessary Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Aug 10, 2024
1 parent afbb2a7 commit 901b113
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 77 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

- **core**: `PartData<T>` now supports `T: ?Sized`, allowing us to separate trait objects from `State`.(#614 @M-Adoo)

### Breaking

- **core**: Removed unnecessary `Writer` since it has the same capabilities as `Stateful`. (#pr @M-Adoo)


## [0.4.0-alpha.4] - 2024-08-07

### Features
Expand Down
8 changes: 4 additions & 4 deletions core/src/animation/stagger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<T: Transition + 'static> Stagger<T> {
{
let transition = Box::new(self.transition.clone());
let animate = rdl! { Animate { transition, state, from } };
self.push_animation_with(stagger, animate.clone_writer().into_inner());
self.push_animation_with(stagger, animate.clone_writer());
animate
}

Expand Down Expand Up @@ -168,7 +168,7 @@ impl<T: Transition + 'static> Animation for Stateful<Stagger<T>> {
}

fn box_clone(&self) -> Box<dyn Animation> {
let c = self.clone_writer().into_inner();
let c = self.clone_writer();
Box::new(c)
}
}
Expand All @@ -187,7 +187,7 @@ impl<T: Transition + 'static> Stateful<Stagger<T>> {
this.forget_modifies();
drop(this);

let this = self.clone_writer().into_inner();
let this = self.clone_writer();
let h = observable::timer_at((), at, AppCtx::scheduler()).subscribe(move |_| {
next.run();
this.trigger_next();
Expand Down Expand Up @@ -265,7 +265,7 @@ mod tests {
Duration::from_millis(100),
EasingTransition { duration: Duration::ZERO, easing: easing::LINEAR },
);
let c_stagger = stagger.clone_writer().into_inner();
let c_stagger = stagger.clone_writer();
let w = fn_widget! {
let mut mock_box = @MockBox { size: Size::new(100., 100.) };
$stagger.write().push_state(
Expand Down
4 changes: 2 additions & 2 deletions core/src/animation/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct RepeatTransition<T> {
/// Trait help to transition the state.
pub trait TransitionState: Sized + 'static {
/// Use an animate to transition the state after it modified.
fn transition(self, transition: Box<dyn Transition>, ctx: &BuildCtx) -> Writer<Animate<Self>>
fn transition(self, transition: Box<dyn Transition>, ctx: &BuildCtx) -> Stateful<Animate<Self>>
where
Self: AnimateState,
{
Expand Down Expand Up @@ -58,7 +58,7 @@ impl<S: AnimateState + 'static> TransitionState for S {}
pub trait TransitionWithFn: AnimateStateSetter + Sized {
fn transition_with<F>(
self, transition: Box<dyn Transition>, lerp_fn: F, ctx: &BuildCtx,
) -> Writer<Animate<LerpFnState<Self, F>>>
) -> Stateful<Animate<LerpFnState<Self, F>>>
where
F: FnMut(&Self::Value, &Self::Value, f32) -> Self::Value + 'static,
{
Expand Down
2 changes: 1 addition & 1 deletion core/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ mod tests {
wid: Option<WidgetId>,
}

fn build(task: Writer<Task>) -> impl IntoWidget<'static, FN> {
fn build(task: Stateful<Task>) -> impl IntoWidget<'static, FN> {
fn_widget! {
@TaskWidget {
keep_alive: pipe!($task.pin),
Expand Down
2 changes: 1 addition & 1 deletion core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<T: 'static> StateWatcher for State<T> {
}

impl<T: 'static> StateWriter for State<T> {
type Writer = Writer<T>;
type Writer = Stateful<T>;
type OriginWriter = Self;

#[inline]
Expand Down
63 changes: 2 additions & 61 deletions core/src/state/stateful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub struct Stateful<W> {

pub struct Reader<W>(Sc<StateCell<W>>);

pub struct Writer<W>(Stateful<W>);

/// The notifier is a `RxRust` stream that emit notification when the state
/// changed.
#[derive(Default, Clone)]
Expand Down Expand Up @@ -80,7 +78,7 @@ impl<W: 'static> StateWatcher for Stateful<W> {
}

impl<W: 'static> StateWriter for Stateful<W> {
type Writer = Writer<W>;
type Writer = Self;
type OriginWriter = Self;

#[inline]
Expand All @@ -93,7 +91,7 @@ impl<W: 'static> StateWriter for Stateful<W> {
fn shallow(&self) -> WriteRef<W> { self.write_ref(ModifyScope::FRAMEWORK) }

#[inline]
fn clone_writer(&self) -> Self::Writer { Writer(self.clone()) }
fn clone_writer(&self) -> Self::Writer { self.clone() }

#[inline]
fn origin_writer(&self) -> &Self::OriginWriter { self }
Expand Down Expand Up @@ -124,51 +122,6 @@ impl<W: 'static> StateReader for Reader<W> {
}
}

impl<W: 'static> StateReader for Writer<W> {
type Value = W;
type OriginReader = Self;
type Reader = Reader<W>;

#[inline]
fn read(&'_ self) -> ReadRef<W> { self.0.read() }

#[inline]
fn clone_reader(&self) -> Self::Reader { self.0.clone_reader() }

#[inline]
fn origin_reader(&self) -> &Self::OriginReader { self }

#[inline]
fn try_into_value(self) -> Result<Self::Value, Self> { self.0.try_into_value().map_err(Writer) }
}

impl<V: 'static> StateWatcher for Writer<V> {
#[inline]
fn raw_modifies(&self) -> CloneableBoxOp<'static, ModifyScope, Infallible> {
self.0.raw_modifies()
}
}

impl<V: 'static> StateWriter for Writer<V> {
type Writer = Self;
type OriginWriter = Self;

#[inline]
fn write(&self) -> WriteRef<V> { self.0.write() }

#[inline]
fn silent(&self) -> WriteRef<V> { self.0.silent() }

#[inline]
fn shallow(&self) -> WriteRef<V> { self.0.shallow() }

#[inline]
fn clone_writer(&self) -> Self { self.0.clone_writer() }

#[inline]
fn origin_writer(&self) -> &Self::OriginWriter { self }
}

impl<W> Drop for Stateful<W> {
fn drop(&mut self) { self.info.dec_writer(); }
}
Expand All @@ -184,11 +137,6 @@ impl Drop for WriterInfo {
}
}

impl<W> Writer<W> {
#[inline]
pub fn into_inner(self) -> Stateful<W> { self.0 }
}

impl<W> Stateful<W> {
pub fn new(data: W) -> Self {
Self { data: Sc::new(StateCell::new(data)), info: Sc::new(WriterInfo::new()) }
Expand Down Expand Up @@ -248,13 +196,6 @@ impl<W: Render> IntoWidgetStrict<'static, RENDER> for Reader<W> {
}
}

impl<'w, W, const M: usize> IntoWidgetStrict<'w, M> for Writer<W>
where
Stateful<W>: IntoWidget<'w, M>,
{
fn into_widget_strict(self) -> Widget<'w> { self.0.into_widget() }
}

impl Notifier {
pub(crate) fn raw_modifies(&self) -> CloneableBoxOp<'static, ModifyScope, Infallible> {
self.0.clone().box_it()
Expand Down
2 changes: 1 addition & 1 deletion core/src/widget_tree/layout_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ mod tests {
@ { pipe!($child_box.size.is_empty()).map(move|b| if b {
MockBox { size: Size::new(1., 1.) }.into_widget()
} else {
child_box.clone_writer().into_inner().into_widget()
child_box.clone_writer().into_widget()
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions docs/en/get_started/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,8 @@ let _: &State<AppData> = state.origin_reader();
let _: &State<AppData> = state.origin_writer();

// the sub-state's origin state is where it splits from
let _: &Writer<AppData> = split_count.origin_reader();
let _: &Writer<AppData> = split_count.origin_writer();
let _: &Stateful<AppData> = split_count.origin_reader();
let _: &Stateful<AppData> = split_count.origin_writer();
```

## The next step
Expand Down
4 changes: 2 additions & 2 deletions docs/zh/get_started/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,8 @@ let _: &State<AppData> = state.origin_reader();
let _: &State<AppData> = state.origin_writer();

// 子状态的源状态是它的父亲
let _: &Writer<AppData> = split_count.origin_reader();
let _: &Writer<AppData> = split_count.origin_writer();
let _: &Stateful<AppData> = split_count.origin_reader();
let _: &Stateful<AppData> = split_count.origin_writer();
```

## 下一步
Expand Down
4 changes: 2 additions & 2 deletions examples/todos/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn task_lists(
fn_widget! {
let editing = Stateful::new(None);
let stagger = Stagger::new(Duration::from_millis(100), transitions::EASE_IN_OUT.of(ctx!()));
let c_stagger = stagger.clone_writer().into_inner();
let c_stagger = stagger.clone_writer();

@VScrollBar {
on_mounted: move |_| c_stagger.run(),
Expand Down Expand Up @@ -123,7 +123,7 @@ fn input(
}
.into_widget()
}
fn task_item_widget<S>(task: S, stagger: Writer<Stagger<Box<dyn Transition>>>) -> Widget<'static>
fn task_item_widget<S>(task: S, stagger: Stateful<Stagger<Box<dyn Transition>>>) -> Widget<'static>
where
S: StateWriter<Value = Task> + 'static,
S::OriginWriter: StateWriter<Value = Todos>,
Expand Down
2 changes: 1 addition & 1 deletion tests/rdl_macro_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ fn fix_subscribe_cancel_after_widget_drop() {
reset_test_env!();

let notify_cnt = Stateful::new(0);
let cnt: Writer<i32> = notify_cnt.clone_writer();
let cnt = notify_cnt.clone_writer();
let trigger = Stateful::new(true);
let c_trigger = trigger.clone_watcher();
let w = fn_widget! {
Expand Down

0 comments on commit 901b113

Please sign in to comment.