#reactive #stream #async

async-component

零开销响应式编程

23个版本 (8个重大更改)

0.9.0 2022年12月17日
0.7.0 2022年12月15日
0.4.2 2022年11月30日

#1950 in 异步

Download history 4/week @ 2024-03-08 15/week @ 2024-03-15 5/week @ 2024-03-29

每月73次下载

Apache-2.0

19KB
347 代码行

异步组件

零开销响应式编程

示例

查看 async_component/examples/example.rs 以获取简单示例。

查看 examples/gui-demo 项目,了解使用 gui(winit, raqote, pixels) 的示例。

代码

use async_component::AsyncComponent;

#[derive(Debug, AsyncComponent)]
struct CounterComponent {
    // State must be wrapped with StateCell
    #[state(Self::on_counter_update)]
    counter: StateCell<i32>,

    // Stream
    // It iterates every queued items in single poll to prevent slowdown.
    // If the stream is immediate and resolves indefinitely, the task will fall to infinite loop. See expanded code below.
    #[state(Self::on_counter_recv)]
    counter_recv: StreamCell<Receiver<i32>>,
}

impl CounterComponent {
    fn on_counter_update(&mut self, _: ()) {
        println!("Counter updated to: {}", *self.counter);
    }

    fn on_counter_recv(&mut self, counter: i32) {
        *self.sub_component.counter = counter;
    }
}

运行此组件流将首先打印初始值,如果通过通道发送新值,则将打印更改后的值。

Counter updated to: 0
Counter updated to: ...

依赖项

~1.5MB
~36K SLoC