1use gpui::{App, Application, Context, Entity, EventEmitter, prelude::*};
2
3struct Counter {
4 count: usize,
5}
6
7struct Change {
8 increment: usize,
9}
10
11impl EventEmitter<Change> for Counter {}
12
13fn main() {
14 Application::new().run(|cx: &mut App| {
15 let counter: Entity<Counter> = cx.new(|_cx| Counter { count: 0 });
16 let subscriber = cx.new(|cx: &mut Context<Counter>| {
17 cx.subscribe(&counter, |subscriber, _emitter, event, _cx| {
18 subscriber.count += event.increment * 2;
19 })
20 .detach();
21
22 Counter {
23 count: counter.read(cx).count * 2,
24 }
25 });
26
27 counter.update(cx, |counter, cx| {
28 counter.count += 2;
29 cx.notify();
30 cx.emit(Change { increment: 2 });
31 });
32
33 assert_eq!(subscriber.read(cx).count, 4);
34 });
35}