@@ -508,43 +508,37 @@ module.exports = NodeHelper.create({
508508 } ,
509509
510510 handleGetModules ( query , response ) {
511- if ( ! this . checkInitialized ( response ) ) { return ; }
512- this . callAfterUpdate ( ( ) => {
511+ this . requireLiveState ( response , ( ) => {
513512 this . sendResponse ( response , undefined , { query, data : this . configData . moduleData } ) ;
514513 } ) ;
515514 } ,
516515
517516 handleGetBrightness ( query , response ) {
518- if ( ! this . checkInitialized ( response ) ) { return ; }
519- this . callAfterUpdate ( ( ) => {
517+ this . requireLiveState ( response , ( ) => {
520518 this . sendResponse ( response , undefined , { query, result : this . configData . brightness } ) ;
521519 } ) ;
522520 } ,
523521
524522 handleGetTemp ( query , response ) {
525- if ( ! this . checkInitialized ( response ) ) { return ; }
526- this . callAfterUpdate ( ( ) => {
523+ this . requireLiveState ( response , ( ) => {
527524 this . sendResponse ( response , undefined , { query, result : this . configData . temp } ) ;
528525 } ) ;
529526 } ,
530527
531528 handleGetZoom ( query , response ) {
532- if ( ! this . checkInitialized ( response ) ) { return ; }
533- this . callAfterUpdate ( ( ) => {
529+ this . requireLiveState ( response , ( ) => {
534530 this . sendResponse ( response , undefined , { query, result : this . configData . zoom } ) ;
535531 } ) ;
536532 } ,
537533
538534 handleGetBackgroundColor ( query , response ) {
539- if ( ! this . checkInitialized ( response ) ) { return ; }
540- this . callAfterUpdate ( ( ) => {
535+ this . requireLiveState ( response , ( ) => {
541536 this . sendResponse ( response , undefined , { query, result : this . configData . backgroundColor } ) ;
542537 } ) ;
543538 } ,
544539
545540 handleGetFontColor ( query , response ) {
546- if ( ! this . checkInitialized ( response ) ) { return ; }
547- this . callAfterUpdate ( ( ) => {
541+ this . requireLiveState ( response , ( ) => {
548542 this . sendResponse ( response , undefined , { query, result : this . configData . fontColor } ) ;
549543 } ) ;
550544 } ,
@@ -604,14 +598,36 @@ module.exports = NodeHelper.create({
604598
605599 callAfterUpdate ( callback , timeout = 3000 ) {
606600 let isDone = false ;
607- const once = ( ) => {
601+ const once = ( didUpdate ) => {
608602 if ( isDone ) { return ; }
609603 isDone = true ;
610- callback ( ) ;
604+ callback ( didUpdate ) ;
611605 } ;
612- this . waiting . push ( { run : once } ) ;
606+ this . waiting . push ( { run : ( ) => once ( true ) } ) ;
613607 this . sendSocketNotification ( "UPDATE" ) ;
614- setTimeout ( once , timeout ) ;
608+ setTimeout ( ( ) => once ( false ) , timeout ) ;
609+ } ,
610+
611+ /**
612+ * Serve a request that depends on live frontend state.
613+ *
614+ * Instead of relying on a boot-time notification having been received, this
615+ * actively pulls a fresh CURRENT_STATUS from the frontend (via callAfterUpdate).
616+ * It is self-healing: even if the frontend missed DOM_OBJECTS_CREATED at
617+ * startup, the triggered UPDATE makes it resend its state. An error is only
618+ * returned when no state can be obtained at all (no browser connected).
619+ * @param {object } response - Express or socket response object
620+ * @param {() => void } callback - Invoked once live state is available
621+ * @returns {void }
622+ */
623+ requireLiveState ( response , callback ) {
624+ this . callAfterUpdate ( ( didUpdate ) => {
625+ if ( didUpdate || this . initialized ) {
626+ callback ( ) ;
627+ } else {
628+ this . sendResponse ( response , "Not connected to the MagicMirror² frontend. Open or refresh a browser pointing at the mirror." ) ;
629+ }
630+ } ) ;
615631 } ,
616632
617633 delayedQuery ( query , response ) {
@@ -1058,13 +1074,14 @@ module.exports = NodeHelper.create({
10581074 case "CURRENT_STATUS" :
10591075 this . configData = payload ;
10601076 this . thisConfig = payload . remoteConfig ;
1061- if ( this . initialized ) {
1062- for ( const o of this . waiting ) { o . run ( ) ; }
1063- this . waiting = [ ] ;
1064- } else {
1065- // Do anything else required to initialize
1066- this . initialized = true ;
1067- }
1077+ this . initialized = true ;
1078+
1079+ /*
1080+ * Always drain requests waiting for fresh frontend state. The pull is
1081+ * idempotent, so a missed boot-time notification is no longer fatal.
1082+ */
1083+ for ( const o of this . waiting ) { o . run ( ) ; }
1084+ this . waiting = [ ] ;
10681085
10691086 break ;
10701087 case "REQUEST_DEFAULT_SETTINGS" :
0 commit comments