dispatcher fixed

This commit is contained in:
Giulio De Pasquale 2021-01-04 12:28:40 +00:00
parent 168f324d6b
commit f56a3f84f8
3 changed files with 24 additions and 20 deletions

View File

@ -4,8 +4,8 @@ use std::future::Future;
use bitfinex::positions::Position; use bitfinex::positions::Position;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use crate::BoxError;
use crate::pairs::PairStatus; use crate::pairs::PairStatus;
use crate::BoxError;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum SignalKind { pub enum SignalKind {
@ -66,7 +66,7 @@ impl Event {
} }
pub struct EventDispatcher { pub struct EventDispatcher {
event_handlers: HashMap<EventKind, Vec<Box<dyn FnMut(Event, &PairStatus) -> JoinHandle<()>>>>, event_handlers: HashMap<EventKind, Vec<Box<dyn Fn(&Event, &PairStatus) -> JoinHandle<()>>>>,
} }
impl EventDispatcher { impl EventDispatcher {
@ -76,21 +76,22 @@ impl EventDispatcher {
} }
} }
pub fn call_handler(&mut self, event: &Event, status: &PairStatus) { pub fn call_event_handlers(&self, event: &Event, status: &PairStatus) {
if let Some(callbacks) = self.event_handlers.get_mut(&event.kind()) { if let Some(functions) = self.event_handlers.get(&event.kind()) {
for f in callbacks { for f in functions {
f(event.clone(), status); f(event, status);
} }
} }
} }
pub fn register_event_handler<F: 'static, Fut: 'static>(&mut self, event: EventKind, mut f: F) pub fn register_event_handler<F: 'static, Fut: 'static>(&mut self, event: EventKind, f: F)
where where
F: FnMut(Event, &PairStatus) -> Fut, F: Fn(&Event, &PairStatus) -> Fut,
Fut: Future<Output=()> + Send, { Fut: Future<Output = ()> + Send,
{
self.event_handlers self.event_handlers
.entry(event) .entry(event)
.or_default() .or_default()
.push(Box::new(move |e, s| tokio::spawn(f(e, s)))); .push(Box::new(move |e, s| tokio::spawn(f(&e, s))));
} }
} }

View File

@ -2,10 +2,10 @@ use std::collections::HashMap;
use bitfinex::positions::Position; use bitfinex::positions::Position;
use crate::currency::SymbolPair;
use crate::events::{Event, EventDispatcher}; use crate::events::{Event, EventDispatcher};
use crate::positions::PositionWrapper; use crate::positions::PositionWrapper;
use crate::strategy::Strategy; use crate::strategy::Strategy;
use crate::currency::SymbolPair;
pub struct PairStatus { pub struct PairStatus {
pair: SymbolPair, pair: SymbolPair,
@ -34,17 +34,18 @@ impl PairStatus {
pub fn add_position(&mut self, position: Position) { pub fn add_position(&mut self, position: Position) {
let (pw, events) = { let (pw, events) = {
match &self.strategy { match &self.strategy {
Some(strategy) => { Some(strategy) => strategy.position_on_new_tick(&position, &self),
strategy.position_on_new_tick(position) None => (
}, PositionWrapper::new(position.clone(), position.pl(), position.pl_perc(), None),
None => (PositionWrapper::new(position.clone(), position.pl(), position.pl_perc(), None), vec![]) vec![],
),
} }
}; };
self.positions self.positions
.entry(self.current_tick) .entry(self.current_tick)
.or_default() .or_default()
.push(pw.clone()); .push(pw);
// self.dispatcher.call_state_handlers(pw); // self.dispatcher.call_state_handlers(pw);
@ -52,9 +53,10 @@ impl PairStatus {
self.add_event(e); self.add_event(e);
} }
} }
pub fn add_event(&mut self, event: Event) { pub fn add_event(&mut self, event: Event) {
self.events.push(event); self.events.push(event);
// self.dispatcher.call_handler(&event); self.dispatcher.call_event_handlers(&event, &self);
} }
} }

View File

@ -2,7 +2,8 @@ use bitfinex::positions::Position;
use crate::events::Event; use crate::events::Event;
use crate::positions::PositionWrapper; use crate::positions::PositionWrapper;
use crate::pairs::PairStatus;
pub trait Strategy { pub trait Strategy {
fn position_on_new_tick(&self, position: Position) -> (PositionWrapper, Vec<Event>); fn position_on_new_tick(&self, position: &Position, status: &PairStatus) -> (PositionWrapper, Vec<Event>);
} }