Comments (15)
Hey @tmeasday, sorry for the late reply.
Can you point me to the code where the problem is happening? Like the original post on meteor-core where the user described which helper is being re-run multiple times?
from todos.
Hey @avital, do you mean how do you know if the problem is happening?
If you add a console.log()
to the checkedClass()
helper in todos-item.js:14
, you'll get a console each time a todo is rendered.
The repro is simply:
- Open a list (notice N logs)
- Check an item (notice another N logs).
If you open the version of todos in https://github.com/meteor/meteor, and do the equivalent logging, you'll see:
- Open a list (notice N logs)
- Check an item (notice 1 log).
I was going to look at this today too if I have time so if you make any headway/have any ideas, please post them here.
from todos.
Like the original post on meteor-core where the user described which helper is being re-run multiple times?
I did link to it above, here it is again: https://groups.google.com/forum/#!searchin/meteor-core/Template$20helpers$20re-evaluate$20for$20each$20item/meteor-core/TZpUvWjaLO4/KN2xklayTlAJ
from todos.
Thanks, I meant where in the code I need to log to see the behavior (the checkedClass()
helper you referred to)
I'll look into it
from todos.
I found this fact:
In lists-show.js, when changing:
todos(listId) {
return Lists.findOne(listId).todos();
},
to
todos(listId) {
return Todos.find({listId: listId});
},
the problem goes away.
I guess that's because Lists.findOne(listId)
registers a dependency on the entire list document, which includes incompleteCount.
[...]
Yes.
You can solve this by changing that line to return Lists.findOne(listId, {fields: {_id: 1}}).todos();
from todos.
I remember at some point I tried to get around this problem by converting all properties to getter functions and querying Minimongo separately for every single field on the object, but I think it created too much overhead. Presumably GraphQL would solve the query aspect of this if we could implement fine-grained data tracking.
from todos.
Thanks @avital, that's clearly it. Appreciate it.
from todos.
Actually I'm not seeing that behaviour @avital.
I need to actually change the todoArgs
helper to look like:
todoArgs(todo) {
const instance = Tracker.nonreactive(() => Template.instance());
return {
todo,
editing: instance.state.equals('editingTodo', todo._id),
onEdit(doEdit) {
instance.state.set('editingTodo', doEdit ? todo._id : false);
}
};
}
In order to see the "correct" behaviour.
The issue is that that second helper adds a depedency on Template.instance()
, which creates a dependency on the instance's .data
(which is the full list in this case).
@stubailo this is a problem actually!!! It means our habit of aggressively calling Template.instance()
in Blaze helpers means that we easily can't do performance optimizations like this within the template.
Well actually we can, we just have to wrap Template.instance()
in a Tracker.nonreactive()
. But it's starting to get pretty obtuse. I dunno, maybe it's fine.
from todos.
Also, for what it's worth, this issue pretty neatly highlights most of the issues I have with Tracker.
- As an experienced Blaze/Tracker user, I still had a lot of trouble figuring out why the helper was getting re-called. [1]
- It's in no way obvious (via documentation or any mechanism to trace it) that
Template.instance()
adds a dependency on the instance's data. Maybe you could intuit it or something, but there needs to be some way to definitively find out. - We don't have good tools (apart from the sledgehammer of
Tracker.nonreactive
) to change the reactivity properties of a data source likeTemplate.instance()
. There are some community tools for that though (like computed-field)
[1] I know a few tricks like using a console.trace()
inside a Tracker.onInvalidate()
, but due to the source of the invalidation being a generic "Blaze changed the data context of something (who knows what?)", I basically had to binary search debug by commenting stuff out.
from todos.
@tmeasday soooo do we need to recommend a pattern for calling Template.instance()
non-reactively? Wow, it really sucks that Template.instance().data
exists at all - it's 100% a duplicate of Template.currentData()
.
from todos.
I think we don't need to recommend a pattern -- in general if you are calling Template.instance()
in a helper, you are just setting the same dependency you already have with this
, right?
It's just special cases like this, where you are trying to be tricky to control reactivity where it's good to know this. I think this example actually makes for an interesting little section of the Blaze guide to explain some of this.
from todos.
in general if you are calling Template.instance() in a helper, you are just setting the same dependency you already have with this, right?
That's not correct - this
is the current data context, Template.instance()
is the data context of the template inclusion. Could be different.
from todos.
Yeah, that's what I mean by "in general" -- like it's unusual for it not to be.
If you are using {{#each in}}
and {{#let}}
, (and not {{#each}}
and {{#with}}
) then it'd always be the same, no?
from todos.
Yes, that's true. The extension of that project was going to be eliminating the data context entirely and passing template args through lexical scope, in which case the template wouldn't actually re-render because of a change in arguments - it would just get the new variable values. So I believe that would fix a lot of stuff here.
from todos.
See #56
from todos.
Related Issues (20)
- Apollo branch needs to be updated to reflect current Apollo project. HOT 5
- Any interest in a Vue port? HOT 17
- Cannot serve the built app through an ad-hoc local network
- Update React Todo to use TypeScript since PropTypes deprecation HOT 1
- replace createContainer with withTracker HOT 1
- CSS Import?
- http://todos.mysite.com redirect to https://todos.mysite.com HOT 1
- Current version won't start (last commit 59f6621 on 27 Apr 2017) HOT 1
- Add Server Side Rendering and Dynamic Imports to React To Dos HOT 1
- Why are collections not imported or defined in the register-api / api.js file ? Where are they imported and defined? HOT 3
- Where are collections defined and imported? HOT 1
- Why the code looks so messy? HOT 2
- ant-design branch ready to pull HOT 2
- Uncaught Error: Target container is not a DOM element. HOT 6
- Error Connection Websocket
- Cannot find sockjs HOT 1
- React branch update with hooks - New react/typescript branch? HOT 5
- todos example doesn't run, error in meteor npm i
- german language HOT 1
- Official Example doesn't update to latest version 1.11.x HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from todos.