From e53375f4ea7935331ae14477e783d102f4e92e97 Mon Sep 17 00:00:00 2001 From: Per Fredelius Date: Wed, 29 Jun 2016 17:51:49 +0200 Subject: [PATCH] autorun no longer implicitly re-running on state change --- lib/index.jsx | 48 ++++++++++++++++++++++++++++++++++++++---------- package.js | 2 +- package.json | 2 +- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lib/index.jsx b/lib/index.jsx index 1ffb799..e3f74d2 100644 --- a/lib/index.jsx +++ b/lib/index.jsx @@ -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; } @@ -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() { diff --git a/package.js b/package.js index 61dcae3..58aac17 100644 --- a/package.js +++ b/package.js @@ -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' diff --git a/package.json b/package.json index 676b8f0..9c89a63 100644 --- a/package.json +++ b/package.json @@ -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",