-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #585 from Stremio/feat/implement-ctx-events
feat: implement events for ctx
- Loading branch information
Showing
12 changed files
with
253 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
mod update_events; | ||
use update_events::*; | ||
|
||
mod update_library; | ||
use update_library::*; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use chrono::DateTime; | ||
use futures::{future, FutureExt, TryFutureExt}; | ||
|
||
use crate::models::common::{eq_update, Loadable}; | ||
use crate::models::ctx::CtxError; | ||
use crate::runtime::msg::{Action, ActionCtx, Internal, Msg}; | ||
use crate::runtime::{Effect, EffectFuture, Effects, Env, EnvFutureExt}; | ||
use crate::types::api::{ | ||
fetch_api, APIRequest, APIResult, GetModalResponse, GetNotificationResponse, | ||
}; | ||
use crate::types::events::Events; | ||
|
||
pub fn update_events<E: Env + 'static>(events: &mut Events, msg: &Msg) -> Effects { | ||
match msg { | ||
Msg::Action(Action::Ctx(ActionCtx::GetEvents)) => { | ||
let modal_effects = eq_update(&mut events.modal, Loadable::Loading); | ||
let notification_effects = eq_update(&mut events.notification, Loadable::Loading); | ||
let requests_effects = Effects::many(vec![get_modal::<E>(), get_notification::<E>()]); | ||
|
||
modal_effects | ||
.join(notification_effects) | ||
.join(requests_effects) | ||
} | ||
Msg::Internal(Internal::GetModalResult(_, result)) => match result { | ||
Ok(response) => eq_update(&mut events.modal, Loadable::Ready(response.to_owned())), | ||
Err(error) => eq_update(&mut events.modal, Loadable::Err(error.to_owned())), | ||
}, | ||
Msg::Internal(Internal::GetNotificationResult(_, result)) => match result { | ||
Ok(response) => eq_update( | ||
&mut events.notification, | ||
Loadable::Ready(response.to_owned()), | ||
), | ||
Err(error) => eq_update(&mut events.notification, Loadable::Err(error.to_owned())), | ||
}, | ||
_ => Effects::none().unchanged(), | ||
} | ||
} | ||
|
||
fn get_modal<E: Env + 'static>() -> Effect { | ||
let request = APIRequest::GetModal { | ||
date: DateTime::from(E::now()), | ||
}; | ||
|
||
EffectFuture::Concurrent( | ||
fetch_api::<E, _, _, Option<GetModalResponse>>(&request) | ||
.map_err(CtxError::from) | ||
.and_then(|result| match result { | ||
APIResult::Ok { result } => future::ok(result), | ||
APIResult::Err { error } => future::err(CtxError::from(error)), | ||
}) | ||
.map(move |result| Msg::Internal(Internal::GetModalResult(request, result))) | ||
.boxed_env(), | ||
) | ||
.into() | ||
} | ||
|
||
fn get_notification<E: Env + 'static>() -> Effect { | ||
let request = APIRequest::GetNotification { | ||
date: DateTime::from(E::now()), | ||
}; | ||
|
||
EffectFuture::Concurrent( | ||
fetch_api::<E, _, _, Option<GetNotificationResponse>>(&request) | ||
.map_err(CtxError::from) | ||
.and_then(|result| match result { | ||
APIResult::Ok { result } => future::ok(result), | ||
APIResult::Err { error } => future::err(CtxError::from(error)), | ||
}) | ||
.map(move |result| Msg::Internal(Internal::GetNotificationResult(request, result))) | ||
.boxed_env(), | ||
) | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use serde::Serialize; | ||
|
||
use crate::{ | ||
models::{common::Loadable, ctx::CtxError}, | ||
types::api::{GetModalResponse, GetNotificationResponse}, | ||
}; | ||
|
||
#[derive(Default, PartialEq, Eq, Serialize, Clone, Debug)] | ||
pub struct Events { | ||
pub modal: Loadable<Option<GetModalResponse>, CtxError>, | ||
pub notification: Loadable<Option<GetNotificationResponse>, CtxError>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod events; | ||
pub use events::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod addon; | ||
pub mod api; | ||
pub mod events; | ||
pub mod library; | ||
pub mod notifications; | ||
pub mod profile; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use std::any::Any; | ||
|
||
use futures::future; | ||
use stremio_derive::Model; | ||
|
||
use crate::{ | ||
models::ctx::Ctx, | ||
runtime::{ | ||
msg::{Action, ActionCtx}, | ||
EnvFutureExt, Runtime, RuntimeAction, TryEnvFuture, | ||
}, | ||
types::api::{APIResult, GetModalResponse, GetNotificationResponse}, | ||
unit_tests::{default_fetch_handler, Request, TestEnv, FETCH_HANDLER}, | ||
}; | ||
|
||
#[test] | ||
fn test_events() { | ||
#[derive(Model, Clone, Debug)] | ||
#[model(TestEnv)] | ||
struct TestModel { | ||
ctx: Ctx, | ||
} | ||
|
||
fn fetch_handler(request: Request) -> TryEnvFuture<Box<dyn Any + Send>> { | ||
match request { | ||
Request { url, .. } if url == "https://api.strem.io/api/getModal" => { | ||
future::ok(Box::new(APIResult::Ok { | ||
result: Some(GetModalResponse { | ||
id: "id".to_owned(), | ||
title: "title".to_owned(), | ||
message: "message".to_owned(), | ||
image_url: "https://image_url".parse().unwrap(), | ||
addon: None, | ||
external_url: None, | ||
}), | ||
}) as Box<dyn Any + Send>) | ||
.boxed_env() | ||
} | ||
Request { url, .. } if url == "https://api.strem.io/api/getNotification" => { | ||
future::ok(Box::new(APIResult::Ok { | ||
result: Some(GetNotificationResponse { | ||
id: "id".to_owned(), | ||
title: "title".to_owned(), | ||
message: "message".to_owned(), | ||
external_url: None, | ||
}), | ||
}) as Box<dyn Any + Send>) | ||
.boxed_env() | ||
} | ||
_ => default_fetch_handler(request), | ||
} | ||
} | ||
|
||
let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv"); | ||
|
||
*FETCH_HANDLER.write().unwrap() = Box::new(fetch_handler); | ||
|
||
let (runtime, _rx) = Runtime::<TestEnv, _>::new( | ||
TestModel { | ||
ctx: Ctx::default(), | ||
}, | ||
vec![], | ||
1000, | ||
); | ||
|
||
assert!( | ||
runtime.model().unwrap().ctx.events.modal.is_loading(), | ||
"Modal should be loading" | ||
); | ||
|
||
assert!( | ||
runtime | ||
.model() | ||
.unwrap() | ||
.ctx | ||
.events | ||
.notification | ||
.is_loading(), | ||
"Notification should be loading" | ||
); | ||
|
||
TestEnv::run(|| { | ||
runtime.dispatch(RuntimeAction { | ||
field: None, | ||
action: Action::Ctx(ActionCtx::GetEvents), | ||
}) | ||
}); | ||
|
||
assert!( | ||
runtime.model().unwrap().ctx.events.modal.is_ready(), | ||
"Modal should be ready" | ||
); | ||
|
||
assert!( | ||
runtime.model().unwrap().ctx.events.notification.is_ready(), | ||
"Notification should be ready" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod add_to_library; | ||
mod authenticate; | ||
mod events; | ||
mod install_addon; | ||
mod logout; | ||
mod notifications { | ||
|