-
Notifications
You must be signed in to change notification settings - Fork 1
Implement a computationHandler API, with a cleanup method #4
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
base: hooks
Are you sure you want to change the base?
Conversation
|
I like that approach, plz gimme time to this WE then we can compare your solution with mine and decide. |
# Conflicts: # packages/react-meteor-data/useTracker.js
|
I added some basic tests to the repo. One of the things I'd like to create tests for is to verify some of the computation behavior. It turns out I need this API to do that! |
|
There is an interesting problem with this API, with server-rendering support. The current very simple server implementation doesn't have any kind of computation life cycle at all. I can fake it (or partially re-implement it), but I'm not sure that's sufficient. If I try to use the regular client side implementation, errors are thrown from using the computation. I can bypass the computation for server runs, but then we don't get a computation to send to the life cycle method. I'm really not sure how to handle it for server side code. |
# Conflicts: # packages/react-meteor-data/useTracker.js
|
I ended up working it so that the server still runs through the computation life cycle, but since it doesn't use a computation, simply passes in What do you think? |
…on of reactiveFn on mount, possibly more with suspense
…invocation of reactiveFn on mount, possibly more with suspense" This reverts commit b9d6911.
# Conflicts: # packages/react-meteor-data/useTracker.js
|
Do you think I should test our luck and throw this at a PR? |
|
You can see my approach here https://github.com/risetechnologies/react-packages/blob/dev/packages/react-meteor-data/client/useTracker.client.js#L143-L171 This is WIP and has not been tested at all. Btw. some existing tests fail because If you are interested in testing you can directly check out https://atmospherejs.com/risetechnologies/react-meteor-data |
|
Please don't wonder, I have added some proper linting and up to date project structure. It also seems that tinytest might not be the best tool for this, but I am afraid that jest won't be easy to use within a meteor package. |
|
I actually added a bunch of tests to the mian PR including porting some very old ones from the original repo, and got the remaining stalls finally worked out. Have you taken a look at those? |
|
I'm not sure I understand what problem your handle implementation is solving. Please forgive me if I'm being thick. |
|
To make it clearer, sometimes we have such a case in our codebase: state = { offline: false };
componentDidMount() {
this.computation = Tracker.nonreactive(() =>
Tracker.autorun((computation) => {
const { connected, status } = Meteor.status();
if (status !== 'connecting') {
this.setState({ offline: !connected });
if (connected) computation.stop();
}
})
);
}
componentWillUnmount() {
if (this.computation) {
this.computation.stop();
this.computation = null;
}
}I could use the |
I used them and had a typo in there, d'oh, thanks for setting them up |
|
I'm still not sure why you need to stop the computation when you are offline, but if we pass the computation (or your computation state abstraction) to the reactive function, would that give you what you need? const [offline, setOffline] = useState(false);
useTracker((computation) => {
const { connected, status } = Meteor.status();
if (status !== 'connecting') {
setOffline(!connected);
if (connected) computation.stop();
}
}, []);For re-usability, one of the nice things about hooks is how you can simply create a set of predefined hooks, and use them everywhere. If you wrap the above inside a hook called // or a configurable version
const useStatus = (doStop = true) => {
const [offline, setOffline] = useState(false);
return useTracker((computation) => {
const { connected, status } = Meteor.status();
if (status !== 'connecting') {
setOffline(!connected);
if (doStop && connected) computation.stop();
}
}, []);
};I actually used to do that even with the old |
…m from that level.
# Conflicts: # packages/react-meteor-data/useTracker.js
# Conflicts: # packages/react-meteor-data/useTracker.js
This implements the proposal I described in #2.
My proposal would simply add a third parameter to
useTrackerwhich would run whenever a new computation is created, and pass that reference for the user to handle. We can also have that return a function which would run against the previous instance:To me, exposing the reference to the computation this way this is the most flexible way to do this, without creating a ton of intermediary code, and this withEffect modeled method should be clean and familiar to hooks users.
Since this is completely additive, I don't think we need to hold up the release of a beta for the main hook.
TODOs: