1#![cfg_attr(target_family = "wasm", no_main)]
2
3use gpui::{App, Context, Entity, EventEmitter, prelude::*};
4use gpui_platform::application;
5
6struct Counter {
7 count: usize,
8}
9
10struct Change {
11 increment: usize,
12}
13
14impl EventEmitter<Change> for Counter {}
15
16fn run_example() {
17 application().run(|cx: &mut App| {
18 let counter: Entity<Counter> = cx.new(|_cx| Counter { count: 0 });
19 let subscriber = cx.new(|cx: &mut Context<Counter>| {
20 cx.subscribe(&counter, |subscriber, _emitter, event, _cx| {
21 subscriber.count += event.increment * 2;
22 })
23 .detach();
24
25 Counter {
26 count: counter.read(cx).count * 2,
27 }
28 });
29
30 counter.update(cx, |counter, cx| {
31 counter.count += 2;
32 cx.notify();
33 cx.emit(Change { increment: 2 });
34 });
35
36 assert_eq!(subscriber.read(cx).count, 4);
37 });
38}
39
40#[cfg(not(target_family = "wasm"))]
41fn main() {
42 run_example();
43}
44
45#[cfg(target_family = "wasm")]
46#[wasm_bindgen::prelude::wasm_bindgen(start)]
47pub fn start() {
48 gpui_platform::web_init();
49 run_example();
50}