updated event architecture

This commit is contained in:
michael-bailey 2022-04-18 00:14:39 +01:00
parent 8058c672e8
commit 05851f0aae
6 changed files with 55 additions and 26 deletions

View File

@ -1,5 +1,6 @@
use crate::event::Event;
pub(crate) trait IResponder {
fn accepts_event<'a, 'b>(&'a self, event: Event<'b>) -> bool;
fn accepts_event(&self, event: Event) -> bool;
fn on_event(&self, event: Event);
}

View File

@ -1,37 +1,49 @@
use crate::event::EventResult;
use std::collections::HashMap;
pub enum EventType<'str> {
use futures::channel::oneshot::{channel, Receiver, Sender};
pub enum EventType {
NewConnection,
Custom(&'str str)
Custom(String),
}
pub struct Event<'str> {
Type: EventType<'str>,
args: HashMap<&'str str, String>
pub struct Event {
Type: EventType,
args: HashMap<String, String>,
sender: Sender<EventResult>,
receiver: Option<Receiver<EventResult>>,
}
pub struct Builder<'str> {
Type: EventType<'str>,
args: HashMap<&'str str, String>
pub struct EventBuilder {
Type: EventType,
args: HashMap<String, String>,
sender: Sender<EventResult>,
receiver: Option<Receiver<EventResult>>,
}
impl<'str> Builder<'str> {
pub(super) fn new(Type: EventType<'str>) -> Builder {
Builder {
impl EventBuilder {
pub(super) fn new(Type: EventType) -> EventBuilder {
let (sender, receiver) = channel();
EventBuilder {
Type,
args: HashMap::new()
args: HashMap::new(),
sender,
receiver: Some(receiver),
}
}
pub fn add_arg<T: Into<String>>(mut self, key: &'str str, value: T) -> Self {
self.args.insert(key, value.into());
pub fn add_arg<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
self.args.insert(key.into(), value.into());
self
}
pub(crate) fn build(self) -> Event<'str> {
pub(crate) fn build(self) -> Event {
Event {
Type: self.Type,
args: self.args
args: self.args,
sender: self.sender,
receiver: self.receiver,
}
}
}

View File

@ -0,0 +1,13 @@
use std::collections::HashMap;
pub enum EventResultType {
Success,
InvalidArgs,
InvalidCode,
Other(String),
}
pub struct EventResult {
code: EventResultType,
args: HashMap<String, String>,
}

View File

@ -1,6 +1,8 @@
#[path = "IResponder.rs"]
mod IResponderMod;
mod event;
mod event_result;
pub(crate) use self::{IResponderMod::IResponder};
pub use event::{Builder, EventType, Event};
pub(crate) use self::IResponderMod::IResponder;
pub use event::{Event, EventBuilder, EventType};
pub use event_result::{EventResult, EventResultType};

View File

@ -1,8 +1,11 @@
use crate::event::Event;
use crate::event::EventResult;
use crate::plugin::plugin_interface::IPluginInterface;
use crate::plugin::PluginInterface;
use serde::{Deserialize, Serialize};
use futures::channel::oneshot::Receiver;
use crate::plugin::plugin::Plugin;
use crate::plugin::plugin_entry::PluginExecutionState::{Paused, Running, Stopped};
use std::sync::Arc;
@ -128,10 +131,7 @@ impl PluginEntry {
}
impl IPluginInterface for PluginEntry {
fn get_next_event(&self) -> std::option::Option<Event<'_>> {
todo!()
}
fn get_event_count(&self) -> usize {
fn send_event(&self, _event: Event) -> Receiver<EventResult> {
todo!()
}
}

View File

@ -1,13 +1,14 @@
use crate::event::Event;
use crate::event::EventResult;
use std::fmt::Debug;
use std::sync::Arc;
use std::sync::Weak;
use futures::channel::oneshot::Receiver;
pub type WeakPluginInterface = Weak<dyn IPluginInterface>;
pub(crate) type PluginInterface = Arc<dyn IPluginInterface>;
#[async_trait::async_trait]
pub trait IPluginInterface: Send + Sync + Debug {
fn get_next_event(&self) -> Option<Event>;
fn get_event_count(&self) -> usize;
fn send_event(&self, event: Event) -> Receiver<EventResult>;
}