From f9165938b0d25f2238f56895732f7aa47a56faed Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 18 Jan 2024 07:01:46 -0700 Subject: [PATCH] More adjustments for blog post --- crates/gpui/examples/ownership_post.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/crates/gpui/examples/ownership_post.rs b/crates/gpui/examples/ownership_post.rs index 603b63b254..cd3b6264c3 100644 --- a/crates/gpui/examples/ownership_post.rs +++ b/crates/gpui/examples/ownership_post.rs @@ -4,12 +4,18 @@ struct Counter { count: usize, } +struct Change { + increment: usize, +} + +impl EventEmitter for Counter {} + fn main() { App::new().run(|cx: &mut AppContext| { let counter: Model = cx.new_model(|_cx| Counter { count: 0 }); - let observer = cx.new_model(|cx: &mut ModelContext| { - cx.observe(&counter, |observer, observed, cx| { - observer.count = observed.read(cx).count * 2; + let subscriber = cx.new_model(|cx: &mut ModelContext| { + cx.subscribe(&counter, |subscriber, _emitter, event, _cx| { + subscriber.count += event.increment * 2; }) .detach(); @@ -19,16 +25,11 @@ fn main() { }); counter.update(cx, |counter, cx| { - counter.count += 1; + counter.count += 2; cx.notify(); + cx.emit(Change { increment: 2 }); }); - assert_eq!(observer.read(cx).count, 2); + assert_eq!(subscriber.read(cx).count, 4); }); } - -struct Change { - delta: isize, -} - -impl EventEmitter for Counter {}