History

Speed up offline page by loading download statuses in a single transaction

Thibaut committed 11 years ago

Commit c2879a51 parent 4f781d74 files · +44 −25Browse files

Showing the changes to assets/javascripts (4 of 4 files). Show all

Unified · Split

4 files
  1. assets/javascripts/app/db.coffee +20 −0
  2. assets/javascripts/collections/docs.coffee +6 −0
  3. assets/javascripts/templates/pages/offline_tmpl.coffee +6 −9
  4. assets/javascripts/views/content/offline_page.coffee +12 −16
assets/javascripts/app/db.coffee+20−0
@@ -103,6 +103,26 @@ class app.DB · 102 unmodified lines
103103 return
104104 return
105105
106+ versions: (docs, fn) ->
107+ @db (db) ->
108+ result = {}
109+
110+ unless db
111+ result[doc.slug] = false for doc in docs
112+ fn(result)
113+ return
114+
115+ txn = db.transaction ['docs'], 'readonly'
116+ txn.oncomplete = -> fn(result)
117+ store = txn.objectStore('docs')
118+
119+ docs.forEach (doc) ->
120+ req = store.get(doc.slug)
121+ req.onsuccess = -> result[doc.slug] = req.result
122+ req.onerror = -> result[doc.slug] = false
123+ return
124+ return
125+
106126 load: (entry, onSuccess, onError) ->
107127 if @useIndexedDB and @_db isnt false
108128 onError = @loadWithXHR.bind(@, entry, onSuccess, onError)
assets/javascripts/collections/docs.coffee+6−0
@@ -34,3 +34,9 @@ class app.collections.Docs extends app.Collection · 33 unmodified lines
3434 clearCache: ->
3535 doc.clearCache() for doc in @models
3636 return
37+
38+ getDownloadStatuses: (callback) ->
39+ app.db.versions @models, (statuses) ->
40+ for key, value of statuses
41+ statuses[key] = downloaded: !!value, version: value
42+ callback(statuses)
assets/javascripts/templates/pages/offline_tmpl.coffee+6−9
@@ -1,16 +1,13 @@
1−app.templates.offlinePage = ->
1+app.templates.offlinePage = (docs) ->
22 """ <h1 class="_lined-heading">Offline</h1>
3− <table class="_docs">
4− #{app.templates.render 'offlineDoc', app.docs.all()}
5− </table> """
3+ <table class="_docs">#{docs}</table> """
64
7−app.templates.offlineDoc = (doc) ->
8− """<tr data-slug="#{doc.slug}"></tr>"""
9−
10−app.templates.offlineDocContent = (doc, status) ->
11− html = """<th class="_icon-#{doc.slug}">#{doc.name}</th>"""
5+app.templates.offlineDoc = (doc, status) ->
6+ html = """<tr data-slug="#{doc.slug}">"""
7+ html += """<th class="_icon-#{doc.slug}">#{doc.name}</th>"""
128 html += if status.downloaded
139 """<td><a data-del>Delete</a></td>"""
1410 else
1511 """<td><a data-dl>Download</a></td>"""
12+ html += """</tr>"""
1613 html
assets/javascripts/views/content/offline_page.coffee+12−16
@@ -4,32 +4,26 @@ class app.views.OfflinePage extends app.View · 3 unmodified lines
44 @events:
55 click: 'onClick'
66
7− @elements:
8− list: '_._docs'
9−
107 deactivate: ->
118 if super
129 @empty()
1310 return
1411
1512 render: ->
16− @html @tmpl('offlinePage')
17− @refreshElements()
18− app.docs.each(@renderDoc)
13+ app.docs.getDownloadStatuses (statuses) =>
14+ html = ''
15+ html += @renderDoc(doc, statuses[doc.slug]) for doc in app.docs.all()
16+ @html @tmpl('offlinePage', html)
17+ return
1918 return
2019
21− renderDoc: (doc) =>
22− doc.getDownloadStatus (status) =>
23− html = app.templates.render('offlineDocContent', doc, status)
24− el = @docEl(doc)
25− el.className = ''
26− el.innerHTML = html
27− return
20+ renderDoc: (doc, status) ->
21+ app.templates.render('offlineDoc', doc, status)
2822
2923 getTitle: ->
3024 'Offline'
3125
32− getDoc: (el) ->
26+ docByEl: (el) ->
3327 el = el.parentNode until slug = el.getAttribute('data-slug')
3428 app.docs.findBy('slug', slug)
3529
@@ -48,16 +42,18 @@ class app.views.OfflinePage extends app.View · 12 unmodified lines
4842
4943 if action
5044 $.stopEvent(event)
51− doc = @getDoc(event.target)
45+ doc = @docByEl(event.target)
5246 doc[action](@onDownloadSuccess.bind(@, doc), @onDownloadError.bind(@, doc))
5347 @docEl(doc).classList.add("#{action}ing")
5448 return
5549
5650 onDownloadSuccess: (doc) ->
57− @renderDoc(doc)
51+ doc.getDownloadStatus (status) =>
52+ @docEl(doc).outerHTML = @renderDoc(doc, status)
5853 return
5954
6055 onDownloadError: (doc) ->
6156 el = @docEl(doc)
6257 el.className = ''
6358 el.classList.add('error')
59+ return