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