Skip to content

autorun no longer implicitly re-running on state change #15

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 38 additions & 10 deletions lib/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ else Tracker = Package['tracker'].Tracker;
Tracker.Component = class extends React.Component {
constructor(props) {
super(props);
this.__subs = {}, this.__comps = []; this.__live = false;
this.__subs = {}, this.__comps = {}; this.__allcomps = []; this.__live = false;
this.__subscribe = props && props.subscribe || Meteor.subscribe;
}

Expand All @@ -15,28 +15,56 @@ Tracker.Component = class extends React.Component {
this.__subscribe.apply(this, [name, ...options]);
}

autorun(fn) { return this.__comps.push(Tracker.autorun(c => {
this.__live = true; fn(c); this.__live = false;
}))}
autorun(fn) {
this.__stateKeys = [];
let c = Tracker.autorun(c => {
this.__live = true; fn(c); this.__live = false;
})
this.__allcomps.push(c);
this.__stateKeys.forEach(stateKey => {
this.__comps[stateKey] = [...this.__comps[stateKey],c];
});

componentDidUpdate() { !this.__live && this.__comps.forEach(c => {
c.invalidated = c.stopped = false; !c.invalidate();
})}
this.__stateKeys = null;
}

getState(stateKey) {
this.__stateKeys.push(stateKey);
return this.state[stateKey];
}

componentDidUpdate(prevProps) {
// Always invalidate when props are changed
// Assuming that the reference will change when the props change!
if (prevProps!==this.props) {
this.__allcomps.forEach(c => {
c.invalidate();
});
}
}

subscriptionsReady() {
return !Object.keys(this.__subs).some(id => !this.__subs[id].ready());
}

setState(state) {
if (!this._reactInternalInstance)
Object.keys(state).forEach((stateKey) =>
this.__comps[stateKey] && this.__comps[stateKey]
.forEach(c => c.invalidate())
);

if (!this._reactInternalInstance) {
return this.state = Object.assign({}, this.state, state);
else
} else {
return super.setState.apply(this, arguments);
}
}

componentWillUnmount() {
Object.keys(this.__subs).forEach(sub => this.__subs[sub].stop());
this.__comps.forEach(comp => comp.stop());
Object.keys(this.__comps).forEach(stateKey =>
this.__comps[stateKey] && this.__comps[stateKey]
.forEach(c => c.stop()));
}

render() {
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: 'std:tracker-component',
version: '1.3.21',
version: '1.4.0',
summary: 'Easy reactive React Components with Meteor and Tracker',
git: 'https://github.com/studiointeract/tracker-component',
documentation: 'README.md'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tracker-component",
"version": "1.3.21",
"version": "1.4.0",
"description": "Easy reactive React Components with Meteor and Tracker",
"repository": {
"type": "git",
Expand Down