Skip to content

Commit

Permalink
Store apis as Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Oct 16, 2024
1 parent c3d3f67 commit 32018de
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
15 changes: 7 additions & 8 deletions app/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::stdout, marker::PhantomData, time::Duration};
use std::{io::stdout, marker::PhantomData, sync::Arc, time::Duration};

use futures::executor::block_on;
use ratatui::{
Expand Down Expand Up @@ -104,19 +104,18 @@ impl App {
}
};

let mut api_registry = Some(api_registry);
let api_registry = Arc::new(api_registry);

loop {
let screen = self
.screens
.last()
.expect("At least one screen should be present");

let screen = Screen::new(*screen, state.take().unwrap(), api_registry.take().unwrap());
let screen = Screen::new(*screen, state.take().unwrap(), api_registry.clone());

let (new_state, new_api_registry, msg) = Self::screen_loop(screen, &mut terminal);
let (new_state, msg) = Self::screen_loop(screen, &mut terminal);
state = Some(new_state);
api_registry = Some(new_api_registry);

match msg {
OutgoingMessage::Exit => {
Expand All @@ -137,7 +136,7 @@ impl App {
fn screen_loop<B: Backend, L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT>(
mut screen: Screen<L, C, M>,
terminal: &mut Terminal<B>,
) -> (StateRegistry, ApiRegistry<L, C, M>, OutgoingMessage) {
) -> (StateRegistry, OutgoingMessage) {
let resources = Resources::default();

loop {
Expand All @@ -152,8 +151,8 @@ impl App {
let msg = screen.tick(event);

if let Some(msg) = msg {
let (state, api_registry) = screen.deconstruct();
return (state, api_registry, msg);
let state = screen.deconstruct();
return (state, msg);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions app/src/screen/asset/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use futures::executor::block_on;
use ratatui::{crossterm::event::Event, Frame};
use rust_decimal::Decimal;
Expand Down Expand Up @@ -26,7 +28,7 @@ pub struct Model<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> {
show_navigation_help: bool,

state: StateRegistry,
apis: ApiRegistry<L, C, M>,
apis: Arc<ApiRegistry<L, C, M>>,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
Expand Down Expand Up @@ -94,7 +96,7 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> Model<L, C, M
impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> ScreenT<L, C, M>
for Model<L, C, M>
{
fn construct(state: StateRegistry, api_registry: ApiRegistry<L, C, M>) -> Self {
fn construct(state: StateRegistry, api_registry: Arc<ApiRegistry<L, C, M>>) -> Self {
Self {
coin_price_history: Default::default(),
transactions: Default::default(),
Expand All @@ -116,7 +118,7 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> ScreenT<L, C,
controller::process_input(event.as_ref()?, self)
}

fn deconstruct(self) -> (StateRegistry, ApiRegistry<L, C, M>) {
(self.state, self.apis)
fn deconstruct(self) -> StateRegistry {
self.state
}
}
12 changes: 6 additions & 6 deletions app/src/screen/deposit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::Instant;
use std::{marker::PhantomData, sync::Arc, time::Instant};

use ratatui::{crossterm::event::Event, Frame};

Expand All @@ -19,19 +19,19 @@ pub struct Model<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> {
show_navigation_help: bool,

state: StateRegistry,
apis: ApiRegistry<L, C, M>,
_phantom: PhantomData<(L, C, M)>,
}

impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> ScreenT<L, C, M>
for Model<L, C, M>
{
fn construct(state: StateRegistry, api_registry: ApiRegistry<L, C, M>) -> Self {
fn construct(state: StateRegistry, _api_registry: Arc<ApiRegistry<L, C, M>>) -> Self {
Self {
last_address_copy: None,
show_navigation_help: false,

state,
apis: api_registry,
_phantom: PhantomData,
}
}

Expand All @@ -43,7 +43,7 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> ScreenT<L, C,
controller::process_input(event.as_ref()?, self)
}

fn deconstruct(self) -> (StateRegistry, ApiRegistry<L, C, M>) {
(self.state, self.apis)
fn deconstruct(self) -> StateRegistry {
self.state
}
}
13 changes: 8 additions & 5 deletions app/src/screen/device_selection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::time::{Duration, Instant};
use std::{
sync::Arc,
time::{Duration, Instant},
};

use futures::executor::block_on;
use ratatui::{crossterm::event::Event, Frame};
Expand All @@ -25,7 +28,7 @@ pub struct Model<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> {
show_navigation_help: bool,

state: StateRegistry,
apis: ApiRegistry<L, C, M>,
apis: Arc<ApiRegistry<L, C, M>>,
}

impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> Model<L, C, M> {
Expand Down Expand Up @@ -61,7 +64,7 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> Model<L, C, M
impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> ScreenT<L, C, M>
for Model<L, C, M>
{
fn construct(state: StateRegistry, api_registry: ApiRegistry<L, C, M>) -> Self {
fn construct(state: StateRegistry, api_registry: Arc<ApiRegistry<L, C, M>>) -> Self {
Self {
devices: vec![],
previous_poll: Instant::now() - DEVICE_POLL_PERIOD,
Expand All @@ -83,7 +86,7 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> ScreenT<L, C,
controller::process_input(event.as_ref()?, self)
}

fn deconstruct(self) -> (StateRegistry, ApiRegistry<L, C, M>) {
(self.state, self.apis)
fn deconstruct(self) -> StateRegistry {
self.state
}
}
10 changes: 6 additions & 4 deletions app/src/screen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use ratatui::{crossterm::event::Event, Frame};
use resources::Resources;

Expand Down Expand Up @@ -27,7 +29,7 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> Screen<L, C,
pub fn new(
name: ScreenName,
state_registry: StateRegistry,
api_registry: ApiRegistry<L, C, M>,
api_registry: Arc<ApiRegistry<L, C, M>>,
) -> Self {
match name {
ScreenName::Asset => Self::Asset(asset::Model::construct(state_registry, api_registry)),
Expand Down Expand Up @@ -61,7 +63,7 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> Screen<L, C,
}
}

pub fn deconstruct(self) -> (StateRegistry, ApiRegistry<L, C, M>) {
pub fn deconstruct(self) -> StateRegistry {
match self {
Self::Asset(screen) => screen.deconstruct(),
Self::Deposit(screen) => screen.deconstruct(),
Expand All @@ -72,12 +74,12 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> Screen<L, C,
}

trait ScreenT<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> {
fn construct(state: StateRegistry, api_registry: ApiRegistry<L, C, M>) -> Self;
fn construct(state: StateRegistry, api_registry: Arc<ApiRegistry<L, C, M>>) -> Self;

fn render(&self, frame: &mut Frame<'_>, resources: &Resources);
fn tick(&mut self, event: Option<Event>) -> Option<OutgoingMessage>;

fn deconstruct(self) -> (StateRegistry, ApiRegistry<L, C, M>);
fn deconstruct(self) -> StateRegistry;
}

pub enum OutgoingMessage {
Expand Down
8 changes: 4 additions & 4 deletions app/src/screen/portfolio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> Model<L, C, M
impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> ScreenT<L, C, M>
for Model<L, C, M>
{
fn construct(state: StateRegistry, api_registry: ApiRegistry<L, C, M>) -> Self {
fn construct(state: StateRegistry, api_registry: Arc<ApiRegistry<L, C, M>>) -> Self {
Self {
selected_account: None,
coin_prices: Default::default(),
balances: Default::default(),
show_navigation_help: false,

state,
apis: Arc::from(api_registry),
apis: api_registry,
}
}

Expand All @@ -128,7 +128,7 @@ impl<L: LedgerApiT, C: CoinPriceApiT, M: BlockchainMonitoringApiT> ScreenT<L, C,
controller::process_input(event.as_ref()?, self)
}

fn deconstruct(self) -> (StateRegistry, ApiRegistry<L, C, M>) {
(self.state, todo!())
fn deconstruct(self) -> StateRegistry {
self.state
}
}

0 comments on commit 32018de

Please sign in to comment.