Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Add ownership post example

Nathan Sobobef1b8326557
diff --git a/crates/gpui/examples/ownership_post.rs b/crates/gpui/examples/ownership_post.rs
new file mode 100644
index 00000000000..603b63b2546
--- /dev/null
+++ b/crates/gpui/examples/ownership_post.rs
@@ -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 {}