/*global indexedDB*/
'use strict';
(function (factory) {
    if (typeof define === 'function' && define.amd) {
        define([], factory);
    } else if (typeof define === 'function' && define.cmd) {
        define(function (require, exports, module) {
            module.exports = factory();
        });
    }
}(function () {
    var DbPromise = function (fun) {
        this.state = 'pending';
        this.resolveList = [];
        this.rejectList = [];
        var _this = this;
        fun(function () {
            _this.resolve.apply(_this, arguments)
        }, function () {
            _this.reject.apply(_this, arguments)
        });
    };
    DbPromise.prototype = {};
    DbPromise.prototype.resolve = function (data) {
        this.state = 'resolved';
        this.data = data;
        var _this = this;
        this.resolveList.forEach(function (fun) {
            _this.data = fun(_this.data)
        });
    };
    DbPromise.prototype.reject = function (data) {
        this.state = 'rejected';
        this.error = data;
        this.rejectList.forEach(function (fun) {
            fun(data);
        });
    };
    DbPromise.prototype.then = function (fun) {
        if (this.state === 'pending') {
            this.resolveList.push(fun);
        } else {
            this.data = fun(this.data);
        }
        return this;
    };
    DbPromise.prototype.catch = function (fun) {
        if (this.state === 'pending') {
            this.rejectList.push(fun);
        } else {
            fun(this.error);
        }
        return this;
    };

    var DB = function (name, upgrade, version) {
        var _this = this;
        if (DB.dbMap[name]) {
            var map = DB.dbMap[name];
            return _open(name, map.upgrade, map.version).then(function (db) {
                _this.db = db;
                return _this;
            }).then(map.nextStep);
        } else {
            return _open(name, upgrade, version).then(function (db) {
                _this.db = db;
                return _this;
            });
        }
    };
    DB.prototype = {};

    DB.prototype.put = function (table, data, tx) {
        return _put(this.db, table, data, tx || this.tx);
    };
    DB.prototype.get = function (table, name, tx) {
        return _get(this.db, table, name, tx || this.tx);
    };
    DB.prototype.clear = function (table, tx) {
        return _clear(this.db, table, tx || this.tx);
    };
    DB.prototype.transaction = function (table, type, asDefault) {
        var tx = _transaction(this.db, table, type);
        if (asDefault) {
            this.tx = tx;
        }
        return tx;
    };
    DB.prototype.transactionEnd = function () {
        this.tx = void 0;
    };
    DB.prototype.getKv = function (table, k, tx) {
        return _get(this.db, table, k, tx).then(o=>(o.target.result || {}).v);
    };
    DB.prototype.putKv = function (table, k, v, tx) {
        return _put(this.db, table, {k, v}, tx).then(o=>(o.target.result));
    };

    DB.prototype.getKvStore = function (name) {
        var _this = this;
        return function (k, v) {
            if (v === void 0) {
                return _get(_this.db, name, k).then(o=>(o.target.result || {}).v);
            } else {
                return _put(_this.db, name, {k, v}).then(o=>(o.target.result || {}).v);
            }
        }
    };
    DB.dbMap = {};
    return DB;

    function _open(name, upgrade, version) {
        return new DbPromise(function (resolve, reject) {
            var request = indexedDB.open(name, version);
            request.onupgradeneeded = upgrade;
            request.onsuccess = function (e) {
                resolve(request.result);
            };
            request.onerror = reject;
        });
    }

    function _put(db, table, data, tx, g) {
        return new DbPromise(function (resolve, reject) {
            tx = tx || db.transaction(table, 'readwrite');
            var store = tx.objectStore(table);
            var request = store.put(data);
            request.onsuccess = function (e) {
                resolve(e);
            };
            request.onerror = reject;

        });
    }

    function _clear(db, table, tx) {
        return new Promise(function (resolve, reject) {
            tx = tx || db.transaction(table, 'readwrite');
            var store = tx.objectStore(table);
            var request = store.clear();
            request.onsuccess = function (e) {
                resolve(e);
            };
            request.onerror = reject;
        });
    }

    function _get(db, table, key, tx, g) {
        return new DbPromise(function (resolve, reject) {
            tx = tx || db.transaction(table, 'readonly');
            var store = tx.objectStore(table);
            var request = store.get(key);
            request.onsuccess = function (e) {
                resolve(e);
            };
            request.onerror = reject;
        });
    }

    function _transaction(db, table, type) {
        return db.transaction(table, type);
    }
}));
@@ -1,8 +1,49 @@
+/**%0A * Created by sail on 2015/11/6.%0A */%0A
 /*global
@@ -2855,38 +2855,24 @@
 ).then(o=%3E(o
-.target.result
  %7C%7C %7B%7D).v);%0A
@@ -2983,35 +2983,8 @@
  tx)
-.then(o=%3E(o.target.result))
 ;%0A  
@@ -3181,38 +3181,24 @@
 ).then(o=%3E(o
-.target.result
  %7C%7C %7B%7D).v);%0A
@@ -3284,22 +3284,8 @@
 =%3E(o
-.target.result
  %7C%7C 
@@ -3599,33 +3599,32 @@
 ess = function (
-e
 ) %7B%0A            
@@ -3757,27 +3757,24 @@
 le, data, tx
-, g
 ) %7B%0A        
@@ -4006,33 +4006,32 @@
 ess = function (
-e
 ) %7B%0A            
@@ -4034,33 +4034,46 @@
         resolve(
-e
+request.result
 );%0A            %7D
@@ -4406,33 +4406,32 @@
 ess = function (
-e
 ) %7B%0A            
@@ -4434,33 +4434,46 @@
         resolve(
-e
+request.result
 );%0A            %7D
@@ -4571,11 +4571,8 @@
 , tx
-, g
 ) %7B%0A
@@ -4818,17 +4818,16 @@
 nction (
-e
 ) %7B%0A    
@@ -4846,17 +4846,30 @@
 resolve(
-e
+request.result
 );%0A     

Fix bug of return type