Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions packages/react-meteor-data/useTracker.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,41 +220,6 @@ if (Meteor.isClient) {
completed();
});

Tinytest.addAsync('useTracker - basic track', async function (test, completed) {
var container = document.createElement("DIV");

var x = new ReactiveVar('aaa');

var Foo = () => {
const data = useTracker(() => {
return {
x: x.get()
};
}, []);
return <span>{data.x}</span>;
};

ReactDOM.render(<Foo/>, container);
test.equal(getInnerHtml(container), '<span>aaa</span>');

x.set('bbb');
await waitFor(() => {
Tracker.flush({_throwFirstError: true});
}, { container, timeout: 250 });

test.equal(getInnerHtml(container), '<span>bbb</span>');

test.equal(x._numListeners(), 1);

await waitFor(() => {
ReactDOM.unmountComponentAtNode(container);
}, { container, timeout: 250 });

test.equal(x._numListeners(), 0);

completed();
});

// Make sure that calling ReactDOM.render() from an autorun doesn't
// associate that autorun with the mixin's autorun. When autoruns are
// nested, invalidating the outer one stops the inner one, unless
Expand Down
13 changes: 9 additions & 4 deletions packages/react-meteor-data/useTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ const useTrackerNoDeps = <T = any>(reactiveFn: IReactiveFn<T>) => {
const useTrackerWithDeps = <T = any>(reactiveFn: IReactiveFn<T>, deps: DependencyList): T => {
let [data, setData] = useState<T>();

const { current: refs } = useRef({ reactiveFn });
refs.reactiveFn = reactiveFn;

useMemo(() => {
// To jive with the lifecycle interplay between Tracker/Subscribe, run the
// reactive function in a computation, then stop it, to force flush cycle.
const comp = Tracker.nonreactive(
() => Tracker.autorun((c: Tracker.Computation) => {
if (c.firstRun) data = reactiveFn();
if (c.firstRun) data = refs.reactiveFn();
})
);
// To avoid creating side effects in render, stop the computation immediately
Expand All @@ -122,9 +125,11 @@ const useTrackerWithDeps = <T = any>(reactiveFn: IReactiveFn<T>, deps: Dependenc
}, deps);

useEffect(() => {
const computation = Tracker.autorun((c) => {
setData(reactiveFn(c));
});
const computation = Tracker.nonreactive(
() => Tracker.autorun((c) => {
setData(refs.reactiveFn(c));
})
);
return () => {
computation.stop();
}
Expand Down