Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #72 from Stremio/feat/ctx-search-history
Browse files Browse the repository at this point in the history
Serialize search history in ctx
  • Loading branch information
tymmesyde authored Nov 20, 2023
2 parents 6e2e196 + fef108a commit 960b457
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/model/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use stremio_core::{
types::{
addon::DescriptorPreview, api::LinkAuthKey, library::LibraryBucket,
notifications::NotificationsBucket, profile::Profile, resource::MetaItemPreview,
streams::StreamsBucket,
search_history::SearchHistoryBucket, streams::StreamsBucket,
},
Model,
};
Expand Down Expand Up @@ -67,6 +67,7 @@ impl WebModel {
library: LibraryBucket,
streams: StreamsBucket,
notifications: NotificationsBucket,
search_history: SearchHistoryBucket,
) -> (WebModel, Effects) {
let (continue_watching_preview, continue_watching_preview_effects) =
ContinueWatchingPreview::new(&library, &notifications);
Expand All @@ -82,7 +83,7 @@ impl WebModel {
let (streaming_server, streaming_server_effects) = StreamingServer::new::<WebEnv>(&profile);
let (local_search, local_search_effects) = LocalSearch::new::<WebEnv>();
let model = WebModel {
ctx: Ctx::new(profile, library, streams, notifications),
ctx: Ctx::new(profile, library, streams, notifications, search_history),
auth_link: Default::default(),
data_export: Default::default(),
local_search,
Expand Down
9 changes: 9 additions & 0 deletions src/model/serialize_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub fn serialize_ctx(ctx: &Ctx) -> JsValue {
mod model {
use std::collections::HashMap;

use itertools::Itertools;
use serde::Serialize;

use chrono::{DateTime, Utc};
Expand All @@ -23,6 +24,7 @@ mod model {
/// keep the original Profile model inside.
pub profile: &'a Profile,
pub notifications: Notifications<'a>,
pub search_history: Vec<&'a str>,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -50,6 +52,13 @@ mod model {
last_updated: ctx.notifications.last_updated,
created: ctx.notifications.created,
},
search_history: ctx
.search_history
.items
.iter()
.sorted_by(|(_, a_date), (_, b_date)| Ord::cmp(b_date, a_date))
.map(|(query, ..)| query.as_str())
.collect_vec(),
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/stremio_core_web.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::RwLock;

use enclose::enclose;
use futures::{future, FutureExt, StreamExt};
use futures::{future, try_join, FutureExt, StreamExt};
use lazy_static::lazy_static;
use tracing::{info, Level};
use tracing_wasm::WASMLayerConfigBuilder;
Expand All @@ -11,13 +11,13 @@ use wasm_bindgen::JsValue;
use stremio_core::{
constants::{
LIBRARY_RECENT_STORAGE_KEY, LIBRARY_STORAGE_KEY, NOTIFICATIONS_STORAGE_KEY,
PROFILE_STORAGE_KEY, STREAMS_STORAGE_KEY,
PROFILE_STORAGE_KEY, SEARCH_HISTORY_STORAGE_KEY, STREAMS_STORAGE_KEY,
},
models::common::Loadable,
runtime::{msg::Action, Env, EnvError, Runtime, RuntimeAction, RuntimeEvent},
types::{
library::LibraryBucket, notifications::NotificationsBucket, profile::Profile,
resource::Stream, streams::StreamsBucket,
resource::Stream, search_history::SearchHistoryBucket, streams::StreamsBucket,
},
};

Expand Down Expand Up @@ -58,21 +58,22 @@ pub async fn initialize_runtime(emit_to_ui: js_sys::Function) -> Result<(), JsVa
let env_init_result = WebEnv::init().await;
match env_init_result {
Ok(_) => {
let storage_result = future::try_join5(
let storage_result = try_join!(
WebEnv::get_storage::<Profile>(PROFILE_STORAGE_KEY),
WebEnv::get_storage::<LibraryBucket>(LIBRARY_RECENT_STORAGE_KEY),
WebEnv::get_storage::<LibraryBucket>(LIBRARY_STORAGE_KEY),
WebEnv::get_storage::<StreamsBucket>(STREAMS_STORAGE_KEY),
WebEnv::get_storage::<NotificationsBucket>(NOTIFICATIONS_STORAGE_KEY),
)
.await;
WebEnv::get_storage::<SearchHistoryBucket>(SEARCH_HISTORY_STORAGE_KEY),
);
match storage_result {
Ok((
profile,
recent_bucket,
other_bucket,
streams_bucket,
notifications_bucket,
search_history_bucket,
)) => {
let profile = profile.unwrap_or_default();
let mut library = LibraryBucket::new(profile.uid(), vec![]);
Expand All @@ -86,8 +87,15 @@ pub async fn initialize_runtime(emit_to_ui: js_sys::Function) -> Result<(), JsVa
streams_bucket.unwrap_or_else(|| StreamsBucket::new(profile.uid()));
let notifications_bucket = notifications_bucket
.unwrap_or(NotificationsBucket::new::<WebEnv>(profile.uid(), vec![]));
let (model, effects) =
WebModel::new(profile, library, streams_bucket, notifications_bucket);
let search_history_bucket =
search_history_bucket.unwrap_or(SearchHistoryBucket::new(profile.uid()));
let (model, effects) = WebModel::new(
profile,
library,
streams_bucket,
notifications_bucket,
search_history_bucket,
);
let (runtime, rx) = Runtime::<WebEnv, _>::new(
model,
effects.into_iter().collect::<Vec<_>>(),
Expand Down

0 comments on commit 960b457

Please sign in to comment.