question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it possible to teardown a $watch when immediate is set

See original GitHub issue

What problem does this feature solve?

I use the following pattern to wait for a condition to hold:

const unwatch = this.$watch(function () {
  return this.queue.find((element) => element.shown === false);
}, function (newValue, oldValue) {
  if (!newValue) return;

  unwatch();

  // There is an element in the queue with shown === false.
});

The problem is that this pattern is not possible to use if I also want callback to be called if the condition holds already when $watch is called. This is done by setting immediate to true. But then unwatch is not yet set because $watch has not returned.

I could be checking if the condition holds before creating $watch but this makes the code more complicated and in parts duplicated.

What does the proposed API look like?

In Meteor’s Tracker this is solved by providing access to current computation as an argument to the callback function. Maybe Vue could do the same and provide unwatch as the third argument to the callback (newValue, oldValue, unwatch).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Glandoscommented, Dec 16, 2021

I have to say that I was hit with the same issue. Now my code looks like:

        const condition = () => vxm.isOk(vxm.cameraPaths.paths) && this.sequences.length > 0
        if (condition()) {
          // See https://github.com/vuejs/vue/issues/9501
          this.setNextCameras(true)
        } else {
          const unwatch = this.$watch(condition, (allLoaded, oldLoaded) => {
            this.setNextCameras(allLoaded, oldLoaded, unwatch)
          })
        }

And it’s not really self-explaining on why I must execute the condition manually first. Extending the API of the watch function should not break it, as mentionned.

1reaction
mitarcommented, Feb 15, 2019

The implementation is also very easy. Currently there is:

    const watcher = new Watcher(vm, expOrFn, cb, options)
    if (options.immediate) {
      try {
        cb.call(vm, watcher.value)
      } catch (error) {
        handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
      }
    }
    return function unwatchFn () {
      watcher.teardown()
    }

And in my proposal would be:

    const watcher = new Watcher(vm, expOrFn, cb, options)
    function unwatchFn () {
      watcher.teardown()
    }
    if (options.immediate) {
      try {
        cb.call(vm, watcher.value, undefined, unwatchFn)
      } catch (error) {
        handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
      }
    }
    return unwatchFn

Where is this breaking any API anywhere?

Read more comments on GitHub >

github_iconTop Results From Across the Web

HOW TO RELEASE A LOADED MAINSPRING INSIDE YOUR ...
Please do not forget to subscribe and hit that bell for future notification! ... to produce more educational videos for everyone to watch...
Read more >
Trying to FIX: Faulty 1980s Digital Watch from eBay - YouTube
Hi, in the video I attempt to repair an old quartz digital watch from the 1980s. I purchased it faulty from eBay and...
Read more >
How to Replace a Watch Movement - Watch Repair Guides
A screw off watch back will have large notches around the edges of the back of the watch. Use a Case Opener wrench...
Read more >
Apple Watch Teardown - iFixit
1. Ladies and gentlemen, the Apple Watch has arrived. But before we get down to the brass tacks, here's a quick overview of...
Read more >
Apple Watch Series 4 Teardown - iFixit
1. Apple Watch patient profile: 2. While these black squares may look outwardly similar, X-rays from our experts at Creative Electron reveal radically...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found