reactive_graph_std_array/behaviour/entity/
pop.rs1use reactive_graph_behaviour_model_api::behaviour_validator;
2use reactive_graph_behaviour_model_api::prelude::*;
3use reactive_graph_behaviour_model_impl::entity_behaviour;
4use reactive_graph_graph::prelude::*;
5use reactive_graph_reactive_model_impl::ReactiveEntity;
6use serde_json::Value;
7use uuid::Uuid;
8
9use reactive_graph_std_array_model::ArrayPopProperties::ARRAY;
10use reactive_graph_std_array_model::ArrayPopProperties::VALUE;
11use reactive_graph_std_result_model::ResultArrayProperties::RESULT;
12
13entity_behaviour!(ArrayPop, ArrayPopFactory, ArrayPopFsm, ArrayPopBehaviourTransitions, ArrayPopValidator);
14
15behaviour_validator!(ArrayPopValidator, Uuid, ReactiveEntity, ARRAY.as_ref(), RESULT.as_ref(), VALUE.as_ref());
16
17impl BehaviourInit<Uuid, ReactiveEntity> for ArrayPopBehaviourTransitions {
18 fn init(&self) -> Result<(), BehaviourInitializationFailed> {
19 if let Some(array) = self.reactive_instance.get(ARRAY) {
20 let (result, value) = pop_array(&array);
21 self.reactive_instance.set(RESULT, result);
22 if let Some(value) = value {
23 self.reactive_instance.set(VALUE, value);
24 }
25 }
26 Ok(())
27 }
28}
29
30impl BehaviourConnect<Uuid, ReactiveEntity> for ArrayPopBehaviourTransitions {
31 fn connect(&self) -> Result<(), BehaviourConnectFailed> {
32 let reactive_instance = self.reactive_instance.clone();
33 self.property_observers.observe_with_handle(ARRAY.as_ref(), move |array: &Value| {
34 let (result, value) = pop_array(array);
35 reactive_instance.set(RESULT, result);
36 if let Some(value) = value {
37 reactive_instance.set(VALUE, value);
38 }
39 });
40 Ok(())
41 }
42}
43
44impl BehaviourShutdown<Uuid, ReactiveEntity> for ArrayPopBehaviourTransitions {}
45impl BehaviourTransitions<Uuid, ReactiveEntity> for ArrayPopBehaviourTransitions {}
46
47fn pop_array(array: &Value) -> (Value, Option<Value>) {
48 match array.as_array() {
49 Some(array) => {
50 let mut array = array.clone();
51 let value = array.pop();
52 (Value::Array(array), value)
53 }
54 None => (array.clone(), None),
55 }
56}