Skip to content

Commit

Permalink
client: Use method definition syntax
Browse files Browse the repository at this point in the history
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions

> Method definition is a shorter syntax for defining a function property in an object initializer.

It will make the diffs smaller when we migrate to classes.
  • Loading branch information
jtojnar committed Jul 30, 2023
1 parent 408df91 commit cdc7f3e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
34 changes: 17 additions & 17 deletions client/js/selfoss-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const selfoss = {
/**
* initialize application
*/
init: async function() {
async init() {
// Load off-line mode enabledness.
selfoss.db.enableOffline.update(window.localStorage.getItem('enableOffline') === 'true');

Expand Down Expand Up @@ -75,7 +75,7 @@ const selfoss = {
},


initMain: async function(configuration) {
async initMain(configuration) {
selfoss.config = configuration;

if (selfoss.db.enableOffline.value) {
Expand Down Expand Up @@ -120,7 +120,7 @@ const selfoss = {
/**
* Create basic DOM structure of the page.
*/
attachApp: function(configuration) {
attachApp(configuration) {
document.getElementById('js-loading-message')?.remove();

const mainUi = document.createElement('div');
Expand All @@ -145,27 +145,27 @@ const selfoss = {
loggedin: new ValueListenable(false),


setSession: function() {
setSession() {
window.localStorage.setItem('onlineSession', true);
selfoss.loggedin.update(true);
},


clearSession: function() {
clearSession() {
window.localStorage.removeItem('onlineSession');
selfoss.loggedin.update(false);
},


hasSession: function() {
hasSession() {
return selfoss.loggedin.value;
},

/**
* Try to log in using given credentials
* @return Promise<undefined>
*/
login: function({ configuration, username, password, enableOffline }) {
login({ configuration, username, password, enableOffline }) {
selfoss.db.enableOffline.update(enableOffline);
window.localStorage.setItem('enableOffline', selfoss.db.enableOffline.value);
if (!selfoss.db.enableOffline.value) {
Expand Down Expand Up @@ -198,7 +198,7 @@ const selfoss = {
});
},

setupServiceWorker: function() {
setupServiceWorker() {
if (!('serviceWorker' in navigator) || selfoss.serviceWorkerInitialized) {
return;
}
Expand All @@ -224,7 +224,7 @@ const selfoss = {
});
},

logout: async function() {
async logout() {
selfoss.clearSession();

selfoss.db.clear(); // will not work after a failure, since storage is nulled
Expand Down Expand Up @@ -295,7 +295,7 @@ const selfoss = {
*
* @return true if device resolution smaller equals 1024
*/
isMobile: function() {
isMobile() {
// first check useragent
if ((/iPhone|iPod|iPad|Android|BlackBerry/).test(navigator.userAgent)) {
return true;
Expand All @@ -311,7 +311,7 @@ const selfoss = {
*
* @return true if device resolution smaller equals 1024
*/
isTablet: function() {
isTablet() {
if (document.body.clientWidth <= 1024) {
return true;
}
Expand All @@ -324,7 +324,7 @@ const selfoss = {
*
* @return true if device resolution smaller equals 1024
*/
isSmartphone: function() {
isSmartphone() {
if (document.body.clientWidth <= 640) {
return true;
}
Expand All @@ -351,7 +351,7 @@ const selfoss = {
* @param {Number} new unread stats
* @param {Number} new starred stats
*/
refreshStats: function(all, unread, starred) {
refreshStats(all, unread, starred) {
selfoss.app.setAllItemsCount(all);
selfoss.app.setStarredItemsCount(starred);

Expand All @@ -365,7 +365,7 @@ const selfoss = {
* @return void
* @param {Number} new unread stats
*/
refreshUnread: function(unread) {
refreshUnread(unread) {
selfoss.app.setUnreadItemsCount(unread);
},

Expand All @@ -375,7 +375,7 @@ const selfoss = {
*
* @return void
*/
reloadTags: function() {
reloadTags() {
selfoss.app.setTagsState(LoadingState.LOADING);

getAllTags().then((data) => {
Expand All @@ -388,7 +388,7 @@ const selfoss = {
},


handleAjaxError: function(error, tryOffline = true) {
handleAjaxError(error, tryOffline = true) {
if (!(error instanceof HttpError || error instanceof TimeoutError)) {
return Promise.reject(error);
}
Expand All @@ -403,7 +403,7 @@ const selfoss = {
},


listenWaitingSW: function(reg, callback) {
listenWaitingSW(reg, callback) {
const awaitStateChange = () => {
reg.installing.addEventListener('statechange', (event) => {
if (event.target.state === 'installed') {
Expand Down
34 changes: 17 additions & 17 deletions client/js/selfoss-db-offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ selfoss.dbOffline = {
shouldLoadEntriesOnline: false,
olderEntriesOnline: false,

_tr: function(...args) {
_tr(...args) {
return selfoss.db.storage.transaction(...args)
.catch((error) => {
selfoss.app.showError(
Expand All @@ -37,7 +37,7 @@ selfoss.dbOffline = {
},


init: function() {
init() {
if (!selfoss.db.enableOffline.value || selfoss.db.storage) {
return;
}
Expand Down Expand Up @@ -129,7 +129,7 @@ selfoss.dbOffline = {
},


_memLastItemId: function() {
_memLastItemId() {
return selfoss.db.storage.entries.orderBy('id').reverse().first((entry) => {
if (entry) {
selfoss.dbOffline.lastItemId = entry.id;
Expand All @@ -140,7 +140,7 @@ selfoss.dbOffline = {
},


storeEntries: function(entries) {
storeEntries(entries) {
return selfoss.dbOffline._tr(
'rw',
selfoss.db.storage.entries,
Expand All @@ -158,7 +158,7 @@ selfoss.dbOffline = {
},


GCEntries: function(more = false) {
GCEntries(more = false) {
if (more) {
// We need to garbage collect more, as the browser storage limit
// seems to be exceeded: decrease the amount of days entries are
Expand Down Expand Up @@ -211,7 +211,7 @@ selfoss.dbOffline = {
},


storeStats: function(stats) {
storeStats(stats) {
return selfoss.dbOffline._tr('rw', selfoss.db.storage.stats, () => {
for (const [name, value] of Object.entries(stats)) {
selfoss.db.storage.stats.put({
Expand All @@ -223,7 +223,7 @@ selfoss.dbOffline = {
},


storeLastUpdate: function(lastUpdate) {
storeLastUpdate(lastUpdate) {
return selfoss.dbOffline._tr('rw', selfoss.db.storage.stamps, () => {
if (lastUpdate) {
selfoss.db.storage.stamps.put({
Expand All @@ -235,7 +235,7 @@ selfoss.dbOffline = {
},


getEntries: function(fetchParams) {
getEntries(fetchParams) {
let hasMore = false;
return selfoss.dbOffline._tr(
'r',
Expand Down Expand Up @@ -310,7 +310,7 @@ selfoss.dbOffline = {
},


reloadOnlineStats: function() {
reloadOnlineStats() {
return selfoss.dbOffline._tr(
'r',
selfoss.db.storage.stats,
Expand All @@ -328,7 +328,7 @@ selfoss.dbOffline = {
},


refreshStats: function() {
refreshStats() {
return selfoss.dbOffline._tr(
'r',
selfoss.db.storage.entries,
Expand All @@ -353,7 +353,7 @@ selfoss.dbOffline = {
},


enqueueStatuses: function(statuses) {
enqueueStatuses(statuses) {
if (statuses) {
selfoss.dbOffline.needsSync = true;
}
Expand All @@ -376,7 +376,7 @@ selfoss.dbOffline = {
},


enqueueStatus: function(entryId, statusName, statusValue) {
enqueueStatus(entryId, statusName, statusValue) {
return selfoss.dbOffline.enqueueStatuses([{
entryId: entryId,
name: statusName,
Expand All @@ -385,7 +385,7 @@ selfoss.dbOffline = {
},


sendNewStatuses: function() {
sendNewStatuses() {
selfoss.db.storage.statusq.toArray().then(statuses => {
return statuses.map(s => {
const statusUpdate = {
Expand All @@ -407,7 +407,7 @@ selfoss.dbOffline = {
},


storeEntryStatuses: function(itemStatuses, dequeue = false, updateStats = true) {
storeEntryStatuses(itemStatuses, dequeue = false, updateStats = true) {
return selfoss.dbOffline._tr(
'rw',
selfoss.db.storage.entries,
Expand Down Expand Up @@ -470,7 +470,7 @@ selfoss.dbOffline = {
},


entriesMark: function(itemIds, unread) {
entriesMark(itemIds, unread) {
selfoss.dbOnline.statsDirty = true;
const newStatuses = itemIds.map((itemId) => {
return {id: itemId, unread: unread};
Expand All @@ -479,12 +479,12 @@ selfoss.dbOffline = {
},


entryMark: function(itemId, unread) {
entryMark(itemId, unread) {
return selfoss.dbOffline.entriesMark([itemId], unread);
},


entryStar: function(itemId, starred) {
entryStar(itemId, starred) {
return selfoss.dbOffline.storeEntryStatuses([{
id: itemId,
starred: starred
Expand Down
8 changes: 4 additions & 4 deletions client/js/selfoss-db-online.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ selfoss.dbOnline = {
firstSync: true,


_syncBegin: function() {
_syncBegin() {
if (!selfoss.dbOnline.syncing.promise) {
selfoss.dbOnline.syncing.promise = new Promise((resolve, reject) => {
selfoss.dbOnline.syncing.resolve = resolve;
Expand Down Expand Up @@ -52,7 +52,7 @@ selfoss.dbOnline = {
},


_syncDone: function(success = true) {
_syncDone(success = true) {
if (selfoss.dbOnline.syncing.promise) {
if (success) {
selfoss.dbOnline.syncing.resolve();
Expand All @@ -72,7 +72,7 @@ selfoss.dbOnline = {
*
* @return Promise
*/
sync: function(updatedStatuses, chained) {
sync(updatedStatuses, chained) {
if (selfoss.dbOnline.syncing.promise && !chained) {
if (updatedStatuses) {
// Ensure the status queue is not cleared and gets sync'ed at
Expand Down Expand Up @@ -230,7 +230,7 @@ selfoss.dbOnline = {
*
* @return void
*/
getEntries: function(fetchParams, abortController) {
getEntries(fetchParams, abortController) {
return itemsRequests.getItems({
...fetchParams,
itemsPerPage: selfoss.config.itemsPerPage
Expand Down
14 changes: 7 additions & 7 deletions client/js/selfoss-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ selfoss.db = {
lastUpdate: null,


setOnline: function() {
setOnline() {
if (!selfoss.db.online) {
selfoss.db.online = true;
selfoss.db.sync();
Expand All @@ -40,12 +40,12 @@ selfoss.db = {
},


tryOnline: function() {
tryOnline() {
return selfoss.db.sync(true);
},


setOffline: function() {
setOffline() {
if (selfoss.db.storage && !selfoss.db.broken) {
selfoss.dbOnline._syncDone(false);
selfoss.db.online = false;
Expand All @@ -59,7 +59,7 @@ selfoss.db = {
},


clear: function() {
clear() {
if (selfoss.db.storage) {
window.localStorage.removeItem('offlineDays');
const clearing = selfoss.db.storage.delete();
Expand All @@ -72,20 +72,20 @@ selfoss.db = {
},


isValidTag: function(name) {
isValidTag(name) {
return selfoss.app.state.tags.length === 0 || selfoss.app.state.tags.find((tag) => tag.tag === name) !== undefined;
},


isValidSource: function(id) {
isValidSource(id) {
return selfoss.app.state.sources.length === 0 || selfoss.app.state.sources.find((source) => source.id === id) !== undefined;
},


lastSync: null,


sync: function(force = false) {
sync(force = false) {
const lastUpdateIsOld = selfoss.db.lastUpdate === null || selfoss.db.lastSync === null || Date.now() - selfoss.db.lastSync > 5 * 60 * 1000;
const shouldSync = force || selfoss.dbOffline.needsSync || lastUpdateIsOld;
if (selfoss.isAllowedToRead() && selfoss.isOnline() && shouldSync) {
Expand Down

0 comments on commit cdc7f3e

Please sign in to comment.