add JsDoc and small code improvements

This commit is contained in:
Thomas Rupprecht 2022-12-24 13:50:28 +01:00
parent e3d6cce769
commit 0476519e99
2 changed files with 54 additions and 25 deletions

View file

@ -1,9 +1,18 @@
import Config from "./config.js";
function fetchJson(url) {
return fetch(url).then((response) => (response.json()));
/**
* @param {string} url
* @returns {Promise<any>}
*/
async function fetchJson(url) {
const response = await fetch(url);
return response.json();
}
/**
* @param {number} days
* @returns {Promise<any>}
*/
function fetchCalendar(days = 28) {
let url = `${Config.calenderUrl}?o=json`;
if (days) {
@ -12,10 +21,16 @@ function fetchCalendar(days = 28) {
return fetchJson(url);
}
/**
* @returns {Promise<any>}
*/
function fetchSpaceApi() {
return fetchJson(Config.spaceApiUrl);
}
/**
* @param {boolean} open
*/
async function updateBadge(open) {
let badgeText, badgeColor;
if (open) {
@ -32,6 +47,9 @@ async function updateBadge(open) {
}
}
/**
* @param {boolean} open
*/
function sendNotification(open) {
browser.notifications.create('status-change', {
type: 'basic',

View file

@ -19,6 +19,9 @@ const doorOpenSVG = `
const dateTimeFormat = Intl.DateTimeFormat([], {dateStyle: "medium", timeStyle: "short"});
/**
* @param {Event} event
*/
async function linkElementClickListener(event) {
try {
await browser.tabs.create({url: event.currentTarget.dataset.url});
@ -45,6 +48,9 @@ async function init() {
}
init();
/**
* @param {object} nextEvents
*/
function updateNextEvent(nextEvents) {
const calendarElement = document.getElementById('calendar');
calendarElement.innerText = '';
@ -54,9 +60,7 @@ function updateNextEvent(nextEvents) {
const strongElement = document.createElement('strong');
strongElement.append(hintNode);
calendarElement.append(strongElement);
return;
}
} else {
const nextEventDate = nextEvents[0].begin.substr(0, 10);
const nextEventDateEvents = nextEvents.filter((nextEvent) => (nextEvent.begin.startsWith(nextEventDate)));
@ -78,8 +82,12 @@ function updateNextEvent(nextEvents) {
}
calendarElement.append(divElement);
});
}
}
/**
* @param {object} spaceApi
*/
function updateSpaceApiJson(spaceApi) {
const spaceApiElement = document.querySelector('#space-api code');
const json = JSON.stringify(spaceApi, null, 2).replace(/ /g, '&nbsp;').replace(/\n/g, '<br/>');
@ -89,6 +97,9 @@ function updateSpaceApiJson(spaceApi) {
// spaceApiElement.append(jsonNode);
}
/**
* @param {object} spaceApi
*/
function updateState(spaceApi) {
const stateElement = document.getElementById('state');
const since = new Date(spaceApi.state.lastchange * 1000);