MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    SOLVED Notification between modules

    Development
    4
    10
    1457
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      grasshopper001 last edited by

      I tried to have two modules sending notification to each other: ModuleA sending “TEST_NOTI” to ModuleB, and ModuleB shows the information of this notification on mirror.
      I put ModuleA and ModuleB in different folders named by themselves.
      My code in ModuleA is like:

      start:function(){
             this.count=0;
      },
      notificationReceived:function(notification,payload,sender){
              switch(notification){
                  case "DOM_OBJECTS_CREATED":
                  var timer=setInterval(()=>{
                      this.count++;
                      this.sendNotification("TEST_NOTI",this.count);
                  },1000)
                  break; 
                  default:
                      break;
              }
          },
      

      My code in ModuleB is like:

      getDom:function(){
              var wrapper=document.createElement("p");
              wrapper.className="test results";
              wrapper.id="parent";
              wrapper.innerHTML="test results: ";
              return wrapper;
          },  
          notificationReceived:function(notification,payload,sender){
              var self=this;
              switch(notification){
                  case "DOM_OBJECTS_CREATED": 
                       setInterval(()=>{
                           this.updateDom();
                       },1000)
                        break;
                  case "TEST_NOTI":
                       var parent=document.getElementById("parent");
                       var child=document.createElement("p");
                       child.id="test notification";
                       child.innerHTML="TEST_NOTI received"+payload;
                       parent.appendChild(child);
                       break;
                  default:
                      break;
                  }
          },
      

      I assume that ModuleB will show

      test results:
      TEST_NOTI received [counting]
      

      where [counting] counts from 1 to infinity.
      However, the second line of my assumption only appears occasionally
      . I could hardly find any regulation in its appearance.
      Could anyone help me to figure out whether its a problem within my code or its a problem of the notification connection?
      Are there any place to learn about the notification except the doc?

      idoodler 1 Reply Last reply Reply Quote 0
      • S
        sdetweil last edited by sdetweil

        technically, you are not supposed to update the dom until getDom() method time.
        you can update the data in the notificationReceived method, and then call updateDom() to indicate you have changes to present.

        this will cause getDom() to be called.

        Sam

        Create a working config
        How to add modules

        G 1 Reply Last reply Reply Quote 1
        • S
          sdetweil last edited by sdetweil

          technically, you are not supposed to update the dom until getDom() method time.
          you can update the data in the notificationReceived method, and then call updateDom() to indicate you have changes to present.

          this will cause getDom() to be called.

          Sam

          Create a working config
          How to add modules

          G 1 Reply Last reply Reply Quote 1
          • G
            grasshopper001 @sdetweil last edited by

            @sdetweil but I did call updateDom() in ModuleB…

            1 Reply Last reply Reply Quote 0
            • G
              grasshopper001 last edited by

              finally I solved this problem as @sdetweil told me: keep all coding of DOM within the getDom() function, initialize my data in start function, and update the data in the notificationReceived method.
              the updateDom() function is repeatedly called after receiving “DOM_OBJECT_CREATED” notification.

              S 1 Reply Last reply Reply Quote 0
              • idoodler
                idoodler Module Developer @grasshopper001 last edited by

                @grasshopper001 You are in two different scopes. Try to add .bind(this) to your setInterval’s callback.

                Also do you see any errors on the browser side?

                • Open Chrome and navigate to your MagicMirror’s Webinterface
                • Open the Chrome Dev Tools via F12 (on Windows)
                • Keep an eye out for anything red in the console
                • Optional You can also directly debug your modules code in the dev tools.
                strawberry 3.141 G 2 Replies Last reply Reply Quote 0
                • strawberry 3.141
                  strawberry 3.141 Project Sponsor Module Developer @idoodler last edited by

                  @idoodler he’s using arrow functions =>, therefore he stays in the same scope.

                  Please create a github issue if you need help, so I can keep track

                  idoodler 1 Reply Last reply Reply Quote 1
                  • idoodler
                    idoodler Module Developer @strawberry 3.141 last edited by

                    @strawberry-3-141 Thanks, again what learned:)

                    1 Reply Last reply Reply Quote 1
                    • S
                      sdetweil @grasshopper001 last edited by

                      @grasshopper001 nice work.

                      One comment to think about. You should not call updateDom() if you do NOT have changed content to present

                      This wastes cpu cycles, raising heat. And maybe preventing some other module from displaying properly.

                      Sam

                      Create a working config
                      How to add modules

                      G 1 Reply Last reply Reply Quote 1
                      • G
                        grasshopper001 @sdetweil last edited by

                        @sdetweil Thanks! I’ve rewritten my code according to your comment.

                        1 Reply Last reply Reply Quote 0
                        • G
                          grasshopper001 @idoodler last edited by

                          @idoodler thanks for the help.The console part in the dev tool does help a lot!

                          1 Reply Last reply Reply Quote 1
                          • 1 / 1
                          • First post
                            Last post
                          Enjoying MagicMirror? Please consider a donation!
                          MagicMirror created by Michael Teeuw.
                          Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                          This forum is using NodeBB as its core | Contributors
                          Contact | Privacy Policy