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

Added initStorage to driver's API. Only creates websql database if really needed. #99

Merged
merged 3 commits into from
Mar 12, 2014
Merged

Conversation

nantunes
Copy link
Contributor

@nantunes nantunes commented Mar 9, 2014

WebSQL "localforage" database and respective table were always being created, even when using other driver.

The initStorage() function mechanism allows for similar behavior in other drivers, if needed.

An alternative could be to add a withStore to websql.js, similar to the one in indexeddb.js.
However, I think this is a better solution as it cuts the overhead of checking if db == null in every method call.

LocalStorage driver needs no initialization, so initStorage simply does nothing.

Note: This pull request replaces "Create websql database only if really needed" #79, as I forgot to create a new feature branch.

@tofumatt
Copy link
Member

Neat that this passes all tests already. Should we add some that ensure the databases are only created on demand? How would you ensure that?

@nantunes
Copy link
Contributor Author

There is no way to simply check if a database exists, but we can call openDatabase with a creation callback in the 5th parameter, that is called when the database is being created. The problem is nothing is called when the database already exists. So casper will wait for the callback in one case and wait for a timeout in another? I'm not seeing it clear now, but I'll think bit more.

Meanwhile I've committed the tests that checks for the new initStorage method.

// Open the database; the openDatabase API will automatically create it for
// us if it doesn't exist.
var db = window.openDatabase(DB_NAME, DB_VERSION, STORE_NAME, DB_SIZE);
function initStorage(callback) {
Copy link
Member

Choose a reason for hiding this comment

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

Should this be exposed like it's part of the public API? Does any developer have cause to call this on their own, or is it localForage-only? If so, maybe we can advertise its private-ness with a leading underscore or something.

@tofumatt
Copy link
Member

Largely this looks really good. Can you check out my few comments? After that I think it's good to merge in. Nice work.

@nantunes
Copy link
Contributor Author

Done.

As for the tests, is hard to do a negative test case. We would be testing what not happens when using other driver (websql db doesn't exists if indexdb is used, indexdb store doesn't exists if localstorage is used, etc.).
In fact, to test the effects of not calling something. I think is not worth it.

The positive test case is already covered, as test.api would fail if the correct db or store isn't created.

@tofumatt
Copy link
Member

Ah, you rule! Thanks so much!

That makes sense, thanks for explaining. 👍

tofumatt pushed a commit that referenced this pull request Mar 12, 2014
Added initStorage to driver's API. Only creates websql database if really needed.
@tofumatt tofumatt merged commit 452d9ac into localForage:master Mar 12, 2014
@nantunes nantunes deleted the init_storage branch March 12, 2014 16:48
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.

None yet

2 participants