Showing the changes to assets/javascripts (2 of 2 files). Show all
Unified · Split
| @@ -61,7 +61,7 @@ · 60 unmodified lines | ||
| 61 | 61 | bootOne: -> |
| 62 | 62 | @doc = new app.models.Doc @DOC |
| 63 | 63 | @docs.reset [@doc] |
| 64 | − @doc.load @bootDB.bind(@), @onBootError.bind(@), readCache: true | |
| 64 | + @doc.load @start.bind(@), @onBootError.bind(@), readCache: true | |
| 65 | 65 | new app.views.Notice 'singleDoc', @doc |
| 66 | 66 | delete @DOC |
| 67 | 67 | return |
| @@ -72,21 +72,17 @@ · 4 unmodified lines | ||
| 72 | 72 | (if docs.indexOf(doc.slug) >= 0 then @docs else @disabledDocs).add(doc) |
| 73 | 73 | @docs.sort() |
| 74 | 74 | @disabledDocs.sort() |
| 75 | − @docs.load @bootDB.bind(@), @onBootError.bind(@), readCache: true, writeCache: true | |
| 75 | + @docs.load @start.bind(@), @onBootError.bind(@), readCache: true, writeCache: true | |
| 76 | 76 | delete @DOCS |
| 77 | 77 | return |
| 78 | 78 | |
| 79 | − bootDB: -> | |
| 79 | + start: -> | |
| 80 | 80 | for doc in @docs.all() |
| 81 | 81 | @entries.add doc.toEntry() |
| 82 | 82 | @entries.add type.toEntry() for type in doc.types.all() |
| 83 | 83 | @entries.add doc.entries.all() |
| 84 | 84 | |
| 85 | 85 | @db = new app.DB() |
| 86 | − @db.init(@start.bind(@)) | |
| 87 | − return | |
| 88 | − | |
| 89 | − start: -> | |
| 90 | 86 | @trigger 'ready' |
| 91 | 87 | @router.start() |
| 92 | 88 | @hideLoading() |
| @@ -4,14 +4,23 @@ class app.DB · 3 unmodified lines | ||
| 4 | 4 | constructor: -> |
| 5 | 5 | @useIndexedDB = @useIndexedDB() |
| 6 | 6 | |
| 7 | − init: (@_callback) -> | |
| 8 | − if @useIndexedDB | |
| 9 | − @initIndexedDB() | |
| 10 | − else | |
| 11 | − @callback() | |
| 12 | − return | |
| 7 | + 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 | |
| 13 | 23 | |
| 14 | − initIndexedDB: -> | |
| 15 | 24 | try |
| 16 | 25 | req = indexedDB.open(NAME, @indexedDBVersion()) |
| 17 | 26 | req.onerror = @callback |
| @@ -21,20 +30,12 @@ class app.DB · 3 unmodified lines | ||
| 21 | 30 | @callback() |
| 22 | 31 | return |
| 23 | 32 | |
| 24 | − isEnabled: -> | |
| 25 | − !!@db | |
| 26 | − | |
| 27 | − callback: => | |
| 28 | − @_callback?() | |
| 29 | − @_callback = null | |
| 30 | − return | |
| 31 | − | |
| 32 | 33 | onOpenSuccess: (event) => |
| 33 | 34 | try |
| 34 | − @db = event.target.result | |
| 35 | − @db.transaction(['docs', app.docs.all()[0].slug], 'readwrite').abort() # https://bugs.webkit.org/show_bug.cgi?id=136937 | |
| 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 | |
| 36 | 37 | catch |
| 37 | − @db = null | |
| 38 | + @_db = false | |
| 38 | 39 | |
| 39 | 40 | @callback() |
| 40 | 41 | return |
| @@ -53,43 +54,61 @@ class app.DB · 12 unmodified lines | ||
| 53 | 54 | return |
| 54 | 55 | |
| 55 | 56 | store: (doc, data, onSuccess, onError) -> |
| 56 | − txn = @db.transaction ['docs', doc.slug], 'readwrite' | |
| 57 | − txn.oncomplete = -> if txn.error then onError() else onSuccess() | |
| 57 | + @db (db) -> | |
| 58 | + unless db | |
| 59 | + onError() | |
| 60 | + return | |
| 61 | + | |
| 62 | + txn = db.transaction ['docs', doc.slug], 'readwrite' | |
| 63 | + txn.oncomplete = -> if txn.error then onError() else onSuccess() | |
| 58 | 64 | |
| 59 | − store = txn.objectStore(doc.slug) | |
| 60 | − store.clear() | |
| 61 | − store.add(content, path) for path, content of data | |
| 65 | + store = txn.objectStore(doc.slug) | |
| 66 | + store.clear() | |
| 67 | + store.add(content, path) for path, content of data | |
| 62 | 68 | |
| 63 | − store = txn.objectStore('docs') | |
| 64 | − store.put(doc.mtime, doc.slug) | |
| 69 | + store = txn.objectStore('docs') | |
| 70 | + store.put(doc.mtime, doc.slug) | |
| 71 | + return | |
| 65 | 72 | return |
| 66 | 73 | |
| 67 | 74 | unstore: (doc, onSuccess, onError) -> |
| 68 | − txn = @db.transaction ['docs', doc.slug], 'readwrite' | |
| 69 | − txn.oncomplete = -> if txn.error then onError() else onSuccess() | |
| 75 | + @db (db) -> | |
| 76 | + unless db | |
| 77 | + onError() | |
| 78 | + return | |
| 79 | + | |
| 80 | + txn = db.transaction ['docs', doc.slug], 'readwrite' | |
| 81 | + txn.oncomplete = -> if txn.error then onError() else onSuccess() | |
| 70 | 82 | |
| 71 | − store = txn.objectStore(doc.slug) | |
| 72 | − store.clear() | |
| 83 | + store = txn.objectStore(doc.slug) | |
| 84 | + store.clear() | |
| 73 | 85 | |
| 74 | − store = txn.objectStore('docs') | |
| 75 | − store.delete(doc.slug) | |
| 86 | + store = txn.objectStore('docs') | |
| 87 | + store.delete(doc.slug) | |
| 88 | + return | |
| 76 | 89 | return |
| 77 | 90 | |
| 78 | − version: (doc, callback) -> | |
| 79 | − txn = @db.transaction ['docs'], 'readonly' | |
| 80 | − store = txn.objectStore('docs') | |
| 91 | + version: (doc, fn) -> | |
| 92 | + @db (db) -> | |
| 93 | + unless db | |
| 94 | + fn(false) | |
| 95 | + return | |
| 81 | 96 | |
| 82 | − req = store.get(doc.slug) | |
| 83 | − req.onsuccess = -> callback(!!req.result) | |
| 84 | − req.onerror = -> callback(false) | |
| 97 | + txn = db.transaction ['docs'], 'readonly' | |
| 98 | + store = txn.objectStore('docs') | |
| 99 | + | |
| 100 | + req = store.get(doc.slug) | |
| 101 | + req.onsuccess = -> fn(!!req.result) | |
| 102 | + req.onerror = -> fn(false) | |
| 103 | + return | |
| 85 | 104 | return |
| 86 | 105 | |
| 87 | 106 | load: (entry, onSuccess, onError) -> |
| 88 | − if @isEnabled() | |
| 107 | + if @useIndexedDB and @_db isnt false | |
| 89 | 108 | onError = @loadWithXHR.bind(@, entry, onSuccess, onError) |
| 90 | − @loadWithIDB(entry, onSuccess, onError) | |
| 109 | + @loadWithIDB entry, onSuccess, onError | |
| 91 | 110 | else |
| 92 | − @loadWithXHR(entry, onSuccess, onError) | |
| 111 | + @loadWithXHR entry, onSuccess, onError | |
| 93 | 112 | |
| 94 | 113 | loadWithXHR: (entry, onSuccess, onError) -> |
| 95 | 114 | ajax |
| @@ -99,14 +118,19 @@ class app.DB · 3 unmodified lines | ||
| 99 | 118 | error: onError |
| 100 | 119 | |
| 101 | 120 | loadWithIDB: (entry, onSuccess, onError) -> |
| 102 | − txn = @db.transaction [entry.doc.slug], 'readonly' | |
| 103 | − store = txn.objectStore(entry.doc.slug) | |
| 121 | + @db (db) -> | |
| 122 | + unless db | |
| 123 | + onError() | |
| 124 | + return | |
| 125 | + | |
| 126 | + txn = db.transaction [entry.doc.slug], 'readonly' | |
| 127 | + store = txn.objectStore(entry.doc.slug) | |
| 104 | 128 | |
| 105 | − req = store.get(entry.path) | |
| 106 | − req.onsuccess = -> if req.result then onSuccess(req.result) else onError() | |
| 107 | − req.onerror = onError | |
| 129 | + req = store.get(entry.path) | |
| 130 | + req.onsuccess = -> if req.result then onSuccess(req.result) else onError() | |
| 131 | + req.onerror = onError | |
| 108 | 132 | |
| 109 | − txn | |
| 133 | + txn | |
| 110 | 134 | |
| 111 | 135 | reset: -> |
| 112 | 136 | try indexedDB?.deleteDatabase(NAME) catch |