Only create stores for enabled docs in IndexedDB

Ref #25.
pull/308/merge
Thibaut Courouble 9 years ago
parent b2d2066d96
commit 6ed3994988

@ -16,6 +16,7 @@
@store = new Store @store = new Store
@appCache = new app.AppCache if app.AppCache.isEnabled() @appCache = new app.AppCache if app.AppCache.isEnabled()
@settings = new app.Settings @store @settings = new app.Settings @store
@db = new app.DB()
@docs = new app.collections.Docs @docs = new app.collections.Docs
@disabledDocs = new app.collections.Docs @disabledDocs = new app.collections.Docs
@ -86,7 +87,6 @@
@entries.add doc.toEntry() for doc in @docs.all() @entries.add doc.toEntry() for doc in @docs.all()
@entries.add doc.toEntry() for doc in @disabledDocs.all() @entries.add doc.toEntry() for doc in @disabledDocs.all()
@initDoc(doc) for doc in @docs.all() @initDoc(doc) for doc in @docs.all()
@db = new app.DB()
@trigger 'ready' @trigger 'ready'
@router.start() @router.start()
@hideLoading() @hideLoading()
@ -125,6 +125,7 @@
saveDocs: -> saveDocs: ->
@settings.setDocs(doc.slug for doc in @docs.all()) @settings.setDocs(doc.slug for doc in @docs.all())
@db.migrate()
@appCache?.updateInBackground() @appCache?.updateInBackground()
welcomeBack: -> welcomeBack: ->

@ -3,7 +3,7 @@ class app.DB
constructor: -> constructor: ->
@useIndexedDB = @useIndexedDB() @useIndexedDB = @useIndexedDB()
@indexedDBVersion = @indexedDBVersion() @appVersion = @appVersion()
@callbacks = [] @callbacks = []
db: (fn) -> db: (fn) ->
@ -13,7 +13,7 @@ class app.DB
try try
@open = true @open = true
req = indexedDB.open(NAME, @indexedDBVersion) req = indexedDB.open(NAME, @schemaVersion())
req.onsuccess = @onOpenSuccess req.onsuccess = @onOpenSuccess
req.onerror = @onOpenError req.onerror = @onOpenError
req.onupgradeneeded = @onUpgradeNeeded req.onupgradeneeded = @onUpgradeNeeded
@ -59,14 +59,16 @@ class app.DB
onUpgradeNeeded: (event) -> onUpgradeNeeded: (event) ->
return unless db = event.target.result return unless db = event.target.result
unless db.objectStoreNames.contains('docs') objectStoreNames = $.makeArray(db.objectStoreNames)
unless $.arrayDelete(objectStoreNames, 'docs')
db.createObjectStore('docs') db.createObjectStore('docs')
for doc in app.docs.all() when not db.objectStoreNames.contains(doc.slug) for doc in app.docs.all() when not $.arrayDelete(objectStoreNames, doc.slug)
db.createObjectStore(doc.slug) db.createObjectStore(doc.slug)
for doc in app.disabledDocs.all() when not db.objectStoreNames.contains(doc.slug) for name in objectStoreNames
db.createObjectStore(doc.slug) db.deleteObjectStore(name)
return return
store: (doc, data, onSuccess, onError) -> store: (doc, data, onSuccess, onError) ->
@ -201,6 +203,11 @@ class app.DB
onError() onError()
return return
unless db.objectStoreNames.contains(entry.doc.slug)
onError()
@loadDocsCache(db)
return
txn = @idbTransaction db, stores: [entry.doc.slug], mode: 'readonly' txn = @idbTransaction db, stores: [entry.doc.slug], mode: 'readonly'
store = txn.objectStore(entry.doc.slug) store = txn.objectStore(entry.doc.slug)
@ -212,10 +219,11 @@ class app.DB
event.preventDefault() event.preventDefault()
onError() onError()
return return
@loadDocsCache(db) unless @cachedDocs @loadDocsCache(db)
return return
loadDocsCache: (db) -> loadDocsCache: (db) ->
return if @cachedDocs
@cachedDocs = {} @cachedDocs = {}
txn = @idbTransaction db, stores: ['docs'], mode: 'readonly' txn = @idbTransaction db, stores: ['docs'], mode: 'readonly'
@ -261,5 +269,15 @@ class app.DB
catch catch
false false
indexedDBVersion: -> migrate: ->
if app.config.env is 'production' then parseInt(app.config.version, 10) else Date.now() / 1000 app.settings.set('schema', @userVersion() + 1)
return
schemaVersion: ->
@appVersion * 10 + @userVersion()
userVersion: ->
app.settings.get('schema')
appVersion: ->
if app.config.env is 'production' then parseInt(app.config.version, 10) else Math.floor(Date.now() / 1000)

@ -5,18 +5,19 @@ class app.Settings
LAYOUT_KEY = 'layout' LAYOUT_KEY = 'layout'
SIZE_KEY = 'size' SIZE_KEY = 'size'
@defaults: -> @defaults:
count: 0 count: 0
hideDisabled: false hideDisabled: false
hideIntro: false hideIntro: false
news: 0 news: 0
autoUpdate: true autoUpdate: true
schema: 0
constructor: (@store) -> constructor: (@store) ->
@create() unless @settings = @store.get(SETTINGS_KEY) @create() unless @settings = @store.get(SETTINGS_KEY)
create: -> create: ->
@settings = @constructor.defaults() @settings = $.extend({}, @constructor.defaults)
@applyLegacyValues @settings @applyLegacyValues @settings
@save() @save()
return return
@ -35,7 +36,7 @@ class app.Settings
@save() @save()
get: (key) -> get: (key) ->
@settings[key] @settings[key] ? @constructor.defaults[key]
hasDocs: -> hasDocs: ->
try !!Cookies.get DOCS_KEY try !!Cookies.get DOCS_KEY

@ -239,6 +239,14 @@ $.makeArray = (object) ->
else else
Array::slice.apply(object) Array::slice.apply(object)
$.arrayDelete = (array, object) ->
index = array.indexOf(object)
if index >= 0
array.splice(index, 1)
true
else
false
# Returns true if the object is an array or a collection of DOM elements. # Returns true if the object is an array or a collection of DOM elements.
$.isCollection = (object) -> $.isCollection = (object) ->
Array.isArray(object) or typeof object?.item is 'function' Array.isArray(object) or typeof object?.item is 'function'

@ -50,7 +50,9 @@ class app.views.DocPicker extends app.View
app.settings.setDocs(docs) app.settings.setDocs(docs)
@saveLink.textContent = if app.appCache then 'Downloading\u2026' else 'Saving\u2026' @saveLink.textContent = if app.appCache then 'Downloading\u2026' else 'Saving\u2026'
disabledDocs = new app.collections.Docs(doc for doc in app.docs.all() when docs.indexOf(doc.slug) is -1) disabledDocs = new app.collections.Docs(doc for doc in app.docs.all() when docs.indexOf(doc.slug) is -1)
disabledDocs.uninstall -> app.reload() disabledDocs.uninstall ->
app.db.migrate()
app.reload()
return return
getSelectedDocs: -> getSelectedDocs: ->

Loading…
Cancel
Save