Skip to content

Commit fb931df

Browse files
refactor(main): simplify _updateDom with async/await (MagicMirrorOrg#4185)
`_updateDom` was wrapping its entire body in a `new Promise(resolve => {...})` constructor just to chain `getDom()` and `updateDomWithContent()` together. Since `updateDomWithContent` was already converted to async in MagicMirrorOrg#4182, we can now just `await` it directly - the manual wrapper, the explicit `Promise.resolve()` normalization, and the nested `.then().catch()` chain all become unnecessary, making the control flow easier to follow. Also added `.catch(Log.error)` at both call sites, since async functions reject on error instead of swallowing it silently.
1 parent b9be026 commit fb931df

1 file changed

Lines changed: 19 additions & 34 deletions

File tree

js/main.js

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let modules = [];
88
/**
99
* Create dom objects for all modules that are configured for a specific position.
1010
*/
11-
function createDomObjects () {
11+
async function createDomObjects () {
1212
const domCreationPromises = [];
1313

1414
modules.forEach(function (module) {
@@ -58,19 +58,16 @@ function createDomObjects () {
5858

5959
domCreationPromises.push(domCreationPromise);
6060
domCreationPromise
61-
.then(function () {
62-
_sendNotification("MODULE_DOM_CREATED", null, null, module);
63-
})
61+
.then(
62+
() => _sendNotification("MODULE_DOM_CREATED", null, null, module)
63+
)
6464
.catch(Log.error);
6565
});
6666

6767
updateWrapperStates();
6868

69-
Promise.all(domCreationPromises)
70-
.then(function () {
71-
_sendNotification("DOM_OBJECTS_CREATED");
72-
})
73-
.catch(Log.error);
69+
await Promise.all(domCreationPromises);
70+
_sendNotification("DOM_OBJECTS_CREATED");
7471
}
7572

7673
/**
@@ -166,20 +163,11 @@ async function updateDomWithContent (module, speed, newHeader, newContent, anima
166163
return;
167164
}
168165

169-
await new Promise((resolve) => {
170-
_hideModule(
171-
module,
172-
speed / 2,
173-
function () {
174-
updateModuleContent(module, newHeader, newContent);
175-
if (!module.hidden) {
176-
_showModule(module, speed / 2, null, { animate: animateIn });
177-
}
178-
resolve();
179-
},
180-
{ animate: animateOut }
181-
);
182-
});
166+
await new Promise((resolve) => _hideModule(module, speed / 2, resolve, { animate: animateOut }));
167+
updateModuleContent(module, newHeader, newContent);
168+
if (!module.hidden) {
169+
await new Promise((resolve) => _showModule(module, speed / 2, resolve, { animate: animateIn }));
170+
}
183171
}
184172

185173
/**
@@ -241,7 +229,7 @@ function updateModuleContent (module, newHeader, newContent) {
241229
* Hide the module.
242230
* @param {Module} module The module to hide.
243231
* @param {number} speed The speed of the hide animation.
244-
* @param {Promise} callback Called when the animation is done.
232+
* @param {() => void} callback Called when the animation is done.
245233
* @param {object} [options] Optional settings for the hide method.
246234
*/
247235
function _hideModule (module, speed, callback, options = {}) {
@@ -325,7 +313,7 @@ function _hideModule (module, speed, callback, options = {}) {
325313
* Show the module.
326314
* @param {Module} module The module to show.
327315
* @param {number} speed The speed of the show animation.
328-
* @param {Promise} callback Called when the animation is done.
316+
* @param {() => void} callback Called when the animation is done.
329317
* @param {object} [options] Optional settings for the show method.
330318
*/
331319
function _showModule (module, speed, callback, options = {}) {
@@ -657,7 +645,7 @@ export const MM = {
657645
* @param {Module} module The module that needs an update.
658646
* @param {object|number} [updateOptions] The (optional) number of microseconds for the animation or object with updateOptions (speed/animates)
659647
*/
660-
updateDom (module, updateOptions) {
648+
async updateDom (module, updateOptions) {
661649
if (!(module instanceof Module)) {
662650
Log.error("updateDom: Sender should be a module.");
663651
return;
@@ -669,12 +657,9 @@ export const MM = {
669657
}
670658

671659
// Further implementation is done in the private method.
672-
_updateDom(module, updateOptions)
673-
.then(function () {
674-
// Once the update is complete and rendered, send a notification to the module that the DOM has been updated
675-
_sendNotification("MODULE_DOM_UPDATED", null, null, module);
676-
})
677-
.catch(Log.error);
660+
await _updateDom(module, updateOptions);
661+
// Once the update is complete and rendered, send a notification to the module that the DOM has been updated
662+
_sendNotification("MODULE_DOM_UPDATED", null, null, module);
678663
},
679664

680665
/**
@@ -690,7 +675,7 @@ export const MM = {
690675
* Hide the module.
691676
* @param {Module} module The module to hide.
692677
* @param {number} speed The speed of the hide animation.
693-
* @param {Promise} callback Called when the animation is done.
678+
* @param {() => void} callback Called when the animation is done.
694679
* @param {object} [options] Optional settings for the hide method.
695680
*/
696681
hideModule (module, speed, callback, options) {
@@ -702,7 +687,7 @@ export const MM = {
702687
* Show the module.
703688
* @param {Module} module The module to show.
704689
* @param {number} speed The speed of the show animation.
705-
* @param {Promise} callback Called when the animation is done.
690+
* @param {() => void} callback Called when the animation is done.
706691
* @param {object} [options] Optional settings for the show method.
707692
*/
708693
showModule (module, speed, callback, options) {

0 commit comments

Comments
 (0)