Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples #314

Merged
merged 6 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crux_core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ where
self.effects.is_empty() && self.events.is_empty() && self.tasks.is_empty()
}

/// Run the effect state machine until it settles and collect all effects generated
/// Run the effect state machine until it settles and return an iterator over the effects
pub fn effects(&mut self) -> impl Iterator<Item = Effect> + '_ {
self.run_until_settled();

self.effects.try_iter()
}

/// Run the effect state machine until it settles and collect all events generated
/// Run the effect state machine until it settles and return an iterator over the events
pub fn events(&mut self) -> impl Iterator<Item = Event> + '_ {
self.run_until_settled();

Expand Down
21 changes: 20 additions & 1 deletion crux_core/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
channel::Receiver, executor_and_spawner, CommandSpawner, Operation, ProtoContext,
QueuingExecutor,
},
Request, WithContext,
Command, Request, WithContext,
};

/// AppTester is a simplified execution environment for Crux apps for use in
Expand Down Expand Up @@ -237,6 +237,25 @@ impl<Ef, Ev> Update<Ef, Ev> {
}
}

impl<Effect, Event> Command<Effect, Event>
where
Effect: Send + 'static,
Event: Send + 'static,
{
/// Assert that the Command contains _exactly_ one effect and zero events,
/// and return the effect
pub fn expect_one_effect(&mut self) -> Effect {
if self.events().next().is_some() {
panic!("Expected only one effect, but found an event");
}
if let Some(effect) = self.effects().next() {
effect
} else {
panic!("Expected one effect but found none");
}
}
}

/// Panics if the pattern doesn't match an `Effect` from the specified `Update`
///
/// Like in a `match` expression, the pattern can be optionally followed by `if`
Expand Down
17 changes: 3 additions & 14 deletions examples/cat_facts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions examples/cat_facts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ rust-version = "1.66"

[workspace.dependencies]
anyhow = "1.0.95"
# crux_core = { path = "../../crux_core" }
# crux_http = { path = "../../crux_http" }
# crux_kv = { path = "../../crux_kv" }
# crux_platform = { path = "../../crux_platform" }
# crux_time = { path = "../../crux_time", features = ["chrono"] }
crux_core = "0.11.1"
crux_http = "0.11.4"
crux_kv = "0.6.0"
crux_platform = "0.3.0"
crux_time = { version = "0.8.0", features = ["chrono"] }
crux_core = { path = "../../crux_core" }
crux_http = { path = "../../crux_http" }
crux_kv = { path = "../../crux_kv" }
crux_platform = { path = "../../crux_platform" }
crux_time = { path = "../../crux_time", features = ["chrono"] }
# crux_core = "0.11.1"
# crux_http = "0.11.4"
# crux_kv = "0.6.0"
# crux_platform = "0.3.0"
# crux_time = { version = "0.8.0", features = ["chrono"] }
serde = "1.0.217"

[workspace.metadata.bin]
Expand Down
27 changes: 14 additions & 13 deletions examples/cat_facts/shared/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crux_core::{
render::{self, Render},
Capability, Command,
};
use crux_kv::{command::KeyValue as key_value, error::KeyValueError, KeyValue};
use crux_kv::{command::KeyValue, error::KeyValueError};
use crux_platform::Platform;
use crux_time::{Time, TimeResponse};
use crux_time::{command::Time, TimeResponse};

use platform::Capabilities;

Expand Down Expand Up @@ -94,10 +94,11 @@ pub struct CatFactCapabilities {
#[allow(unused)]
http: crux_http::Http<Event>,
#[allow(unused)]
key_value: KeyValue<Event>,
key_value: crux_kv::KeyValue<Event>,
platform: Platform<Event>,
render: Render<Event>,
time: Time<Event>,
#[allow(unused)]
StuartHarris marked this conversation as resolved.
Show resolved Hide resolved
time: crux_time::Time<Event>,
}

// Allow easily using Platform as a submodule
Expand Down Expand Up @@ -125,12 +126,13 @@ impl App for CatFacts {
) -> Command<Effect, Event> {
match msg {
Event::GetPlatform => {
self.platform
.update(platform::Event::Get, &mut model.platform, &caps.into());
let _ =
self.platform
.update(platform::Event::Get, &mut model.platform, &caps.into());
StuartHarris marked this conversation as resolved.
Show resolved Hide resolved
Command::done()
}
Event::Platform(msg) => {
self.platform.update(msg, &mut model.platform, &caps.into());
let _ = self.platform.update(msg, &mut model.platform, &caps.into());
Command::done()
}
Event::Clear => {
Expand All @@ -139,7 +141,7 @@ impl App for CatFacts {
let bytes = serde_json::to_vec(&model).unwrap();

Command::all([
key_value::set(KEY, bytes).then_send(|_| Event::None),
KeyValue::set(KEY, bytes).then_send(|_| Event::None),
render::render(),
])
}
Expand Down Expand Up @@ -168,16 +170,15 @@ impl App for CatFacts {
Event::SetFact(Ok(mut response)) => {
model.cat_fact = Some(response.take_body().unwrap());

caps.time.now(Event::CurrentTime);
Command::done()
Time::now().then_send(Event::CurrentTime)
}
Event::SetImage(Ok(mut response)) => {
model.cat_image = Some(response.take_body().unwrap());

let bytes = serde_json::to_vec(&model).unwrap();

Command::all([
key_value::set(KEY, bytes).then_send(|_| Event::None),
KeyValue::set(KEY, bytes).then_send(|_| Event::None),
render::render(),
])
}
Expand All @@ -192,12 +193,12 @@ impl App for CatFacts {
let bytes = serde_json::to_vec(&model).unwrap();

Command::all([
key_value::set(KEY, bytes).then_send(|_| Event::None),
KeyValue::set(KEY, bytes).then_send(|_| Event::None),
render::render(),
])
}
Event::CurrentTime(_) => panic!("Unexpected time response"),
Event::Restore => key_value::get(KEY).then_send(Event::SetState),
Event::Restore => KeyValue::get(KEY).then_send(Event::SetState),
Event::SetState(Ok(Some(value))) => match serde_json::from_slice::<Model>(&value) {
Ok(m) => {
*model = m;
Expand Down
3 changes: 2 additions & 1 deletion examples/cat_facts/web-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"@types/react": "19.0.7",
"@types/react-dom": "19.0.3",
"@types/ua-parser-js": "^0.7.39"
}
},
"packageManager": "[email protected]+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e"
}
26 changes: 12 additions & 14 deletions examples/notes/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions examples/notes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ rust-version = "1.66"

[workspace.dependencies]
anyhow = "1.0"
# crux_core = { path = "../../crux_core" }
# crux_kv = { path = "../../crux_kv" }
crux_core = "0.11.0"
crux_kv = "0.6.0"
crux_core = { path = "../../crux_core" }
crux_kv = { path = "../../crux_kv" }
crux_time = { path = "../../crux_time" }
# crux_core = "0.11.0"
# crux_kv = "0.6.0"
serde = "1.0"

[workspace.metadata.bin]
Expand Down
4 changes: 1 addition & 3 deletions examples/notes/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ name = "shared"
automerge = "0.4.1"
crux_core.workspace = true
crux_kv.workspace = true
crux_time.workspace = true
futures = "0.3"
lazy_static = "1.5"
serde = { workspace = true, features = ["derive"] }
Expand All @@ -35,6 +36,3 @@ uniffi = { version = "0.28.3", features = ["cli"] }

[build-dependencies]
uniffi = { version = "0.28.3", features = ["build"] }

[dev-dependencies]
assert_let_bind = "0.1.1"
Loading