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

feat(localforage): add support for custom drivers #282

Merged
merged 1 commit into from
Oct 19, 2014

Conversation

thgreasi
Copy link
Member

Resolves #267

This is a simple implementation, not guessing the use of requirejs, browserify or anything else.
The developer uses an async call like localforage.defineDriver(dummyStorageDriver); and passes a "driver definition object" that should look like

{
    _driver: 'customDriverUniqueName',
    _initStorage: function(options) { },
    clear: function(callback) { },
    getItem: function(key, callback) { },
    key: function(n, callback) { },
    keys: function(callback) { },
    length: function(callback) { },
    removeItem: function(key, callback) { },
    setItem: function(key, value, callback) { }
}

It also may contain a _support property that is either boolean (true/false) or it returns a Promise that resolves to a boolean value. If _support is omitted, then true is used.

For a brief how-to take a look at test/dummyStorageDriver.js and test/test.customdriver.js.

PS: The async nature of the defineDriver method may also prove useful for a future implementation that utilizes requirejs/browserify to load the driver, as presented in this comment of #267.
PS2: Also, my naming may be bad.

@thgreasi thgreasi mentioned this pull request Oct 19, 2014
@tofumatt
Copy link
Member

Whoa! This is awesome and unexpected ^_^ 👍

I'll look through now, but I like the idea.

@tofumatt
Copy link
Member

Looking through the code, it looks like each instance of localForage has the defineDriver() method and can load a custom driver object. Does this allow all instances of localForage access to the custom driver, or only the instance that calls defineDriver()?

@thgreasi
Copy link
Member Author

Driver definitions are available for all instances to be used, since the CustomDrivers object (that holds the custom driver definitions) is inside the closure created by the localforage.js IIFE.

@tofumatt
Copy link
Member

Awesome. Okay cool. I think there are a few nitpicks but I'll clean them up and merge this in. Awesome stuff!

var defineDriver = new Promise(function(resolve, reject) {
try {
var driverName = driverObject._driver;
var complianceError = new Error('Custom driver not complian.');
Copy link
Member

Choose a reason for hiding this comment

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

I'll fix this and a few whitespace things before merging, but it's "compliant" 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like, that besides naming I also have typo issues.
This should also be changed in src/localforage.js test.customdriver.js.

@tofumatt tofumatt modified the milestone: 1.1 getItems(); keys() Oct 19, 2014
@tofumatt tofumatt merged commit 0755859 into localForage:master Oct 19, 2014
@tofumatt
Copy link
Member

Totally awesome; thanks so much 👍

I'm drafting up a release of 1.1 to add this in now :-)

@jprichardson
Copy link

Love this, thank you so much!

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.

Write your own driver?
3 participants