Skip to content

Commit

Permalink
Hash-based History type & Unified Location. (#177)
Browse files Browse the repository at this point in the history
* Add skeleton code for HashHistory and HashLocation.

* Implement hash history and hash location.

* Add tests.

* Fix clippy.

* Add extra assertion.

* Implement HashHistory with BrowserHistory.

* Common location query.

* Fix tests.

* Unified Location.

* Adjust wording.
  • Loading branch information
futursolo authored Dec 4, 2021
1 parent 2245cd9 commit 816b8f1
Show file tree
Hide file tree
Showing 18 changed files with 702 additions and 309 deletions.
5 changes: 1 addition & 4 deletions crates/console/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ pub mod __macro {
columns: impl IntoIterator<Item = &'a str>,
) {
let data = JsValue::from_serde(&data).unwrap_throw();
let columns = columns
.into_iter()
.map(|it| JsValue::from_str(it))
.collect();
let columns = columns.into_iter().map(JsValue::from_str).collect();

crate::externs::table_with_data_and_columns(data, columns);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/file/src/file_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub mod callbacks {
#[derive(Debug)]
pub struct FileReader {
reader: web_sys::FileReader,
load_listener: EventListener,
error_listener: EventListener,
_load_listener: EventListener,
_error_listener: EventListener,
}

impl std::ops::Drop for FileReader {
Expand Down Expand Up @@ -135,8 +135,8 @@ pub mod callbacks {

FileReader {
reader,
load_listener,
error_listener,
_load_listener: load_listener,
_error_listener: error_listener,
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/history/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ categories = ["api-bindings", "history", "wasm"]
wasm-bindgen = "0.2"
gloo-utils = { version = "0.1.0", path = "../utils" }
gloo-events = { version = "0.1.0", path = "../events" }
serde = { version = "1", optional = true }
serde = "1"
serde-wasm-bindgen = "0.3.1"
serde_urlencoded = { version = "0.7", optional = true }
serde-wasm-bindgen = { version = "0.3.1", optional = true }
thiserror = { version = "1.0", optional = true }

[dependencies.web-sys]
Expand All @@ -25,6 +25,7 @@ features = [
"History",
"Window",
"Location",
"Url",
]

[dev-dependencies]
Expand All @@ -33,6 +34,5 @@ serde = { version = "1", features = ["derive"] }
gloo-timers = { version = "0.2.0", features = ["futures"], path = "../timers" }

[features]
query = ["serde", "thiserror", "serde_urlencoded"]
state = ["serde", "thiserror", "serde-wasm-bindgen"]
default = ["query", "state"]
query = ["thiserror", "serde_urlencoded"]
default = ["query"]
102 changes: 29 additions & 73 deletions crates/history/src/any.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::borrow::Cow;

#[cfg(feature = "serde")]
use serde::de::DeserializeOwned;
#[cfg(feature = "serde")]
#[cfg(feature = "query")]
use serde::Serialize;

use crate::browser::{BrowserHistory, BrowserLocation};
#[cfg(feature = "serde")]
use crate::browser::BrowserHistory;
#[cfg(feature = "query")]
use crate::error::HistoryResult;
use crate::hash::HashHistory;
use crate::history::History;
use crate::listener::HistoryListener;
use crate::location::Location;
Expand All @@ -17,63 +16,56 @@ use crate::location::Location;
pub enum AnyHistory {
/// A Browser History.
Browser(BrowserHistory),
}

/// The [`Location`] for [`AnyHistory`]
#[derive(Clone, PartialEq, Debug)]
pub enum AnyLocation {
/// A Browser Location.
Browser(BrowserLocation),
/// A Hash History
Hash(HashHistory),
}

impl History for AnyHistory {
type Location = AnyLocation;

fn len(&self) -> usize {
match self {
Self::Browser(m) => m.len(),
Self::Hash(m) => m.len(),
}
}

fn go(&self, delta: isize) {
match self {
Self::Browser(m) => m.go(delta),
Self::Hash(m) => m.go(delta),
}
}

fn push<'a>(&self, route: impl Into<Cow<'a, str>>) {
match self {
Self::Browser(m) => m.push(route),
Self::Hash(m) => m.push(route),
}
}

fn replace<'a>(&self, route: impl Into<Cow<'a, str>>) {
match self {
Self::Browser(m) => m.replace(route),
Self::Hash(m) => m.replace(route),
}
}

#[cfg(feature = "state")]
fn push_with_state<'a, T>(&self, route: impl Into<Cow<'a, str>>, state: T) -> HistoryResult<()>
fn push_with_state<'a, T>(&self, route: impl Into<Cow<'a, str>>, state: T)
where
T: Serialize + 'static,
T: 'static,
{
match self {
Self::Browser(m) => m.push_with_state(route, state),
Self::Hash(m) => m.push_with_state(route, state),
}
}

#[cfg(feature = "state")]
fn replace_with_state<'a, T>(
&self,
route: impl Into<Cow<'a, str>>,
state: T,
) -> HistoryResult<()>
fn replace_with_state<'a, T>(&self, route: impl Into<Cow<'a, str>>, state: T)
where
T: Serialize + 'static,
T: 'static,
{
match self {
Self::Browser(m) => m.replace_with_state(route, state),
Self::Hash(m) => m.replace_with_state(route, state),
}
}

Expand All @@ -84,6 +76,7 @@ impl History for AnyHistory {
{
match self {
Self::Browser(m) => m.push_with_query(route, query),
Self::Hash(m) => m.push_with_query(route, query),
}
}
#[cfg(feature = "query")]
Expand All @@ -97,10 +90,11 @@ impl History for AnyHistory {
{
match self {
Self::Browser(m) => m.replace_with_query(route, query),
Self::Hash(m) => m.replace_with_query(route, query),
}
}

#[cfg(all(feature = "query", feature = "state"))]
#[cfg(all(feature = "query"))]
fn push_with_query_and_state<'a, Q, T>(
&self,
route: impl Into<Cow<'a, str>>,
Expand All @@ -113,10 +107,11 @@ impl History for AnyHistory {
{
match self {
Self::Browser(m) => m.push_with_query_and_state(route, query, state),
Self::Hash(m) => m.push_with_query_and_state(route, query, state),
}
}

#[cfg(all(feature = "query", feature = "state"))]
#[cfg(all(feature = "query"))]
fn replace_with_query_and_state<'a, Q, T>(
&self,
route: impl Into<Cow<'a, str>>,
Expand All @@ -129,6 +124,7 @@ impl History for AnyHistory {
{
match self {
Self::Browser(m) => m.replace_with_query_and_state(route, query, state),
Self::Hash(m) => m.replace_with_query_and_state(route, query, state),
}
}

Expand All @@ -138,54 +134,14 @@ impl History for AnyHistory {
{
match self {
Self::Browser(m) => m.listen(callback),
Self::Hash(m) => m.listen(callback),
}
}

fn location(&self) -> Self::Location {
match self {
Self::Browser(m) => AnyLocation::Browser(m.location()),
}
}
}

impl Location for AnyLocation {
type History = AnyHistory;

fn path(&self) -> String {
match self {
Self::Browser(m) => m.path(),
}
}

fn search(&self) -> String {
match self {
Self::Browser(m) => m.search(),
}
}

#[cfg(feature = "query")]
fn query<T>(&self) -> HistoryResult<T>
where
T: DeserializeOwned,
{
match self {
Self::Browser(m) => m.query(),
}
}

fn hash(&self) -> String {
match self {
Self::Browser(m) => m.hash(),
}
}

#[cfg(feature = "state")]
fn state<T>(&self) -> HistoryResult<T>
where
T: DeserializeOwned + 'static,
{
fn location(&self) -> Location {
match self {
Self::Browser(m) => m.state(),
Self::Browser(m) => m.location(),
Self::Hash(m) => m.location(),
}
}
}
Expand All @@ -196,8 +152,8 @@ impl From<BrowserHistory> for AnyHistory {
}
}

impl From<BrowserLocation> for AnyLocation {
fn from(m: BrowserLocation) -> AnyLocation {
AnyLocation::Browser(m)
impl From<HashHistory> for AnyHistory {
fn from(m: HashHistory) -> AnyHistory {
AnyHistory::Hash(m)
}
}
Loading

0 comments on commit 816b8f1

Please sign in to comment.