From d768d8abc6e011741f51fcaccce7131967d09a21 Mon Sep 17 00:00:00 2001 From: Wang Hao Date: Tue, 18 Nov 2014 11:27:30 +0800 Subject: [PATCH] add insert_guarantee option --- src/core/proxy.coffee | 2 ++ src/core/proxy.js | 2 ++ src/core/storage.coffee | 5 ++++- src/core/storage.js | 6 +++++- src/localdb.coffee | 2 ++ src/localdb.js | 4 +++- 6 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/core/proxy.coffee b/src/core/proxy.coffee index 295af6e..54133ae 100644 --- a/src/core/proxy.coffee +++ b/src/core/proxy.coffee @@ -12,6 +12,7 @@ define (require, exports, module) -> @encrypt = options.encrypt @name = options.name @proxy = options.proxy + @insert_guarantee = options.insert_guarantee @evemit = new Evemit() @iframe = Utils.createIframe @proxy Evemit.bind window, "message", (e) -> @@ -29,6 +30,7 @@ define (require, exports, module) -> data.expire = @expire data.encrypt = @encrypt data.name = @name + data.insert_guarantee = @insert_guarantee @evemit.once eve, callback data = JSON.stringify data ifrWin = @iframe.contentWindow diff --git a/src/core/proxy.js b/src/core/proxy.js index 9e93713..80d45c3 100644 --- a/src/core/proxy.js +++ b/src/core/proxy.js @@ -12,6 +12,7 @@ define(function(require, exports, module) { this.encrypt = options.encrypt; this.name = options.name; this.proxy = options.proxy; + this.insert_guarantee = options.insert_guarantee; this.evemit = new Evemit(); this.iframe = Utils.createIframe(this.proxy); Evemit.bind(window, "message", function(e) { @@ -36,6 +37,7 @@ define(function(require, exports, module) { data.expire = this.expire; data.encrypt = this.encrypt; data.name = this.name; + data.insert_guarantee = this.insert_guarantee; this.evemit.once(eve, callback); data = JSON.stringify(data); ifrWin = this.iframe.contentWindow; diff --git a/src/core/storage.coffee b/src/core/storage.coffee index 2f8e205..54c64e8 100644 --- a/src/core/storage.coffee +++ b/src/core/storage.coffee @@ -11,6 +11,7 @@ define (require, exports, module) -> @expire = options.expire @encrypt = options.encrypt @token = options.name + @insert_guarantee = options.insert_guarantee if @expire is "window" throw new Error("sessionStorage is not supported!") if not Support.sessionstorage() @storage = sessionStorage @@ -42,9 +43,11 @@ define (require, exports, module) -> @storage.setItem(key, val) catch e ### TODO - * 需要在LocalDB的构造函数中增加配置参数,来确定是否自动删除最老数据 * 增加过期时间配置项 ### + if not @insert_guarantee + callback(e) + return val = Encrypt.decode(val, @token) if @encrypt data = Utils.parse val while cnt > 10 diff --git a/src/core/storage.js b/src/core/storage.js index c4c12f0..71e3268 100644 --- a/src/core/storage.js +++ b/src/core/storage.js @@ -10,6 +10,7 @@ define(function(require, exports, module) { this.expire = options.expire; this.encrypt = options.encrypt; this.token = options.name; + this.insert_guarantee = options.insert_guarantee; if (this.expire === "window") { if (!Support.sessionstorage()) { throw new Error("sessionStorage is not supported!"); @@ -58,9 +59,12 @@ define(function(require, exports, module) { e = _error; /* TODO - * 需要在LocalDB的构造函数中增加配置参数,来确定是否自动删除最老数据 * 增加过期时间配置项 */ + if (!this.insert_guarantee) { + callback(e); + return; + } if (this.encrypt) { val = Encrypt.decode(val, this.token); } diff --git a/src/localdb.coffee b/src/localdb.coffee index 1b3d5a7..a13780e 100644 --- a/src/localdb.coffee +++ b/src/localdb.coffee @@ -35,11 +35,13 @@ define (require, exports, module) -> @expire = if options.expire? then options.expire else "window" @encrypt = if options.encrypt? then options.encrypt else true @proxy = if options.proxy? then options.proxy else null + @insert_guarantee = if options.guarantee then options.guarantee else false @engine = new Engine { expire: @expire encrypt: @encrypt name: @name proxy: @proxy + insert_guarantee: @insert_guarantee } # get options diff --git a/src/localdb.js b/src/localdb.js index 41bcfac..aa1841c 100644 --- a/src/localdb.js +++ b/src/localdb.js @@ -39,11 +39,13 @@ define(function(require, exports, module) { this.expire = options.expire != null ? options.expire : "window"; this.encrypt = options.encrypt != null ? options.encrypt : true; this.proxy = options.proxy != null ? options.proxy : null; + this.insert_guarantee = options.guarantee ? options.guarantee : false; this.engine = new Engine({ expire: this.expire, encrypt: this.encrypt, name: this.name, - proxy: this.proxy + proxy: this.proxy, + insert_guarantee: this.insert_guarantee }); }