Extremely basic impl of fetch/spawn methods for getting data off the web

This commit is contained in:
David Wendt 2019-11-07 14:34:38 -05:00
parent 2137b9f1fd
commit 00d25a768c
6 changed files with 46 additions and 3 deletions

1
Cargo.lock generated
View File

@ -1640,6 +1640,7 @@ dependencies = [
"svg 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen 0.2.57 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen-futures 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"wasm-bindgen-test 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"web-sys 0.3.34 (registry+https://github.com/rust-lang/crates.io-index)",
]

View File

@ -82,7 +82,7 @@ pub trait AudioBackend {
/// This is only necessary if your particular audio backend needs to know
/// what the stage frame rate is. Otherwise, you are free to avoid
/// implementing it.
fn set_frame_rate(&mut self, frame_rate: f64) {}
fn set_frame_rate(&mut self, _frame_rate: f64) {}
}
/// Rust does not auto-impl a Trait for Box<Trait> or Deref<Target=Trait>

View File

@ -1,8 +1,11 @@
//! Browser-related platform functions
use std::collections::HashMap;
use std::future::Future;
use swf::avm1::types::SendVarsMethod;
pub type Error = Box<dyn std::error::Error>;
/// Enumerates all possible navigation methods.
pub enum NavigationMethod {
/// Indicates that navigation should generate a GET request.
@ -53,6 +56,19 @@ pub trait NavigatorBackend {
window: Option<String>,
vars_method: Option<(NavigationMethod, HashMap<String, String>)>,
);
/// Fetch data at a given URL and return it some time in the future.
fn fetch(&self, url: String) -> Box<dyn Future<Output = Result<Vec<u8>, Error>>>;
/// Arrange for a future to be run at some point in the... well, future.
///
/// This function must be called to ensure a future is actually computed.
/// The future must output an empty value and not hold any stack references
/// which would cause it to become invalidated.
///
/// TODO: For some reason, `wasm_bindgen_futures` wants unpinnable futures.
/// This seems highly limiting.
fn spawn_future(&mut self, future: Box<dyn Future<Output = ()> + Unpin + 'static>);
}
/// A null implementation for platforms that do not live in a web browser.
@ -78,4 +94,10 @@ impl NavigatorBackend for NullNavigatorBackend {
_vars_method: Option<(NavigationMethod, HashMap<String, String>)>,
) {
}
fn fetch(&self, _url: String) -> Box<dyn Future<Output = Result<Vec<u8>, Error>>> {
Box::new(async { Err("Fetch IO not implemented".into()) })
}
fn spawn_future(&mut self, _future: Box<dyn Future<Output = ()> + Unpin + 'static>) {}
}

View File

@ -1,8 +1,9 @@
//! Navigator backend for web
use log;
use ruffle_core::backend::navigator::{NavigationMethod, NavigatorBackend};
use ruffle_core::backend::navigator::{Error, NavigationMethod, NavigatorBackend};
use std::collections::HashMap;
use std::future::Future;
use url::Url;
use webbrowser;
@ -60,4 +61,12 @@ impl NavigatorBackend for ExternalNavigatorBackend {
Err(e) => log::error!("Could not open URL {}: {}", modified_url, e),
};
}
fn fetch(&self, _url: String) -> Box<dyn Future<Output = Result<Vec<u8>, Error>>> {
Box::new(async { Err("Fetch not implemented on desktop!".into()) })
}
fn spawn_future(&mut self, _future: Box<dyn Future<Output = ()> + Unpin + 'static>) {
unimplemented!();
}
}

View File

@ -24,6 +24,7 @@ svg = "0.6.0"
percent-encoding = "2.1.0"
url = "2.1.1"
wasm-bindgen = "0.2.57"
wasm-bindgen-futures = "0.4.4"
[dependencies.jpeg-decoder]
version = "0.1.18"

View File

@ -1,8 +1,10 @@
//! Navigator backend for web
use ruffle_core::backend::navigator::{NavigationMethod, NavigatorBackend};
use ruffle_core::backend::navigator::{Error, NavigationMethod, NavigatorBackend};
use std::collections::HashMap;
use std::future::Future;
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::spawn_local;
use web_sys::window;
pub struct WebNavigatorBackend {}
@ -72,4 +74,12 @@ impl NavigatorBackend for WebNavigatorBackend {
};
}
}
fn fetch(&self, _url: String) -> Box<dyn Future<Output = Result<Vec<u8>, Error>>> {
Box::new(async { Ok(Vec::new()) })
}
fn spawn_future(&mut self, future: Box<dyn Future<Output = ()> + Unpin + 'static>) {
spawn_local(future)
}
}