@ -9,6 +9,12 @@ const limit = 1024;
// https://webkit.org/status/#specification-service-workers
const asyncCache = window . caches ? caches . open ( 'mastodon-system' ) : Promise . reject ( ) ;
function printErrorIfAvailable ( error ) {
if ( error ) {
console . warn ( error ) ;
}
}
function put ( name , objects , onupdate , oncreate ) {
return asyncDB . then ( db => new Promise ( ( resolve , reject ) => {
const putTransaction = db . transaction ( name , 'readwrite' ) ;
@ -77,7 +83,9 @@ function evictAccountsByRecords(records) {
function evict ( toEvict ) {
toEvict . forEach ( record => {
asyncCache . then ( cache => accountAssetKeys . forEach ( key => cache . delete ( records [ key ] ) ) ) ;
asyncCache
. then ( cache => accountAssetKeys . forEach ( key => cache . delete ( records [ key ] ) ) )
. catch ( printErrorIfAvailable ) ;
accountsMovedIndex . getAll ( record . id ) . onsuccess = ( { target } ) => evict ( target . result ) ;
@ -90,11 +98,11 @@ function evictAccountsByRecords(records) {
}
evict ( records ) ;
} ) ;
} ) .catch ( printErrorIfAvailable ) ;
}
export function evictStatus ( id ) {
return evictStatuses ( [ id ] ) ;
evictStatuses ( [ id ] ) ;
}
export function evictStatuses ( ids ) {
@ -110,7 +118,7 @@ export function evictStatuses(ids) {
idIndex . getKey ( id ) . onsuccess =
( { target } ) => target . result && store . delete ( target . result ) ;
} ) ;
} ) ;
} ) .catch ( printErrorIfAvailable ) ;
}
function evictStatusesByRecords ( records ) {
@ -127,7 +135,9 @@ export function putAccounts(records) {
const oldURL = target . result [ key ] ;
if ( newURL !== oldURL ) {
asyncCache . then ( cache => cache . delete ( oldURL ) ) ;
asyncCache
. then ( cache => cache . delete ( oldURL ) )
. catch ( printErrorIfAvailable ) ;
}
} ) ;
@ -145,10 +155,14 @@ export function putAccounts(records) {
oncomplete ( ) ;
} ) . then ( records => {
evictAccountsByRecords ( records ) ;
asyncCache . then ( cache => cache . addAll ( newURLs ) ) ;
} ) ;
asyncCache
. then ( cache => cache . addAll ( newURLs ) )
. catch ( printErrorIfAvailable ) ;
} ) . catch ( printErrorIfAvailable ) ;
}
export function putStatuses ( records ) {
put ( 'statuses' , records ) . then ( evictStatusesByRecords ) ;
put ( 'statuses' , records )
. then ( evictStatusesByRecords )
. catch ( printErrorIfAvailable ) ;
}