mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 14:18:04 +08:00
Make ResourceStorage trait &self-based and remove 'static requirement (#4188)
* Make ResourceStorage trait &self-based with elided-lifetime ResourceFuture * Adress review comments
This commit is contained in:
@@ -3,14 +3,14 @@ use crate::messages::prelude::*;
|
||||
use graph_craft::application_io::PlatformApplicationIo;
|
||||
use graph_craft::application_io::resource::ResourceStorage;
|
||||
pub use graphene_std::uuid::*;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
pub struct Editor {
|
||||
pub dispatcher: Dispatcher,
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
pub fn new(environment: Environment, uuid_random_seed: u64, resource_storage: Box<dyn ResourceStorage>, mut application_io: PlatformApplicationIo, wake: Wake) -> Self {
|
||||
pub fn new(environment: Environment, uuid_random_seed: u64, resource_storage: Arc<dyn ResourceStorage>, mut application_io: PlatformApplicationIo, wake: Wake) -> Self {
|
||||
ENVIRONMENT.set(environment).expect("Editor shoud only be initialized once");
|
||||
graphene_std::uuid::set_uuid_seed(uuid_random_seed);
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ use crate::messages::preferences::preferences_message_handler::PreferencesMessag
|
||||
use crate::messages::prelude::*;
|
||||
use crate::messages::tool::common_functionality::utility_functions::make_path_editable_is_allowed;
|
||||
use graph_craft::application_io::resource::ResourceStorage;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Dispatcher {
|
||||
@@ -39,7 +40,7 @@ pub struct DispatcherMessageHandlers {
|
||||
}
|
||||
|
||||
impl DispatcherMessageHandlers {
|
||||
pub fn with_resource_storage(resource_storage: Box<dyn ResourceStorage>) -> Self {
|
||||
pub fn with_resource_storage(resource_storage: Arc<dyn ResourceStorage>) -> Self {
|
||||
Self {
|
||||
resource_storage_message_handler: ResourceStorageMessageHandler::new(resource_storage),
|
||||
..Self::default()
|
||||
@@ -88,7 +89,7 @@ const DEBUG_MESSAGE_BLOCK_LIST: &[MessageDiscriminant] = &[
|
||||
const DEBUG_MESSAGE_ENDING_BLOCK_LIST: &[&str] = &["PointerMove", "PointerOutsideViewport", "Overlays", "Draw", "CurrentTime", "Time"];
|
||||
|
||||
impl Dispatcher {
|
||||
pub fn new(resource_storage: Box<dyn ResourceStorage>) -> Self {
|
||||
pub fn new(resource_storage: Arc<dyn ResourceStorage>) -> Self {
|
||||
let mut s = Self::default();
|
||||
s.message_handlers.resource_storage_message_handler = ResourceStorageMessageHandler::new(resource_storage);
|
||||
s
|
||||
|
||||
@@ -1,29 +1,26 @@
|
||||
use crate::messages::prelude::*;
|
||||
use graph_craft::application_io::resource::{LoadResource, ResourceFuture, ResourceHash, ResourceStorage};
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ResourcesHandle {
|
||||
inner: Arc<RwLock<Box<dyn ResourceStorage>>>,
|
||||
inner: Arc<dyn ResourceStorage>,
|
||||
}
|
||||
|
||||
impl LoadResource for ResourcesHandle {
|
||||
fn load(&self, hash: ResourceHash) -> ResourceFuture {
|
||||
let guard = self.inner.read().unwrap();
|
||||
guard.load(hash)
|
||||
fn load(&self, hash: ResourceHash) -> ResourceFuture<'_> {
|
||||
self.inner.load(hash)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(ExtractField)]
|
||||
pub struct ResourceStorageMessageHandler {
|
||||
storage: Option<Arc<RwLock<Box<dyn ResourceStorage>>>>,
|
||||
storage: Option<Arc<dyn ResourceStorage>>,
|
||||
}
|
||||
|
||||
impl ResourceStorageMessageHandler {
|
||||
pub fn new(resource_storage: Box<dyn ResourceStorage>) -> Self {
|
||||
Self {
|
||||
storage: Some(Arc::new(RwLock::new(resource_storage))),
|
||||
}
|
||||
pub fn new(resource_storage: Arc<dyn ResourceStorage>) -> Self {
|
||||
Self { storage: Some(resource_storage) }
|
||||
}
|
||||
|
||||
pub fn resources(&self) -> Box<dyn LoadResource> {
|
||||
@@ -48,7 +45,7 @@ impl Default for ResourceStorageMessageHandler {
|
||||
#[cfg(test)]
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
storage: Some(Arc::new(RwLock::new(Box::new(graph_craft::application_io::resource::HashMapResourceStorage::new())))),
|
||||
storage: Some(Arc::new(graph_craft::application_io::resource::HashMapResourceStorage::new())),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,7 +60,6 @@ impl MessageHandler<ResourceStorageMessage, ResourceStorageMessageContext> for R
|
||||
log::error!("Received resource message but storage is not initialized");
|
||||
return;
|
||||
};
|
||||
let mut storage = storage.write().unwrap();
|
||||
|
||||
match message {
|
||||
ResourceStorageMessage::Store { data } => {
|
||||
|
||||
Reference in New Issue
Block a user