History

Don't keep the IndexedDB database open

Because it makes attempts to open the database in other tabs hang until that tab is close / the database is closed.

Thibaut committed 11 years ago

Commit 04676ed1 parent ada99491 file · +26 −23Browse files

Unified · Split

assets/javascripts/app/db.coffee+26−23
@@ -3,44 +3,47 @@ class app.DB · 2 unmodified lines
33
44 constructor: ->
55 @useIndexedDB = @useIndexedDB()
6+ @callbacks = []
67
78 db: (fn) ->
8− return fn @_db unless @useIndexedDB and @_db is undefined
9−
10− if @callback isnt undefined
11− _callback = @callback
12− @callback = =>
13− _callback()
14− fn @_db
15− return
16− return
17−
18− @callback = =>
19− @_db ?= false
20− delete @callback
21− fn @_db
22− return
9+ return fn() unless @useIndexedDB
10+ @callbacks.push(fn)
11+ return if @open
2312
2413 try
14+ @open = true
2515 req = indexedDB.open(NAME, @indexedDBVersion())
26− req.onerror = @callback
2716 req.onsuccess = @onOpenSuccess
17+ req.onerror = @onOpenError
2818 req.onupgradeneeded = @onUpgradeNeeded
2919 catch
30− @callback()
20+ @onOpenError()
3121 return
3222
3323 onOpenSuccess: (event) =>
3424 try
35− @_db = event.target.result
36− @_db.transaction(['docs', app.docs.all()[0].slug], 'readwrite').abort() # https://bugs.webkit.org/show_bug.cgi?id=136937
25+ db = event.target.result
26+ db.transaction(['docs', app.docs.all()[0].slug], 'readwrite').abort() # https://bugs.webkit.org/show_bug.cgi?id=136937
3727 catch
38− @_db = false
28+ try db.close()
29+ @onOpenError()
30+ return
31+
32+ @runCallbacks(db)
33+ @open = false
34+ db.close()
35+ return
36+
37+ onOpenError: =>
38+ @useIndexedDB = @open = false
39+ @runCallbacks()
40+ return
3941
40− @callback()
42+ runCallbacks: (db) ->
43+ fn(db) while fn = @callbacks.shift()
4144 return
4245
43− onUpgradeNeeded: (event) =>
46+ onUpgradeNeeded: (event) ->
4447 db = event.target.result
4548
4649 unless db.objectStoreNames.contains('docs')
@@ -126,7 +129,7 @@ class app.DB · 79 unmodified lines
126129 return
127130
128131 load: (entry, onSuccess, onError) ->
129− if @useIndexedDB and @_db isnt false
132+ if @useIndexedDB
130133 onError = @loadWithXHR.bind(@, entry, onSuccess, onError)
131134 @loadWithIDB entry, onSuccess, onError
132135 else