mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-28 08:35:33 +03:00
More adjustments for blog post
This commit is contained in:
parent
bef1b83265
commit
f9165938b0
@ -4,12 +4,18 @@ struct Counter {
|
|||||||
count: usize,
|
count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Change {
|
||||||
|
increment: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventEmitter<Change> for Counter {}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new().run(|cx: &mut AppContext| {
|
App::new().run(|cx: &mut AppContext| {
|
||||||
let counter: Model<Counter> = cx.new_model(|_cx| Counter { count: 0 });
|
let counter: Model<Counter> = cx.new_model(|_cx| Counter { count: 0 });
|
||||||
let observer = cx.new_model(|cx: &mut ModelContext<Counter>| {
|
let subscriber = cx.new_model(|cx: &mut ModelContext<Counter>| {
|
||||||
cx.observe(&counter, |observer, observed, cx| {
|
cx.subscribe(&counter, |subscriber, _emitter, event, _cx| {
|
||||||
observer.count = observed.read(cx).count * 2;
|
subscriber.count += event.increment * 2;
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
@ -19,16 +25,11 @@ fn main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
counter.update(cx, |counter, cx| {
|
counter.update(cx, |counter, cx| {
|
||||||
counter.count += 1;
|
counter.count += 2;
|
||||||
cx.notify();
|
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<Change> for Counter {}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user