diff --git a/crates/gpui/examples/ownership_post.rs b/crates/gpui/examples/ownership_post.rs new file mode 100644 index 0000000000000000000000000000000000000000..cd3b6264c36537ff61d84556201b801b887ab26b --- /dev/null +++ b/crates/gpui/examples/ownership_post.rs @@ -0,0 +1,35 @@ +use gpui::{prelude::*, App, AppContext, EventEmitter, Model, ModelContext}; + +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 subscriber = cx.new_model(|cx: &mut ModelContext| { + cx.subscribe(&counter, |subscriber, _emitter, event, _cx| { + subscriber.count += event.increment * 2; + }) + .detach(); + + Counter { + count: counter.read(cx).count * 2, + } + }); + + counter.update(cx, |counter, cx| { + counter.count += 2; + cx.notify(); + cx.emit(Change { increment: 2 }); + }); + + assert_eq!(subscriber.read(cx).count, 4); + }); +} diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 8f7345ae16ced7e84cb145de81635b8c805e3fe6..477bea143916688b84571b2da3ad24b9bbec52cf 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -111,14 +111,20 @@ pub struct App(Rc); /// configured, you'll start the app with `App::run`. impl App { /// Builds an app with the given asset source. - pub fn production(asset_source: Arc) -> Self { + pub fn new() -> Self { Self(AppContext::new( current_platform(), - asset_source, + Arc::new(()), http::client(), )) } + /// Assign + pub fn with_assets(self, asset_source: impl AssetSource) -> Self { + self.0.borrow_mut().asset_source = Arc::new(asset_source); + self + } + /// Start the application. The provided callback will be called once the /// app is fully launched. pub fn run(self, on_finish_launching: F) @@ -1167,7 +1173,7 @@ impl Context for AppContext { type Result = T; /// Build an entity that is owned by the application. The given function will be invoked with - /// a `ModelContext` and must return an object representing the entity. A `Model` will be returned + /// a `ModelContext` and must return an object representing the entity. A `Model` handle will be returned, /// which can be used to access the entity in a context. fn new_model( &mut self, diff --git a/crates/live_kit_client/examples/test_app.rs b/crates/live_kit_client/examples/test_app.rs index 9fc8aafd30c283df748796790964dab11151d9af..06f297083066a2e749d7ecc3d10f94d4a2167115 100644 --- a/crates/live_kit_client/examples/test_app.rs +++ b/crates/live_kit_client/examples/test_app.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc, time::Duration}; +use std::time::Duration; use futures::StreamExt; use gpui::{actions, KeyBinding, Menu, MenuItem}; @@ -12,7 +12,7 @@ actions!(live_kit_client, [Quit]); fn main() { SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); - gpui::App::production(Arc::new(())).run(|cx| { + gpui::App::new().run(|cx| { #[cfg(any(test, feature = "test-support"))] println!("USING TEST LIVEKIT"); diff --git a/crates/storybook/src/storybook.rs b/crates/storybook/src/storybook.rs index 7da1d67b307e660963c34297fc1fcfcca0c4222b..1c5ffb494bdb24dc794bce42aacc4a8265244b66 100644 --- a/crates/storybook/src/storybook.rs +++ b/crates/storybook/src/storybook.rs @@ -60,8 +60,7 @@ fn main() { }); let theme_name = args.theme.unwrap_or("One Dark".to_string()); - let asset_source = Arc::new(Assets); - gpui::App::production(asset_source).run(move |cx| { + gpui::App::new().with_assets(Assets).run(move |cx| { load_embedded_fonts(cx).unwrap(); let mut store = SettingsStore::default(); diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index a7c52e592fcb93089a4f88351d19d377ef9ce0bc..0c9f3d371e08e0c3a96ea3226f3fc2dadffa8c2e 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -67,7 +67,7 @@ fn main() { } log::info!("========== starting zed =========="); - let app = App::production(Arc::new(Assets)); + let app = App::new().with_assets(Assets); let (installation_id, existing_installation_id_found) = app .background_executor()