Skip to content

Commit b9be026

Browse files
refactor(main): simplify _updateDom with async/await (#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 #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 ca7b752 commit b9be026

1 file changed

Lines changed: 31 additions & 41 deletions

File tree

js/main.js

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ function createDomObjects () {
6666

6767
updateWrapperStates();
6868

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

7476
/**
@@ -108,44 +110,30 @@ function _sendNotification (notification, payload, sender, sendTo) {
108110
* @param {Module} module The module that needs an update.
109111
* @param {object|number} [updateOptions] The (optional) number of microseconds for the animation or object with updateOptions (speed/animates)
110112
* @param {boolean} [createAnimatedDom] for displaying only animateIn (used on first start of MagicMirror)
111-
* @returns {Promise} Resolved when the dom is fully updated.
113+
* @returns {Promise<void>} Resolved when the dom is fully updated.
112114
*/
113-
function _updateDom (module, updateOptions, createAnimatedDom = false) {
114-
return new Promise(function (resolve) {
115-
let speed = updateOptions;
116-
let animateOut = null;
117-
let animateIn = null;
118-
if (typeof updateOptions === "object") {
119-
if (typeof updateOptions.options === "object" && updateOptions.options.speed !== undefined) {
120-
speed = updateOptions.options.speed;
121-
Log.debug(`updateDom: ${module.identifier} Has speed in object: ${speed}`);
122-
if (typeof updateOptions.options.animate === "object") {
123-
animateOut = updateOptions.options.animate.out;
124-
animateIn = updateOptions.options.animate.in;
125-
Log.debug(`updateDom: ${module.identifier} Has animate in object: out->${animateOut}, in->${animateIn}`);
126-
}
127-
} else {
128-
Log.debug(`updateDom: ${module.identifier} Has no speed in object`);
129-
speed = 0;
115+
async function _updateDom (module, updateOptions, createAnimatedDom = false) {
116+
let speed = updateOptions;
117+
let animateOut = null;
118+
let animateIn = null;
119+
if (typeof updateOptions === "object") {
120+
if (typeof updateOptions.options === "object" && updateOptions.options.speed !== undefined) {
121+
speed = updateOptions.options.speed;
122+
Log.debug(`updateDom: ${module.identifier} Has speed in object: ${speed}`);
123+
if (typeof updateOptions.options.animate === "object") {
124+
animateOut = updateOptions.options.animate.out;
125+
animateIn = updateOptions.options.animate.in;
126+
Log.debug(`updateDom: ${module.identifier} Has animate in object: out->${animateOut}, in->${animateIn}`);
130127
}
128+
} else {
129+
Log.debug(`updateDom: ${module.identifier} Has no speed in object`);
130+
speed = 0;
131131
}
132+
}
132133

133-
const newHeader = module.getHeader();
134-
let newContentPromise = module.getDom();
135-
136-
if (!(newContentPromise instanceof Promise)) {
137-
// convert to a promise if not already one to avoid if/else's everywhere
138-
newContentPromise = Promise.resolve(newContentPromise);
139-
}
140-
141-
newContentPromise
142-
.then(function (newContent) {
143-
const updatePromise = updateDomWithContent(module, speed, newHeader, newContent, animateOut, animateIn, createAnimatedDom);
144-
145-
updatePromise.then(resolve).catch(Log.error);
146-
})
147-
.catch(Log.error);
148-
});
134+
const newHeader = module.getHeader();
135+
const newContent = await module.getDom();
136+
await updateDomWithContent(module, speed, newHeader, newContent, animateOut, animateIn, createAnimatedDom);
149137
}
150138

151139
/**
@@ -681,10 +669,12 @@ export const MM = {
681669
}
682670

683671
// Further implementation is done in the private method.
684-
_updateDom(module, updateOptions).then(function () {
685-
// Once the update is complete and rendered, send a notification to the module that the DOM has been updated
686-
_sendNotification("MODULE_DOM_UPDATED", null, null, module);
687-
});
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);
688678
},
689679

690680
/**

0 commit comments

Comments
 (0)