Skip to content

Commit

Permalink
解决storage因为某些异常一直无法插入数据时出现死循环的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1100717 committed Nov 17, 2014
1 parent f93e1fc commit efc8a71
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
19 changes: 6 additions & 13 deletions src/core/storage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ define (require, exports, module) ->

setItem: (key, val, callback) ->
self = @
cnt = 0
try
val = Encrypt.encode(val, @token) if @encrypt
@storage.setItem(key, val)
Expand All @@ -41,26 +42,18 @@ define (require, exports, module) ->
* 需要在LocalDB的构造函数中增加配置参数,来确定是否自动删除最老数据
* 增加过期时间配置项
###
### TODO
* 这里有可能是非容量满等其他原因导致出错
* 所以需要设置一个最大尝试阀值,或者根据出错信息来判断是否继续循环
* 避免死循环
###
flag = true
val = Encrypt.decode(val, @token) if @encrypt
data = Utils.parse val
while flag
while cnt > 10
try
data.splice(0, 1)
val = Utils.stringify(data)
val = Encrypt.encode(val, self.token) if self.encrypt
self.storage.setItem(key, val)
flag = false
### TODO
* 目前采用的是删除初始数据来保证在数据存满以后仍然可以继续存下去
* 在初始化LocalDB的时候需要增加配置参数,根据参数来决定是否自动删除初始数据,还是返回e
###
callback()
cnt = 11
catch e
cnt += 1
callback (new Error("exceed maximum times trying setItem into Storage") if cnt > 10)
return

getItem: (key, callback) ->
Expand Down
26 changes: 9 additions & 17 deletions src/core/storage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit efc8a71

Please sign in to comment.