mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-09 00:50:04 +03:00
Add ownership post example
This commit is contained in:
parent
1d3ca8eb5d
commit
bef1b83265
34
crates/gpui/examples/ownership_post.rs
Normal file
34
crates/gpui/examples/ownership_post.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use gpui::{prelude::*, App, AppContext, EventEmitter, Model, ModelContext};
|
||||
|
||||
struct Counter {
|
||||
count: usize,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new().run(|cx: &mut AppContext| {
|
||||
let counter: Model<Counter> = cx.new_model(|_cx| Counter { count: 0 });
|
||||
let observer = cx.new_model(|cx: &mut ModelContext<Counter>| {
|
||||
cx.observe(&counter, |observer, observed, cx| {
|
||||
observer.count = observed.read(cx).count * 2;
|
||||
})
|
||||
.detach();
|
||||
|
||||
Counter {
|
||||
count: counter.read(cx).count * 2,
|
||||
}
|
||||
});
|
||||
|
||||
counter.update(cx, |counter, cx| {
|
||||
counter.count += 1;
|
||||
cx.notify();
|
||||
});
|
||||
|
||||
assert_eq!(observer.read(cx).count, 2);
|
||||
});
|
||||
}
|
||||
|
||||
struct Change {
|
||||
delta: isize,
|
||||
}
|
||||
|
||||
impl EventEmitter<Change> for Counter {}
|
Loading…
Reference in New Issue
Block a user