import api from '../api';
-import asyncDB from '../storage/db';
+import openDB from '../storage/db';
import { evictStatus } from '../storage/modifier';
import { deleteFromTimelines } from './timelines';
dispatch(fetchStatusRequest(id, skipLoading));
- asyncDB.then(db => {
+ openDB().then(db => {
const transaction = db.transaction(['accounts', 'statuses'], 'read');
const accountIndex = transaction.objectStore('accounts').index('id');
const index = transaction.objectStore('statuses').index('id');
- return getFromDB(dispatch, getState, accountIndex, index, id);
+ return getFromDB(dispatch, getState, accountIndex, index, id).then(() => {
+ db.close();
+ }, error => {
+ db.close();
+ throw error;
+ });
}).then(() => {
dispatch(fetchStatusSuccess(skipLoading));
}, () => api(getState).get(`/api/v1/statuses/${id}`).then(response => {
-import asyncDB from './db';
-import { autoPlayGif } from '../initial_state';
+import openDB from './db';
const accountAssetKeys = ['avatar', 'avatar_static', 'header', 'header_static'];
-const avatarKey = autoPlayGif ? 'avatar' : 'avatar_static';
-const limit = 1024;
+const storageMargin = 8388608;
+const storeLimit = 1024;
-// ServiceWorker and Cache API is not available on iOS 11
-// https://webkit.org/status/#specification-service-workers
-const asyncCache = window.caches ? caches.open('mastodon-system') : Promise.reject();
+function openCache() {
+ // ServiceWorker and Cache API is not available on iOS 11
+ // https://webkit.org/status/#specification-service-workers
+ return self.caches ? caches.open('mastodon-system') : Promise.reject();
+}
function printErrorIfAvailable(error) {
if (error) {
}
function put(name, objects, onupdate, oncreate) {
- return asyncDB.then(db => new Promise((resolve, reject) => {
+ return openDB().then(db => (new Promise((resolve, reject) => {
const putTransaction = db.transaction(name, 'readwrite');
const putStore = putTransaction.objectStore(name);
const putIndex = putStore.index('id');
const count = readStore.count();
count.onsuccess = () => {
- const excess = count.result - limit;
+ const excess = count.result - storeLimit;
if (excess > 0) {
const retrieval = readStore.getAll(null, excess);
};
putTransaction.onerror = reject;
+ })).then(resolved => {
+ db.close();
+ return resolved;
+ }, error => {
+ db.close();
+ throw error;
}));
}
function evictAccountsByRecords(records) {
- asyncDB.then(db => {
+ return openDB().then(db => {
const transaction = db.transaction(['accounts', 'statuses'], 'readwrite');
const accounts = transaction.objectStore('accounts');
const accountsIdIndex = accounts.index('id');
function evict(toEvict) {
toEvict.forEach(record => {
- asyncCache
+ openCache()
.then(cache => accountAssetKeys.forEach(key => cache.delete(records[key])))
.catch(printErrorIfAvailable);
}
evict(records);
+
+ db.close();
}).catch(printErrorIfAvailable);
}
}
export function evictStatuses(ids) {
- asyncDB.then(db => {
- const store = db.transaction('statuses', 'readwrite').objectStore('statuses');
+ return openDB().then(db => {
+ const transaction = db.transaction('statuses', 'readwrite');
+ const store = transaction.objectStore('statuses');
const idIndex = store.index('id');
const reblogIndex = store.index('reblog');
idIndex.getKey(id).onsuccess =
({ target }) => target.result && store.delete(target.result);
});
+
+ db.close();
}).catch(printErrorIfAvailable);
}
function evictStatusesByRecords(records) {
- evictStatuses(records.map(({ id }) => id));
+ return evictStatuses(records.map(({ id }) => id));
}
-export function putAccounts(records) {
+export function putAccounts(records, avatarStatic) {
+ const avatarKey = avatarStatic ? 'avatar_static' : 'avatar';
const newURLs = [];
put('accounts', records, (newRecord, oldKey, store, oncomplete) => {
const oldURL = target.result[key];
if (newURL !== oldURL) {
- asyncCache
+ openCache()
.then(cache => cache.delete(oldURL))
.catch(printErrorIfAvailable);
}
}, (newRecord, oncomplete) => {
newURLs.push(newRecord[avatarKey]);
oncomplete();
- }).then(records => {
- evictAccountsByRecords(records);
- asyncCache
- .then(cache => cache.addAll(newURLs))
- .catch(printErrorIfAvailable);
+ }).then(records => Promise.all([
+ evictAccountsByRecords(records),
+ openCache().then(cache => cache.addAll(newURLs)),
+ ])).then(freeStorage, error => {
+ freeStorage();
+ throw error;
}).catch(printErrorIfAvailable);
}
.then(evictStatusesByRecords)
.catch(printErrorIfAvailable);
}
+
+export function freeStorage() {
+ return navigator.storage.estimate().then(({ quota, usage }) => {
+ if (usage + storageMargin < quota) {
+ return null;
+ }
+
+ return openDB().then(db => new Promise((resolve, reject) => {
+ const retrieval = db.transaction('accounts', 'readonly').objectStore('accounts').getAll(null, 1);
+
+ retrieval.onsuccess = () => {
+ if (retrieval.result.length > 0) {
+ resolve(evictAccountsByRecords(retrieval.result).then(freeStorage));
+ } else {
+ resolve(caches.delete('mastodon-system'));
+ }
+ };
+
+ retrieval.onerror = reject;
+
+ db.close();
+ }));
+ });
+}