-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How does watch function work? #77
Comments
Good question. However, the halting problem need not apply since we actually run the code — there is no static analysis or deterministic algorithm. Conceptually this is not that complicated (it is also, by the way, how signals work and how vue works — it is a clever trick though), in your example: function total () {
if (data.logTotal == ifMathTheoremIsTrue()) {
console.log(`Total: ${data.price * data.quantity}`);
}
} we start by running the function and observing which properties are touched. Given a state of: data = {
logTotal: 1,
price: 1,
quantity: 10
} And given that ifMathTheoremIsTrue() has no dependencies on
The only time we need to re-run this function are if any of those dependencies are changed. Lets say
we can now diff the dependency results:
Hope that helps clear up how this works 👍 |
Ah, that's very helpful! I also had this question. When I first read the documentation it seemed like magic, but now I see it just tracks the dependencies from each run. If |
From the docs:
However, the watcher also detects when a dependency is no longer being used by a function and turns $off tracking for those properties.
In our example, this means that when logTotal is false it will not call our total() function again until data.logTotal is set back to true.
The example function in the doc was:
But what if the function was:
Isn't knowing when a dependency is used by an arbitrary function reducible to the Halting Problem?
So when can the watcher detect changes, and when can it not?
The text was updated successfully, but these errors were encountered: