Skip to content

Commit

Permalink
chore: use rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
thgreasi committed May 14, 2016
1 parent a8d7a3c commit db0364b
Show file tree
Hide file tree
Showing 26 changed files with 694 additions and 2,236 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015-rollup" ]
}
25 changes: 25 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"modules": true
}
},
"extends": "eslint:recommended",
"env": {
"node": true,
"mocha": true
},
"rules":{
"no-console": 0
},
"globals": {
"localforage": true,
"Promise": true,
"console": true,
"self": true,
"System": true
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
bower_components
node_modules
components
4 changes: 3 additions & 1 deletion .jscsrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"esnext": true,
"disallowSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true
},
Expand All @@ -25,5 +26,6 @@
"validateQuoteMarks": {
"escape": true,
"mark": "'"
}
},
"validateIndentation": 4
}
8 changes: 6 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"curly": true,
"eqeqeq": true,
"eqnull": true,
"esnext": false,
"esversion": 6,
"immed": true,
"latedef": true,
"newcap": true,
Expand All @@ -20,9 +20,13 @@
"validthis": true,

"globals": {
"console": true,
"define": true,
"localforage": true,
"module": true,
"require": true
"Promise": true,
"require": true,
"self": true,
"System": true
}
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "4.0"
- "5.0"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Adds getItems method to [localForage](https://github.com/mozilla/localForage).

## Requirements

* [localForage](https://github.com/mozilla/localForage) v1.2.1+
* [localForage](https://github.com/mozilla/localForage) v1.4.0+
* for earlier versions of localforage, please use the v1.1.x releases

## Installation
`npm i localforage-getitems`
Expand Down
13 changes: 3 additions & 10 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "localforage-getitems",
"version": "1.1.0",
"main": [
"src/localforage-getitems.js"
"dist/localforage-getitems.js"
],
"ignore": [
".travis.yml",
Expand All @@ -12,20 +12,13 @@
"Gemfile.lock",
"Rakefile",
"LICENSE",
"build*",
"docs*",
"examples*",
"test*",
"site*"
],
"dependencies": {
"localforage": ">=1.2.1"
},
"devDependencies": {
"es6-promise": "~1.0.0",
"requirejs": "~2.1.10",
"mocha": "~1.18.2",
"expect": "~0.3.1",
"assert": "~0.1.0",
"modernizr": "~2.8.1"
"localforage": ">=1.4.0"
}
}
11 changes: 0 additions & 11 deletions component.json

This file was deleted.

216 changes: 216 additions & 0 deletions dist/localforage-getitems.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import localforage from 'localforage';

function getSerializerPromise(localForageInstance) {
if (getSerializerPromise.result) {
return getSerializerPromise.result;
}
if (!localForageInstance || typeof localForageInstance.getSerializer !== 'function') {
Promise.reject(new Error('localforage.getSerializer() was not available! ' + 'localforage v1.4+ is required!'));
}
getSerializerPromise.result = localForageInstance.getSerializer();
return getSerializerPromise.result;
}

function executeCallback(promise, callback) {
if (callback) {
promise.then(function (result) {
callback(null, result);
}, function (error) {
callback(error);
});
}
}

function getItemKeyValue(key, callback) {
var localforageInstance = this;
var promise = localforageInstance.getItem(key).then(function (value) {
return {
key: key,
value: value
};
});
executeCallback(promise, callback);
return promise;
}

function getIDBKeyRange() {
/* global IDBKeyRange, webkitIDBKeyRange, mozIDBKeyRange */
if (typeof IDBKeyRange !== 'undefined') {
return IDBKeyRange;
}
if (typeof webkitIDBKeyRange !== 'undefined') {
return webkitIDBKeyRange;
}
if (typeof mozIDBKeyRange !== 'undefined') {
return mozIDBKeyRange;
}
}

var idbKeyRange = getIDBKeyRange();

function getItemsIndexedDB(keys, callback) {
var localforageInstance = this;
function comparer(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}

var promise = new Promise(function (resolve, reject) {
localforageInstance.ready().then(function () {
// Thanks https://hacks.mozilla.org/2014/06/breaking-the-borders-of-indexeddb/
var dbInfo = localforageInstance._dbInfo;
var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);

var set = keys.sort(comparer);

var keyRangeValue = idbKeyRange.bound(keys[0], keys[keys.length - 1], false, false);
var req = store.openCursor(keyRangeValue);
var result = {};
var i = 0;

req.onsuccess = function () /*event*/{
var cursor = req.result; // event.target.result;

if (!cursor) {
resolve(result);
return;
}

var key = cursor.key;

while (key > set[i]) {

// The cursor has passed beyond this key. Check next.
i++;

if (i === set.length) {
// There is no next. Stop searching.
resolve(result);
return;
}
}

if (key === set[i]) {
// The current cursor value should be included and we should continue
// a single step in case next item has the same key or possibly our
// next key in set.
var value = cursor.value;
if (value === undefined) {
value = null;
}

result[key] = value;
// onfound(cursor.value);
cursor.continue();
} else {
// cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
cursor.continue(set[i]);
}
};

req.onerror = function () /*event*/{
reject(req.error);
};
}).catch(reject);
});
executeCallback(promise, callback);
return promise;
}

function getItemsWebsql(keys, callback) {
var localforageInstance = this;
var promise = new Promise(function (resolve, reject) {
localforageInstance.ready().then(function () {
return getSerializerPromise(localforageInstance);
}).then(function (serializer) {
var dbInfo = localforageInstance._dbInfo;
dbInfo.db.transaction(function (t) {

var queryParts = new Array(keys.length);
for (var i = 0, len = keys.length; i < len; i++) {
queryParts[i] = '?';
}

t.executeSql('SELECT * FROM ' + dbInfo.storeName + ' WHERE (key IN (' + queryParts.join(',') + '))', keys, function (t, results) {

var result = {};

var rows = results.rows;
for (var i = 0, len = rows.length; i < len; i++) {
var item = rows.item(i);
var value = item.value;

// Check to see if this is serialized content we need to
// unpack.
if (value) {
value = serializer.deserialize(value);
}

result[item.key] = value;
}

resolve(result);
}, function (t, error) {
reject(error);
});
});
}).catch(reject);
});
executeCallback(promise, callback);
return promise;
}

function getItemsGeneric(keys, callback) {
var localforageInstance = this;
var promise = new Promise(function (resolve, reject) {
var itemPromises = [];

for (var i = 0, len = keys.length; i < len; i++) {
itemPromises.push(getItemKeyValue.call(localforageInstance, keys[i]));
}

Promise.all(itemPromises).then(function (keyValuePairs) {
var result = {};
for (var i = 0, len = keyValuePairs.length; i < len; i++) {
var keyValuePair = keyValuePairs[i];

result[keyValuePair.key] = keyValuePair.value;
}
resolve(result);
}).catch(reject);
});
executeCallback(promise, callback);
return promise;
}

function localforageGetItems(keys, callback) {
var localforageInstance = this;
var currentDriver = localforageInstance.driver();

if (currentDriver === localforageInstance.INDEXEDDB) {
return getItemsIndexedDB.call(localforageInstance, keys, callback);
} else if (currentDriver === localforageInstance.WEBSQL) {
return getItemsWebsql.call(localforageInstance, keys, callback);
} else {
return getItemsGeneric.call(localforageInstance, keys, callback);
}
}

function extendPrototype(localforage) {
var localforagePrototype = Object.getPrototypeOf(localforage);
if (localforagePrototype) {
localforagePrototype.getItems = localforageGetItems;
localforagePrototype.getItems.indexedDB = function () {
return getItemsIndexedDB.apply(this, arguments);
};
localforagePrototype.getItems.websql = function () {
return getItemsWebsql.apply(this, arguments);
};
localforagePrototype.getItems.generic = function () {
return getItemsGeneric.apply(this, arguments);
};
}
}

var extendPrototypeResult = extendPrototype(localforage);

export { getItemsGeneric, localforageGetItems, extendPrototype, extendPrototypeResult };
Loading

0 comments on commit db0364b

Please sign in to comment.