mirror of
https://github.com/localForage/localForage.git
synced 2026-01-25 14:44:26 +00:00
Tweak supports() PR
This commit is contained in:
parent
af8b460c58
commit
b9d4e04aa1
94
dist/localforage.js
vendored
94
dist/localforage.js
vendored
@ -1998,28 +1998,6 @@ requireModule('promise/polyfill').polyfill();
|
||||
moduleType = MODULE_TYPE_EXPORT;
|
||||
}
|
||||
|
||||
// Initialize IndexedDB; fall back to vendor-prefixed versions if needed.
|
||||
var indexedDB = indexedDB || this.indexedDB || this.webkitIndexedDB ||
|
||||
this.mozIndexedDB || this.OIndexedDB ||
|
||||
this.msIndexedDB;
|
||||
|
||||
var supportsIndexedDB = indexedDB &&
|
||||
typeof indexedDB.open === 'function' &&
|
||||
indexedDB.open('_localforage_spec_test', 1)
|
||||
.onupgradeneeded === null;
|
||||
|
||||
// Check for WebSQL.
|
||||
var openDatabase = this.openDatabase;
|
||||
|
||||
// Check for localStorage.
|
||||
var supportsLocalStorage = (function() {
|
||||
try {
|
||||
return localStorage && typeof localStorage.setItem === 'function';
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
|
||||
// The actual localForage object that we expose as a module or via a
|
||||
// global. It's extended by pulling in one of our other libraries.
|
||||
var _this = this;
|
||||
@ -2078,11 +2056,7 @@ requireModule('promise/polyfill').polyfill();
|
||||
var self = this;
|
||||
|
||||
this._driverSet = new Promise(function(resolve, reject) {
|
||||
if ((!supportsIndexedDB &&
|
||||
driverName === localForage.INDEXEDDB) ||
|
||||
(!openDatabase && driverName === localForage.WEBSQL) ||
|
||||
(!supportsLocalStorage &&
|
||||
driverName === localForage.LOCALSTORAGE)) {
|
||||
if (!self.supports(driverName)) {
|
||||
|
||||
if (errorCallback) {
|
||||
errorCallback();
|
||||
@ -2127,14 +2101,20 @@ requireModule('promise/polyfill').polyfill();
|
||||
self._extend(_this[driverName]);
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
|
||||
this._driverSet.then(callback, errorCallback);
|
||||
|
||||
return this._driverSet;
|
||||
},
|
||||
|
||||
supports: function(driverName) {
|
||||
return !!driverSupport[driverName];
|
||||
},
|
||||
|
||||
ready: function(callback) {
|
||||
var ready = new Promise(function(resolve) {
|
||||
localForage._driverSet.then(function() {
|
||||
@ -2161,21 +2141,57 @@ requireModule('promise/polyfill').polyfill();
|
||||
}
|
||||
};
|
||||
|
||||
// Select our storage library.
|
||||
var storageLibrary;
|
||||
var driverSupport = (function(_this) {
|
||||
// Initialize IndexedDB; fall back to vendor-prefixed versions
|
||||
// if needed.
|
||||
var indexedDB = indexedDB || _this.indexedDB || _this.webkitIndexedDB ||
|
||||
_this.mozIndexedDB || _this.OIndexedDB ||
|
||||
_this.msIndexedDB;
|
||||
|
||||
var result = {};
|
||||
|
||||
result[localForage.WEBSQL] = !!_this.openDatabase;
|
||||
result[localForage.INDEXEDDB] = !!(
|
||||
indexedDB &&
|
||||
typeof indexedDB.open === 'function' &&
|
||||
indexedDB.open('_localforage_spec_test', 1)
|
||||
.onupgradeneeded === null
|
||||
);
|
||||
|
||||
result[localForage.LOCALSTORAGE] = !!(function() {
|
||||
try {
|
||||
return (localStorage &&
|
||||
typeof localStorage.setItem === 'function');
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
|
||||
return result;
|
||||
})(this);
|
||||
|
||||
var driverTestOrder = [
|
||||
localForage.INDEXEDDB,
|
||||
localForage.WEBSQL,
|
||||
localForage.LOCALSTORAGE
|
||||
];
|
||||
|
||||
// Check to see if IndexedDB is available and if it is the latest
|
||||
// implementation; it's our preferred backend library. We use "_spec_test"
|
||||
// as the name of the database because it's not the one we'll operate on,
|
||||
// but it's useful to make sure its using the right spec.
|
||||
// See: https://github.com/mozilla/localForage/issues/128
|
||||
if (supportsIndexedDB) {
|
||||
storageLibrary = localForage.INDEXEDDB;
|
||||
} else if (openDatabase) { // WebSQL is available, so we'll use that.
|
||||
storageLibrary = localForage.WEBSQL;
|
||||
} else if (supportsLocalStorage) { // If nothing else is available,
|
||||
// we try to use localStorage.
|
||||
storageLibrary = localForage.LOCALSTORAGE;
|
||||
}
|
||||
var storageLibrary = (function(driverSupport, driverTestOrder) {
|
||||
for (var i = 0; i < driverTestOrder.length; i++) {
|
||||
var driverToTest = driverTestOrder[i];
|
||||
|
||||
if (driverSupport[driverTestOrder[i]]) {
|
||||
return driverToTest;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
})(driverSupport, driverTestOrder);
|
||||
|
||||
// If window.localForageConfig is set, use it for configuration.
|
||||
if (this.localForageConfig) {
|
||||
|
||||
2
dist/localforage.min.js
vendored
2
dist/localforage.min.js
vendored
File diff suppressed because one or more lines are too long
94
dist/localforage.nopromises.js
vendored
94
dist/localforage.nopromises.js
vendored
@ -1315,28 +1315,6 @@
|
||||
moduleType = MODULE_TYPE_EXPORT;
|
||||
}
|
||||
|
||||
// Initialize IndexedDB; fall back to vendor-prefixed versions if needed.
|
||||
var indexedDB = indexedDB || this.indexedDB || this.webkitIndexedDB ||
|
||||
this.mozIndexedDB || this.OIndexedDB ||
|
||||
this.msIndexedDB;
|
||||
|
||||
var supportsIndexedDB = indexedDB &&
|
||||
typeof indexedDB.open === 'function' &&
|
||||
indexedDB.open('_localforage_spec_test', 1)
|
||||
.onupgradeneeded === null;
|
||||
|
||||
// Check for WebSQL.
|
||||
var openDatabase = this.openDatabase;
|
||||
|
||||
// Check for localStorage.
|
||||
var supportsLocalStorage = (function() {
|
||||
try {
|
||||
return localStorage && typeof localStorage.setItem === 'function';
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
|
||||
// The actual localForage object that we expose as a module or via a
|
||||
// global. It's extended by pulling in one of our other libraries.
|
||||
var _this = this;
|
||||
@ -1395,11 +1373,7 @@
|
||||
var self = this;
|
||||
|
||||
this._driverSet = new Promise(function(resolve, reject) {
|
||||
if ((!supportsIndexedDB &&
|
||||
driverName === localForage.INDEXEDDB) ||
|
||||
(!openDatabase && driverName === localForage.WEBSQL) ||
|
||||
(!supportsLocalStorage &&
|
||||
driverName === localForage.LOCALSTORAGE)) {
|
||||
if (!self.supports(driverName)) {
|
||||
|
||||
if (errorCallback) {
|
||||
errorCallback();
|
||||
@ -1444,14 +1418,20 @@
|
||||
self._extend(_this[driverName]);
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
|
||||
this._driverSet.then(callback, errorCallback);
|
||||
|
||||
return this._driverSet;
|
||||
},
|
||||
|
||||
supports: function(driverName) {
|
||||
return !!driverSupport[driverName];
|
||||
},
|
||||
|
||||
ready: function(callback) {
|
||||
var ready = new Promise(function(resolve) {
|
||||
localForage._driverSet.then(function() {
|
||||
@ -1478,21 +1458,57 @@
|
||||
}
|
||||
};
|
||||
|
||||
// Select our storage library.
|
||||
var storageLibrary;
|
||||
var driverSupport = (function(_this) {
|
||||
// Initialize IndexedDB; fall back to vendor-prefixed versions
|
||||
// if needed.
|
||||
var indexedDB = indexedDB || _this.indexedDB || _this.webkitIndexedDB ||
|
||||
_this.mozIndexedDB || _this.OIndexedDB ||
|
||||
_this.msIndexedDB;
|
||||
|
||||
var result = {};
|
||||
|
||||
result[localForage.WEBSQL] = !!_this.openDatabase;
|
||||
result[localForage.INDEXEDDB] = !!(
|
||||
indexedDB &&
|
||||
typeof indexedDB.open === 'function' &&
|
||||
indexedDB.open('_localforage_spec_test', 1)
|
||||
.onupgradeneeded === null
|
||||
);
|
||||
|
||||
result[localForage.LOCALSTORAGE] = !!(function() {
|
||||
try {
|
||||
return (localStorage &&
|
||||
typeof localStorage.setItem === 'function');
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
|
||||
return result;
|
||||
})(this);
|
||||
|
||||
var driverTestOrder = [
|
||||
localForage.INDEXEDDB,
|
||||
localForage.WEBSQL,
|
||||
localForage.LOCALSTORAGE
|
||||
];
|
||||
|
||||
// Check to see if IndexedDB is available and if it is the latest
|
||||
// implementation; it's our preferred backend library. We use "_spec_test"
|
||||
// as the name of the database because it's not the one we'll operate on,
|
||||
// but it's useful to make sure its using the right spec.
|
||||
// See: https://github.com/mozilla/localForage/issues/128
|
||||
if (supportsIndexedDB) {
|
||||
storageLibrary = localForage.INDEXEDDB;
|
||||
} else if (openDatabase) { // WebSQL is available, so we'll use that.
|
||||
storageLibrary = localForage.WEBSQL;
|
||||
} else if (supportsLocalStorage) { // If nothing else is available,
|
||||
// we try to use localStorage.
|
||||
storageLibrary = localForage.LOCALSTORAGE;
|
||||
}
|
||||
var storageLibrary = (function(driverSupport, driverTestOrder) {
|
||||
for (var i = 0; i < driverTestOrder.length; i++) {
|
||||
var driverToTest = driverTestOrder[i];
|
||||
|
||||
if (driverSupport[driverTestOrder[i]]) {
|
||||
return driverToTest;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
})(driverSupport, driverTestOrder);
|
||||
|
||||
// If window.localForageConfig is set, use it for configuration.
|
||||
if (this.localForageConfig) {
|
||||
|
||||
2
dist/localforage.nopromises.min.js
vendored
2
dist/localforage.nopromises.min.js
vendored
File diff suppressed because one or more lines are too long
2
site/localforage.min.js
vendored
2
site/localforage.min.js
vendored
File diff suppressed because one or more lines are too long
@ -165,8 +165,9 @@
|
||||
}
|
||||
};
|
||||
|
||||
var driverSupport = (function getDriverSupport(_this) {
|
||||
// Initialize IndexedDB; fall back to vendor-prefixed versions if needed.
|
||||
var driverSupport = (function(_this) {
|
||||
// Initialize IndexedDB; fall back to vendor-prefixed versions
|
||||
// if needed.
|
||||
var indexedDB = indexedDB || _this.indexedDB || _this.webkitIndexedDB ||
|
||||
_this.mozIndexedDB || _this.OIndexedDB ||
|
||||
_this.msIndexedDB;
|
||||
@ -174,15 +175,17 @@
|
||||
var result = {};
|
||||
|
||||
result[localForage.WEBSQL] = !!_this.openDatabase;
|
||||
|
||||
result[localForage.INDEXEDDB] = !!(indexedDB &&
|
||||
typeof indexedDB.open === 'function' &&
|
||||
indexedDB.open('_localforage_spec_test', 1)
|
||||
.onupgradeneeded === null);
|
||||
result[localForage.INDEXEDDB] = !!(
|
||||
indexedDB &&
|
||||
typeof indexedDB.open === 'function' &&
|
||||
indexedDB.open('_localforage_spec_test', 1)
|
||||
.onupgradeneeded === null
|
||||
);
|
||||
|
||||
result[localForage.LOCALSTORAGE] = !!(function() {
|
||||
try {
|
||||
return localStorage && typeof localStorage.setItem === 'function';
|
||||
return (localStorage &&
|
||||
typeof localStorage.setItem === 'function');
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
@ -197,22 +200,21 @@
|
||||
localForage.LOCALSTORAGE
|
||||
];
|
||||
|
||||
// Select our storage library.
|
||||
var storageLibrary;
|
||||
// Check to see if IndexedDB is available and if it is the latest
|
||||
// implementation; it's our preferred backend library. We use "_spec_test"
|
||||
// as the name of the database because it's not the one we'll operate on,
|
||||
// but it's useful to make sure its using the right spec.
|
||||
// See: https://github.com/mozilla/localForage/issues/128
|
||||
storageLibrary = (function getPreferedStorageLibrary(driverSupport, driverTestOrder) {
|
||||
var storageLibrary = (function(driverSupport, driverTestOrder) {
|
||||
for (var i = 0; i < driverTestOrder.length; i++) {
|
||||
var driverToTest = driverTestOrder[i];
|
||||
|
||||
if (driverSupport[driverToTest]) {
|
||||
if (driverSupport[driverTestOrder[i]]) {
|
||||
return driverToTest;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
|
||||
return null;
|
||||
})(driverSupport, driverTestOrder);
|
||||
|
||||
// If window.localForageConfig is set, use it for configuration.
|
||||
|
||||
@ -50,7 +50,6 @@ DRIVERS.forEach(function(driverName) {
|
||||
it('has a localStorage API', function() {
|
||||
expect(typeof localforage.getItem).to.be('function');
|
||||
expect(typeof localforage.setItem).to.be('function');
|
||||
expect(typeof localforage.supports).to.be('function');
|
||||
expect(typeof localforage.clear).to.be('function');
|
||||
expect(typeof localforage.length).to.be('function');
|
||||
expect(typeof localforage.removeItem).to.be('function');
|
||||
@ -61,6 +60,7 @@ DRIVERS.forEach(function(driverName) {
|
||||
expect(typeof localforage._initStorage).to.be('function');
|
||||
expect(typeof localforage.config).to.be('function');
|
||||
expect(typeof localforage.driver).to.be('function');
|
||||
expect(typeof localforage.supports).to.be('function');
|
||||
expect(typeof localforage.getItem).to.be('function');
|
||||
expect(typeof localforage.setItem).to.be('function');
|
||||
expect(typeof localforage.clear).to.be('function');
|
||||
@ -71,8 +71,10 @@ DRIVERS.forEach(function(driverName) {
|
||||
expect(typeof localforage.ready).to.be('function');
|
||||
});
|
||||
|
||||
// Make sure we don't support bogus drivers.
|
||||
it('supports ' + driverName + ' database driver', function() {
|
||||
expect(localforage.supports(driverName) === true);
|
||||
expect(localforage.supports('I am not a driver') === false);
|
||||
});
|
||||
|
||||
it('sets the right database driver', function() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user