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

chore: use es6 & rollup #11

Merged
merged 4 commits into from
May 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.DS_Store
.swp
dist
build
bower_components
node_modules
components
Expand Down
31 changes: 31 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"esnext": true,
"disallowSpacesInAnonymousFunctionExpression": {
"beforeOpeningRoundBrace": true
},
"disallowTrailingComma": true,
"requireBlocksOnNewline": true,
"requireLineFeedAtFileEnd": true,
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"return",
"try",
"catch"
],
"requireSpaceBeforeBlockStatements": true,
"requireSpacesInConditionalExpression": true,
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"safeContextKeyword": ["globalObject", "self"],
"validateQuoteMarks": {
"escape": true,
"mark": "'"
},
"validateIndentation": 4
}
32 changes: 32 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"asi": false,
"bitwise": true,
"browser": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"esnext": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"nonew": true,
"quotmark": false,
"strict": false,
"trailing": false,
"undef": true,
"unused": true,

"validthis": true,

"globals": {
"console": true,
"define": true,
"localforage": true,
"module": 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"
10 changes: 1 addition & 9 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "localforage-cordovasqlitedriver",
"version": "1.3.0",
"version": "1.4.0",
"main": [
"src/localforage-cordovasqlitedriver.js"
],
Expand All @@ -19,13 +19,5 @@
],
"dependencies": {
"localforage": "^1.4.0"
},
"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"
}
}
11 changes: 0 additions & 11 deletions component.json

This file was deleted.

25 changes: 25 additions & 0 deletions lib/cordova-sqlite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* global document, sqlitePlugin */
// we can't import this, since it gets defined later
// import sqlitePlugin from 'sqlitePlugin';

var deviceReady = new Promise(function(resolve, reject) {
if (typeof sqlitePlugin !== 'undefined') {
resolve();
} else if (typeof cordova === 'undefined') {
reject();
} else {
// Wait for Cordova to load
document.addEventListener("deviceready", resolve, false);
}
});

export var openDatabasePromise = deviceReady.catch(Promise.resolve).then(function() {
return new Promise(function(resolve, reject) {
if (typeof sqlitePlugin !== 'undefined' &&
typeof sqlitePlugin.openDatabase === 'function') {
resolve(sqlitePlugin.openDatabase);
} else {
reject('SQLite plugin is not present.');
}
});
});
120 changes: 120 additions & 0 deletions lib/localforage-cordovasqlitedriver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Includes code from:
*
* localForage - websql driver
* https://github.com/mozilla/localforage
*
* Copyright (c) 2015 Mozilla
* Licensed under Apache 2.0 license.
*
*/
// import localforage from 'localforage';
import { getSerializerPromise, getWebSqlDriverPromise } from './utils';
import { openDatabasePromise } from './cordova-sqlite';

// // If cordova is not present, we can stop now.
// if (!globalObject.cordova) {
// return;
// }

// Open the cordova sqlite plugin database (automatically creates one if one didn't
// previously exist), using any options set in the config.
function _initStorage(options) {
var self = this;
var dbInfo = {
db: null
};

if (options) {
for (var i in options) {
dbInfo[i] = typeof(options[i]) !== 'string' ?
options[i].toString() : options[i];
}
}

var dbInfoPromise = openDatabasePromise.then(function(openDatabase){
return new Promise(function(resolve, reject) {
// Open the database; the openDatabase API will automatically
// create it for us if it doesn't exist.
try {
dbInfo.location = dbInfo.location || 'default';
dbInfo.db = openDatabase({
name: dbInfo.name,
version: String(dbInfo.version),
description: dbInfo.description,
size: dbInfo.size,
location: dbInfo.location
});
} catch (e) {
reject(e);
}

// Create our key/value table if it doesn't exist.
dbInfo.db.transaction(function(t) {
t.executeSql('CREATE TABLE IF NOT EXISTS ' + dbInfo.storeName +
' (id INTEGER PRIMARY KEY, key unique, value)', [],
function() {
self._dbInfo = dbInfo;
resolve();
}, function(t, error) {
reject(error);
});
});
});
});

var serializerPromise = getSerializerPromise(self);
var webSqlDriverPromise = getWebSqlDriverPromise(self);

return Promise.all([
serializerPromise,
webSqlDriverPromise,
dbInfoPromise
]).then(function(results) {
dbInfo.serializer = results[0];
return dbInfoPromise;
});
}

var cordovaSQLiteDriver = {
_driver: 'cordovaSQLiteDriver',
_initStorage: _initStorage,
_support: function() {
return openDatabasePromise.then(function(openDatabase) {
return !!openDatabase;
}).catch(function(){
return false;
});
}
};

function wireUpDriverMethods(driver) {
var LibraryMethods = [
'clear',
'getItem',
'iterate',
'key',
'keys',
'length',
'removeItem',
'setItem'
];

function wireUpDriverMethod(driver, methodName) {
driver[methodName] = function () {
var localForageInstance = this;
var args = arguments;
return getWebSqlDriverPromise(localForageInstance).then(function (webSqlDriver) {
return webSqlDriver[methodName].apply(localForageInstance, args);
});
};
}

for (var i = 0, len = LibraryMethods.length; i < len; i++) {
wireUpDriverMethod(driver, LibraryMethods[i]);
}
}

wireUpDriverMethods(cordovaSQLiteDriver);

export default cordovaSQLiteDriver;
40 changes: 40 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export 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;
}

export function getDriverPromise(localForageInstance, driverName) {
getDriverPromise.result = getDriverPromise.result || {};
if (getDriverPromise.result[driverName]) {
return getDriverPromise.result[driverName];
}
if (!localForageInstance || typeof localForageInstance.getDriver !== 'function') {
Promise.reject(new Error(
'localforage.getDriver() was not available! ' +
'localforage v1.4+ is required!'));
}
getDriverPromise.result[driverName] = localForageInstance.getDriver(driverName);
return getDriverPromise.result[driverName];
}

export function getWebSqlDriverPromise(localForageInstance) {
return getDriverPromise(localForageInstance, localForageInstance.WEBSQL);
}

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