Skip to content

Commit

Permalink
Merge pull request #576 from Stremio/fix/server-statistics
Browse files Browse the repository at this point in the history
fix: StreamingServer statistics response `null`
  • Loading branch information
elpiel authored Dec 12, 2023
2 parents 261af86 + 08d4195 commit c1ac3ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/models/streaming_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
let selected_effects =
eq_update(&mut self.selected.statistics, Some(request.to_owned()));
let statistics_effects = eq_update(&mut self.statistics, Some(Loadable::Loading));

Effects::one(get_torrent_statistics::<E>(
&self.selected.transport_url,
request,
Expand Down Expand Up @@ -315,7 +316,9 @@ impl<E: Env + 'static> UpdateWithCtx<E> for StreamingServer {
&& self.selected.statistics.as_ref() == Some(request) =>
{
let loadable = match result {
Ok(statistics) => Loadable::Ready(statistics.to_owned()),
Ok(Some(statistics)) => Loadable::Ready(statistics.to_owned()),
// we've loaded the whole stream, no need to update the statistics.
Ok(None) => return Effects::none().unchanged(),
Err(error) => Loadable::Err(error.to_owned()),
};
eq_update(&mut self.statistics, Some(loadable))
Expand Down Expand Up @@ -587,8 +590,12 @@ fn get_torrent_statistics<E: Env + 'static>(url: &Url, request: &StatisticsReque
.header(http::header::CONTENT_TYPE, "application/json")
.body(())
.expect("request builder failed");

// It's happening when the engine is destroyed for inactivity:
// If it was downloaded to 100% and that the stream is paused, then played,
// it will create a new engine and return the correct stats
EffectFuture::Concurrent(
E::fetch::<_, Statistics>(request)
E::fetch::<_, Option<Statistics>>(request)
.map(enclose!((url) move |result|
Msg::Internal(Internal::StreamingServerStatisticsResult((url, statistics_request), result))
))
Expand Down
12 changes: 9 additions & 3 deletions src/runtime/msg/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,16 @@ pub enum Internal {
StreamingServerUpdateSettingsResult(Url, Result<(), EnvError>),
/// Result for creating a torrent.
StreamingServerCreateTorrentResult(String, Result<(), EnvError>),
// Result for playing on device.
/// Result for playing on device.
StreamingServerPlayOnDeviceResult(String, Result<(), EnvError>),
// Result for streaming server statistics.
StreamingServerStatisticsResult((Url, StatisticsRequest), Result<Statistics, EnvError>),
/// Result for streaming server statistics.
///
/// Server will return None (or `null`) in response for [`Statistics`]`,
/// when stream has been fully loaded up to 100%
StreamingServerStatisticsResult(
(Url, StatisticsRequest),
Result<Option<Statistics>, EnvError>,
),
/// Result for fetching resource from addons.
ResourceRequestResult(ResourceRequest, Box<Result<ResourceResponse, EnvError>>),
/// Result for fetching manifest from addon.
Expand Down

0 comments on commit c1ac3ff

Please sign in to comment.