Skip to content

Commit

Permalink
Remove unused IndexedDB reads
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgleason committed Jul 20, 2021
1 parent de24322 commit 09d7ec3
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 367 deletions.
39 changes: 5 additions & 34 deletions app/soapbox/actions/accounts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import api, { getLinks } from '../api';
import openDB from '../storage/db';
import {
importAccount,
importFetchedAccount,
importFetchedAccounts,
importErrorWhileFetchingAccountByUsername,
Expand Down Expand Up @@ -96,24 +94,6 @@ export const NOTIFICATION_SETTINGS_REQUEST = 'NOTIFICATION_SETTINGS_REQUEST';
export const NOTIFICATION_SETTINGS_SUCCESS = 'NOTIFICATION_SETTINGS_SUCCESS';
export const NOTIFICATION_SETTINGS_FAIL = 'NOTIFICATION_SETTINGS_FAIL';

function getFromDB(dispatch, getState, index, id) {
return new Promise((resolve, reject) => {
const request = index.get(id);

request.onerror = reject;

request.onsuccess = () => {
if (!request.result) {
reject();
return;
}

dispatch(importAccount(request.result));
resolve(request.result.moved && getFromDB(dispatch, getState, index, request.result.moved));
};
});
}

export function createAccount(params) {
return (dispatch, getState) => {
dispatch({ type: ACCOUNT_CREATE_REQUEST, params });
Expand All @@ -138,18 +118,9 @@ export function fetchAccount(id) {

dispatch(fetchAccountRequest(id));

openDB().then(db => getFromDB(
dispatch,
getState,
db.transaction('accounts', 'read').objectStore('accounts').index('id'),
id,
).then(() => db.close(), error => {
db.close();
throw error;
})).catch(() => api(getState).get(`/api/v1/accounts/${id}`).then(response => {
api(getState).get(`/api/v1/accounts/${id}`).then(response => {
dispatch(importFetchedAccount(response.data));
})).then(() => {
dispatch(fetchAccountSuccess());
dispatch(fetchAccountSuccess(response.data));
}).catch(error => {
dispatch(fetchAccountFail(id, error));
});
Expand All @@ -168,8 +139,7 @@ export function fetchAccountByUsername(username) {
api(getState).get(`/api/v1/accounts/${username}`).then(response => {
dispatch(fetchRelationships([response.data.id]));
dispatch(importFetchedAccount(response.data));
}).then(() => {
dispatch(fetchAccountSuccess());
dispatch(fetchAccountSuccess(response.data));
}).catch(error => {
dispatch(fetchAccountFail(null, error));
dispatch(importErrorWhileFetchingAccountByUsername(username));
Expand All @@ -184,9 +154,10 @@ export function fetchAccountRequest(id) {
};
};

export function fetchAccountSuccess() {
export function fetchAccountSuccess(account) {
return {
type: ACCOUNT_FETCH_SUCCESS,
account,
};
};

Expand Down
69 changes: 6 additions & 63 deletions app/soapbox/actions/statuses.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import api from '../api';
import openDB from '../storage/db';
import { evictStatus } from '../storage/modifier';
import { deleteFromTimelines } from './timelines';
import { importFetchedStatus, importFetchedStatuses, importAccount, importStatus } from './importer';
import { importFetchedStatus, importFetchedStatuses } from './importer';
import { openModal } from './modal';
import { isLoggedIn } from 'soapbox/utils/auth';

Expand Down Expand Up @@ -59,48 +57,6 @@ export function createStatus(params, idempotencyKey) {
};
};

function getFromDB(dispatch, getState, accountIndex, index, id) {
return new Promise((resolve, reject) => {
const request = index.get(id);

request.onerror = reject;

request.onsuccess = () => {
const promises = [];

if (!request.result) {
reject();
return;
}

dispatch(importStatus(request.result));

if (getState().getIn(['accounts', request.result.account], null) === null) {
promises.push(new Promise((accountResolve, accountReject) => {
const accountRequest = accountIndex.get(request.result.account);

accountRequest.onerror = accountReject;
accountRequest.onsuccess = () => {
if (!request.result) {
accountReject();
return;
}

dispatch(importAccount(accountRequest.result));
accountResolve();
};
}));
}

if (request.result.reblog && getState().getIn(['statuses', request.result.reblog], null) === null) {
promises.push(getFromDB(dispatch, getState, accountIndex, index, request.result.reblog));
}

resolve(Promise.all(promises));
};
});
}

export function fetchStatus(id) {
return (dispatch, getState) => {
const skipLoading = getState().getIn(['statuses', id], null) !== null;
Expand All @@ -113,31 +69,19 @@ export function fetchStatus(id) {

dispatch(fetchStatusRequest(id, skipLoading));

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).then(() => {
db.close();
}, error => {
db.close();
throw error;
});
}).then(() => {
dispatch(fetchStatusSuccess(skipLoading));
}, () => api(getState).get(`/api/v1/statuses/${id}`).then(response => {
api(getState).get(`/api/v1/statuses/${id}`).then(response => {
dispatch(importFetchedStatus(response.data));
dispatch(fetchStatusSuccess(skipLoading));
})).catch(error => {
dispatch(fetchStatusSuccess(response.data, skipLoading));
}).catch(error => {
dispatch(fetchStatusFail(id, error, skipLoading));
});
};
};

export function fetchStatusSuccess(skipLoading) {
export function fetchStatusSuccess(status, skipLoading) {
return {
type: STATUS_FETCH_SUCCESS,
status,
skipLoading,
};
};
Expand Down Expand Up @@ -173,7 +117,6 @@ export function deleteStatus(id, routerHistory, withRedraft = false) {
dispatch(deleteStatusRequest(id));

api(getState).delete(`/api/v1/statuses/${id}`).then(response => {
evictStatus(id);
dispatch(deleteStatusSuccess(id));
dispatch(deleteFromTimelines(id));

Expand Down
2 changes: 0 additions & 2 deletions app/soapbox/reducers/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import {
ADMIN_REMOVE_PERMISSION_GROUP_REQUEST,
ADMIN_REMOVE_PERMISSION_GROUP_SUCCESS,
ADMIN_REMOVE_PERMISSION_GROUP_FAIL,
} from 'soapbox/actions/admin';
import {
ADMIN_USERS_DELETE_REQUEST,
ADMIN_USERS_DELETE_FAIL,
ADMIN_USERS_DEACTIVATE_REQUEST,
Expand Down
31 changes: 1 addition & 30 deletions app/soapbox/service_worker/entry.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// import { freeStorage, storageFreeable } from '../storage/modifier';
import './web_push_notifications';

// function openSystemCache() {
// return caches.open('soapbox-system');
// }

function openWebCache() {
return caches.open('soapbox-web');
}
Expand All @@ -13,9 +8,6 @@ function fetchRoot() {
return fetch('/', { credentials: 'include', redirect: 'manual' });
}

// const firefox = navigator.userAgent.match(/Firefox\/(\d+)/);
// const invalidOnlyIfCached = firefox && firefox[1] < 60;

// Cause a new version of a registered Service Worker to replace an existing one
// that is already installed, and replace the currently active worker on open pages.
self.addEventListener('install', function(event) {
Expand Down Expand Up @@ -81,26 +73,5 @@ self.addEventListener('fetch', function(event) {
return response;
},
() => asyncCache.then(cache => cache.match('/'))));
} /* else if (storageFreeable && (ATTACHMENT_HOST ? url.host === ATTACHMENT_HOST : url.pathname.startsWith('/system/'))) {
event.respondWith(openSystemCache().then(cache => {
return cache.match(event.request.url).then(cached => {
if (cached === undefined) {
const asyncResponse = invalidOnlyIfCached && event.request.cache === 'only-if-cached' ?
fetch(event.request, { cache: 'no-cache' }) : fetch(event.request);
return asyncResponse.then(response => {
if (response.ok) {
cache
.put(event.request.url, response.clone())
.catch(()=>{}).then(freeStorage()).catch();
}
return response;
});
}
return cached;
});
}));
} */
}
});
27 changes: 0 additions & 27 deletions app/soapbox/storage/db.js

This file was deleted.

Loading

0 comments on commit 09d7ec3

Please sign in to comment.