Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kvdb] add an implementation for a browser #194

Closed
wants to merge 6 commits into from

Conversation

ordian
Copy link
Member

@ordian ordian commented Aug 3, 2019

This is a simple implementation on top of localStorage, which serializes the whole DB every 2 seconds. Hopefully good enough for a demo.

cc #182, cc @tomaka

@@ -0,0 +1,25 @@
[package]
name = "kvdb-web"
version = "0.0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be at least 0.1.0, so that we can publish patches.

Suggested change
version = "0.0.1"
version = "0.1.0"

@@ -0,0 +1,15 @@
[package]
name = "kvdb_web_demo"
version = "0.0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version = "0.0.1"
version = "0.1.0"

description = "Playground for kvdb in the browser"
license = "GPL-3.0"
edition = "2018"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add this, to prevent accidentally publishing the demo:

Suggested change
publish = false

type Key = BytesHexEncoding;
type Value = BytesHexEncoding;

macro_rules! console_log {
Copy link
Contributor

@tomaka tomaka Aug 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just use log! and warn! from the log crate, and the user can redirect them to the console, for example using https://crates.io/crates/console_log

[dependencies.web-sys]
version = "0.3"
features = [
'console',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove console; see my remarks about logging.

}

fn flush(&self) -> io::Result<()> {
Ok(())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that call do_persist_db?

.expect_throw("localStorage should be supported in your browser")
.unwrap_throw();

let a = Closure::wrap(Box::new(move || do_persist_db(&db.clone(), &local_storage)) as Box<dyn Fn()>);
Copy link
Contributor

@tomaka tomaka Aug 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use a Weak<Database> here, so that if we close the database, the flushing stops as well.

Ideally we would also stop the timer. This can be done by using set_timeout instead of set_interval, and having the callback call set_timeout again, unless the database is dead.
I'm not sure of the difficulty of doing that, and I'd be fine with merging with set_interval.

@ordian
Copy link
Member Author

ordian commented Sep 5, 2019

closing in favor of #202

@ordian ordian closed this Sep 5, 2019
@ordian ordian deleted the ao-kvdb-localstorage branch September 5, 2019 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants