//! Live AttributeMap instance lookup and update/synchronization queue ownership. use crate::{ object_open_map::ObjectOpenMap, object_open_set::{ObjectKey, ObjectOpenSet}, }; #[derive(Clone, Copy, Debug)] pub enum ValueKind { Computed, Base, } #[derive(Debug)] pub enum ModifierValueError { /// Cached instances dereference getModifier's result; suppliers instead /// report their own missing-attribute/modifier error. MissingInstanceModifier, World(E), } pub trait QueryWorld { type Error; fn supplier_has_attribute( &mut self, map: &mut AttributeMap, key: Option, ) -> Result; fn supplier_has_modifier( &mut self, map: &mut AttributeMap, key: Option, id: &str, ) -> Result; fn supplier_value( &mut self, map: &mut AttributeMap, key: Option, kind: ValueKind, ) -> Result; fn supplier_modifier_value( &mut self, map: &mut AttributeMap, key: Option, id: &str, ) -> Result; fn instance_value( &mut self, map: &mut AttributeMap, instance: I, kind: ValueKind, ) -> Result; fn instance_modifier( &mut self, map: &mut AttributeMap, instance: I, id: &str, ) -> Result, Self::Error>; fn set_instance_base( &mut self, map: &mut AttributeMap, instance: I, value: f64, ) -> Result<(), Self::Error>; } /// Instance handles retain identity independently of map membership. In /// particular, a failed creation callback can leave a queued instance that was /// never inserted into `instances`. pub struct AttributeMap { pub instances: ObjectOpenMap, pub updates: ObjectOpenSet, pub sync: ObjectOpenSet, } impl Default for AttributeMap { fn default() -> Self { Self { instances: ObjectOpenMap::default(), updates: ObjectOpenSet::default(), sync: ObjectOpenSet::default(), } } } impl AttributeMap { pub fn has_attribute>( &mut self, key: Option, world: &mut W, ) -> Result { if self.instances.get(key.as_ref()).is_some() { Ok(true) } else { world.supplier_has_attribute(self, key) } } pub fn has_modifier>( &mut self, key: Option, id: &str, world: &mut W, ) -> Result { if let Some(instance) = self.instances.get(key.as_ref()) { world .instance_modifier(self, instance, id) .map(|m| m.is_some()) } else { world.supplier_has_modifier(self, key, id) } } /// Value queries use a cached instance or its supplier prototype. They do /// not create an instance or schedule a dirty update. pub fn value>( &mut self, key: Option, kind: ValueKind, world: &mut W, ) -> Result { if let Some(instance) = self.instances.get(key.as_ref()) { world.instance_value(self, instance, kind) } else { world.supplier_value(self, key, kind) } } pub fn modifier_value>( &mut self, key: Option, id: &str, world: &mut W, ) -> Result> { if let Some(instance) = self.instances.get(key.as_ref()) { world .instance_modifier(self, instance, id) .map_err(ModifierValueError::World)? .ok_or(ModifierValueError::MissingInstanceModifier) } else { world .supplier_modifier_value(self, key, id) .map_err(ModifierValueError::World) } } pub fn reset_base>( &mut self, key: Option, world: &mut W, ) -> Result { if !world.supplier_has_attribute(self, key.clone())? { return Ok(false); } if let Some(instance) = self.instances.get(key.as_ref()) { let value = world.supplier_value(self, key, ValueKind::Base)?; world.set_instance_base(self, instance, value)?; } Ok(true) } /// AttributeMap dispatches to Map.computeIfAbsent(Function), not fastutil's /// Object2ObjectFunction overload. A null mapping is absent for this call; /// the ordinary put re-finds its slot after the supplier callback returns. pub fn get_instance( &mut self, key: Option, create: impl FnOnce(&mut Self, Option) -> Result, E>, ) -> Result, E> { if let Some(instance) = self.instances.get(key.as_ref()) { return Ok(Some(instance)); } let instance = create(self, key.clone())?; if instance.is_some() { self.instances.put(key, instance.clone()); } Ok(instance) } /// The callback performs instance.getAttribute().value().isClientSyncable(). /// It runs after adding the update entry, and can inspect or mutate both /// queues. Failure preserves the update entry and skips the sync insertion. pub fn modified( &mut self, instance: Option, syncable: impl FnOnce(&mut Self, Option) -> Result, ) -> Result<(), E> { self.updates.add(instance.clone()); if syncable(self, instance.clone())? { self.sync.add(instance); } Ok(()) } }