1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use crate::{
  Actions, AstroRunPlugin, Job, JobRunResult, PluginManager, Workflow, WorkflowRunResult,
  WorkflowStateEvent,
};
use parking_lot::Mutex;
use std::sync::Arc;

#[derive(Clone)]
struct SharedState {
  plugins: PluginManager,
  actions: Actions,
}

impl SharedState {
  pub fn new() -> Self {
    SharedState {
      plugins: PluginManager::new(),
      actions: Actions::new(),
    }
  }
}

#[derive(Clone)]
pub struct AstroRunSharedState(Arc<Mutex<SharedState>>);

impl AstroRunSharedState {
  pub fn new() -> Self {
    AstroRunSharedState(Arc::new(Mutex::new(SharedState::new())))
  }

  pub fn register_plugin(&self, plugin: AstroRunPlugin) {
    self.0.lock().plugins.register(plugin);
  }

  pub fn unregister_plugin(&self, plugin_name: &'static str) {
    self.0.lock().plugins.unregister(plugin_name);
  }

  pub fn plugins(&self) -> PluginManager {
    self.0.lock().plugins.clone()
  }

  pub fn register_action<T>(&self, name: impl Into<String>, action: T)
  where
    T: crate::actions::Action + 'static,
  {
    self.0.lock().actions.register(name, action);
  }

  pub fn unregister_action(&self, name: &str) {
    self.0.lock().actions.unregister(name);
  }

  pub fn actions(&self) -> Actions {
    self.0.lock().actions.clone()
  }

  pub fn on_state_change(&self, event: WorkflowStateEvent) {
    self.0.lock().plugins.on_state_change(event);
  }

  pub fn on_run_workflow(&self, workflow: Workflow) {
    self.0.lock().plugins.on_run_workflow(workflow);
  }

  pub fn on_run_job(&self, job: Job) {
    self.0.lock().plugins.on_run_job(job);
  }

  pub fn on_workflow_completed(&self, result: WorkflowRunResult) {
    self.0.lock().plugins.on_workflow_completed(result);
  }

  pub fn on_job_completed(&self, result: JobRunResult) {
    self.0.lock().plugins.on_job_completed(result);
  }
}