No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    computed refresh too often

    Framework
    3
    14
    239
    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.
    • A
      amoss last edited by

      Hi

      I have 2 properties: value1 and value2. I have a computed value:

      divideValues { return value1 / value2}
      

      in html:

      result -> {{ divideValues }}
      

      On a certain event, value1 is changed, then an axios call is made (with value1 as a parameter) and the response changes value2. Meaning, value1 changes value and only after that value2 changes. This is causing the computed to be calculated twice and rapidly. This is causing a flickering in the GUI and if the call is a bit slow, the user sees incorrect values for a short period of time.

      My question: how can I make the refresh only after the 2 values are updated?
      I thought of using a “middle man” property and change both value1 and value2 in the call’s response but that would also cause a double computing, once per valueX change.

      How can I avoid this?
      Thanks

      1 Reply Last reply Reply Quote 0
      • s.molinari
        s.molinari last edited by

        Put it in a method? Or, put the calculation in a watcher for only value 2 and make dividedValues a part of data?

        Scott

        1 Reply Last reply Reply Quote 0
        • A
          amoss last edited by

          It was a method and the same problem happened, I thought a computed will work but it didn’t.
          Where can I read about the watcher for only value2?

          1 Reply Last reply Reply Quote 0
          • A
            amoss last edited by

            ok, found this: https://flaviocopes.com/vue-watchers/
            now the question is whether this is the good practice for this kind of scenario, thanks

            1 Reply Last reply Reply Quote 0
            • s.molinari
              s.molinari last edited by

              The only difference between watchers and computeds is computeds are cached. And since this is waiting for an update from a server anyway and it isn’t a huge calculation, a watcher should be fine.

              Scott

              1 Reply Last reply Reply Quote 0
              • A
                amoss last edited by

                The watcher works great but… Is there a way to watch 2 properties? 🙂
                I have 3 functions (Y changes immediately so no need to watch it)
                1 use X and Y
                2 use X
                3 use X and Z

                1 and 2 can happen by watching X
                but 3 should happen by watching X and Z because sometimes X returned first and sometimes Z.
                How can this be done?
                Thanks

                1 Reply Last reply Reply Quote 0
                • s.molinari
                  s.molinari last edited by

                  Reacting to two props would need a computed property.

                  Scott

                  1 Reply Last reply Reply Quote 0
                  • A
                    amoss last edited by

                    but it will bring me back to square 1 where a change on each property will trigger the computed calculation.
                    Maybe I will move this part to the server as well.

                    1 Reply Last reply Reply Quote 0
                    • T
                      turigeza last edited by turigeza

                      @amoss Watch as many properties as you need to. Calculate your display number which isn’t watched or computed with a function.
                      https://codepen.io/turigeza/pen/MWwOYZE?editors=1010

                      A 1 Reply Last reply Reply Quote 1
                      • A
                        amoss @turigeza last edited by amoss

                        I’m watching the 2 props

                        watch: {
                            prop1: function() {
                                this.getCalc();
                            },
                            prop2: function() {
                                this.getCalc();
                            }
                        },
                        
                            getCalc() {
                                this.calc = prop1 / prop2 * 100;
                            },
                        

                        This works but it works twice, once when prop1 is being updated and once when prop2 is being updated so there’s a short flickering on the screen with a wrong number.

                        I need both props to be updated and only then, call getCalc.
                        Thanks

                        1 Reply Last reply Reply Quote 0
                        • T
                          turigeza last edited by

                          @amoss There is no built in method to achieve that you have to track changes to those variables yourself.

                          If you only truly want to recalculate when both changed then you should have fields like prop1Tainted, prop2Tainted and mark those tainted with your watchers. Only recalculate if they are both tainted / changed.

                          But if you are just worried about the flickering you should debounce your recalculate function.

                          1 Reply Last reply Reply Quote 0
                          • A
                            amoss last edited by

                            English please 🙂
                            What do you mean by “you should debounce your recalculate function”?

                            1 Reply Last reply Reply Quote 0
                            • T
                              turigeza last edited by

                              @amoss It is english : ))

                              Look at Lodash docs …
                              https://lodash.com/docs/4.17.15#debounce
                              Or
                              https://davidwalsh.name/javascript-debounce-function

                              Basically you don’t react to changes immediately but wait x milliseconds to see if any other action happened which effects the outcome.

                              Typically you use it when users type in a textfield you don’t go and react to every single keystroke but wait for a 250 millisecond silence/nonactivity only then would you go and save the field to the server or do what ever you need to.

                              1 Reply Last reply Reply Quote 0
                              • A
                                amoss last edited by

                                Thanks, will check that

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post