-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Introduce network request handler #4194
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2c91efe
Impl NetworkMessageHandler
timon-schelling d3234d2
Repalce Frontend fetch like messages with NetworkMessage
timon-schelling 3a2b0d3
Reimpl resource loading with NetworkMessage
timon-schelling a4a97d2
Fix wasm
timon-schelling 25b0ce6
dedublicate resource requests
timon-schelling 0cbf742
Embedd font resources created by migration
timon-schelling 168ee59
Fixup
timon-schelling 015343d
Fix tests
timon-schelling 17aefe1
Fix font catalog not loading
timon-schelling db2b737
Cleanup
timon-schelling 6f4e6c9
Fix layouts not updating when font catalog is loaded
timon-schelling 0c65b26
Review
timon-schelling aa7273c
Review
timon-schelling af644d5
Cleanup
timon-schelling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| mod network_message; | ||
| mod network_message_handler; | ||
| pub mod utility_types; | ||
|
|
||
| #[doc(inline)] | ||
| pub use network_message::{NetworkMessage, NetworkMessageDiscriminant}; | ||
| #[doc(inline)] | ||
| pub use network_message_handler::{NetworkMessageContext, NetworkMessageHandler}; | ||
| #[doc(inline)] | ||
| pub use utility_types::Client; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| use std::pin::Pin; | ||
|
|
||
| use dyn_any::WasmNotSend; | ||
|
|
||
| use crate::messages::network::utility_types::Client; | ||
| use crate::messages::prelude::*; | ||
|
|
||
| #[impl_message(Message, Network)] | ||
| #[derive(derivative::Derivative, serde::Serialize, serde::Deserialize)] | ||
| #[derivative(Debug, PartialEq)] | ||
| pub enum NetworkMessage { | ||
| Request { | ||
| #[serde(skip, default)] | ||
| #[derivative(Debug = "ignore", PartialEq = "ignore")] | ||
| request: Option<RequestFn>, | ||
| }, | ||
| } | ||
| impl NetworkMessage { | ||
| pub fn request<F, Fut>(f: F) -> Self | ||
| where | ||
| F: FnOnce(Client) -> Fut + WasmNotSend + 'static, | ||
| Fut: Future<Output = Message> + WasmNotSend + 'static, | ||
| { | ||
| NetworkMessage::Request { | ||
| request: Some(Box::new(move |c| Box::pin(f(c)))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(target_family = "wasm"))] | ||
| type RequestFuture = Pin<Box<dyn Future<Output = Message> + Send>>; | ||
| #[cfg(target_family = "wasm")] | ||
| type RequestFuture = Pin<Box<dyn Future<Output = Message>>>; | ||
|
|
||
| #[cfg(not(target_family = "wasm"))] | ||
| type RequestFn = Box<dyn FnOnce(Client) -> RequestFuture + Send>; | ||
| #[cfg(target_family = "wasm")] | ||
| type RequestFn = Box<dyn FnOnce(Client) -> RequestFuture>; | ||
|
|
||
| impl Clone for NetworkMessage { | ||
| fn clone(&self) -> Self { | ||
| match self { | ||
| NetworkMessage::Request { .. } => { | ||
| log::error!("Cloning a NetworkMessage::Request is not supported"); | ||
| NetworkMessage::Request { request: None } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use crate::messages::network::utility_types::Client; | ||
| use crate::messages::prelude::*; | ||
|
|
||
| #[derive(ExtractField)] | ||
| pub struct NetworkMessageContext {} | ||
|
|
||
| #[derive(Debug, Default, ExtractField)] | ||
| pub struct NetworkMessageHandler { | ||
| client: Client, | ||
| } | ||
| #[message_handler_data] | ||
| impl MessageHandler<NetworkMessage, NetworkMessageContext> for NetworkMessageHandler { | ||
| fn process_message(&mut self, message: NetworkMessage, responses: &mut VecDeque<Message>, _context: NetworkMessageContext) { | ||
| match message { | ||
| NetworkMessage::Request { request } => { | ||
| if let Some(request) = request { | ||
| responses.add(request(self.client.clone())); | ||
| } else { | ||
| log::error!("received a empty NetworkMessage::Request"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| advertise_actions!(NetworkMessageDiscriminant; | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use reqwest::IntoUrl; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct Client { | ||
| inner: Option<reqwest::Client>, | ||
| } | ||
|
|
||
| impl Default for Client { | ||
| fn default() -> Self { | ||
| Self { | ||
| #[cfg(not(target_family = "wasm"))] | ||
| inner: reqwest::Client::builder().timeout(std::time::Duration::from_secs(100)).build().ok(), | ||
| #[cfg(target_family = "wasm")] | ||
| inner: reqwest::Client::builder().build().ok(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Client { | ||
| pub async fn fetch<U: IntoUrl>(&self, url: U) -> Option<Box<[u8]>> { | ||
| let Some(client) = &self.inner else { | ||
| log::error!("HTTP client failed to initialize, cannot fetch"); | ||
| return None; | ||
| }; | ||
| let response = client.get(url).send().await; | ||
| let response = response.and_then(|r| r.error_for_status()).map_err(|err| log::error!("failed to fetch: {err}")).ok()?; | ||
| let bytes = response.bytes().await.map_err(|err| log::error!("failed to read response body: {err}")).ok()?; | ||
| Some(bytes.to_vec().into_boxed_slice()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| use crate::messages::prelude::*; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Custom agent: PR title enforcement PR title is not in imperative mood and lacks a leading action verb required by the PR title convention. Prompt for AI agents |
||
| use graph_craft::application_io::resource::ResourceId; | ||
| use graph_craft::application_io::resource::{DataSource, ResourceHash, ResourceId}; | ||
| use graphene_std::text::Font; | ||
| use std::sync::Arc; | ||
|
|
||
|
|
@@ -8,7 +8,8 @@ use std::sync::Arc; | |
| pub enum ResourceMessage { | ||
| StoreEmbedded { resource_id: ResourceId, data: Arc<[u8]> }, | ||
| AddFont { resource_id: ResourceId, font: Font }, | ||
| Resolve, | ||
| ResolveStep { resource_id: ResourceId }, | ||
| Resolved { resource_id: ResourceId, data: Arc<[u8]> }, | ||
| ResolveAll, | ||
| Resolve { resource_id: ResourceId }, | ||
| Resolved { resource_id: ResourceId, source: DataSource, hash: ResourceHash }, | ||
| ResolveFailed { resource_id: ResourceId }, | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.