feat(babel6): Fix spread operator

This commit is contained in:
Evan Morikawa 2016-05-04 18:38:29 -07:00
parent 92199587b3
commit 706f92bc9d
5 changed files with 127 additions and 119 deletions

View file

@ -1,98 +1,101 @@
import React from 'react'; import React from 'react';
import {DraftStore, Actions, Utils} from 'nylas-exports'; import {DraftStore, Actions, Utils} from 'nylas-exports';
export default ComposedComponent => class extends React.Component { function InflateDraftClientId(ComposedComponent) {
static displayName = ComposedComponent.displayName; return class extends React.Component {
static propTypes = { static displayName = ComposedComponent.displayName;
draftClientId: React.PropTypes.string, static propTypes = {
onDraftReady: React.PropTypes.func, draftClientId: React.PropTypes.string,
} onDraftReady: React.PropTypes.func,
static defaultProps = { }
onDraftReady: () => {}, static defaultProps = {
} onDraftReady: () => {},
static containerRequired = false; }
static containerRequired = false;
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
session: null, session: null,
draft: null, draft: null,
}; };
} }
componentWillMount() { componentWillMount() {
this._unmounted = false; this._unmounted = false;
this._prepareForDraft(this.props.draftClientId); this._prepareForDraft(this.props.draftClientId);
} }
componentWillUnmount() { componentWillUnmount() {
this._unmounted = true; this._unmounted = true;
this._teardownForDraft();
this._deleteDraftIfEmpty();
}
componentWillReceiveProps(newProps) {
if (newProps.draftClientId !== this.props.draftClientId) {
this._teardownForDraft(); this._teardownForDraft();
this._prepareForDraft(newProps.draftClientId); this._deleteDraftIfEmpty();
} }
}
_prepareForDraft(draftClientId) { componentWillReceiveProps(newProps) {
if (!draftClientId) { if (newProps.draftClientId !== this.props.draftClientId) {
return; this._teardownForDraft();
this._prepareForDraft(newProps.draftClientId);
}
} }
DraftStore.sessionForClientId(draftClientId).then((session) => {
if (this._unmounted) { _prepareForDraft(draftClientId) {
if (!draftClientId) {
return; return;
} }
if (session.draftClientId !== this.props.draftClientId) { DraftStore.sessionForClientId(draftClientId).then((session) => {
if (this._unmounted) {
return;
}
if (session.draftClientId !== this.props.draftClientId) {
return;
}
this._sessionUnlisten = session.listen(() => {
this.setState({draft: session.draft()});
});
this.setState({
session: session,
draft: session.draft(),
});
this.props.onDraftReady()
});
}
_teardownForDraft() {
if (this.state.session) {
this.state.session.changes.commit();
}
if (this._sessionUnlisten) {
this._sessionUnlisten();
}
}
_deleteDraftIfEmpty() {
if (!this.state.draft) {
return; return;
} }
if (this.state.draft.pristine) {
Actions.destroyDraft(this.props.draftClientId);
}
}
this._sessionUnlisten = session.listen(() => { // Returns a promise for use in composer/main.es6, to show the window
this.setState({draft: session.draft()}); // once the composer is rendered and focused.
focus() {
return Utils.waitFor(() => this.refs.composed).then(() =>
this.refs.composed.focus()
).catch(() => {
}); });
this.setState({
session: session,
draft: session.draft(),
});
this.props.onDraftReady()
});
}
_teardownForDraft() {
if (this.state.session) {
this.state.session.changes.commit();
} }
if (this._sessionUnlisten) {
this._sessionUnlisten();
}
}
_deleteDraftIfEmpty() { render() {
if (!this.state.draft) { if (!this.state.draft) {
return; return <span/>;
}
return <ComposedComponent ref="composed" {...this.props} {...this.state} />;
} }
if (this.state.draft.pristine) { };
Actions.destroyDraft(this.props.draftClientId); }
} export default InflateDraftClientId
}
// Returns a promise for use in composer/main.es6, to show the window
// once the composer is rendered and focused.
focus() {
return Utils.waitFor(() => this.refs.composed).then(() =>
this.refs.composed.focus()
).catch(() => {
});
}
render() {
if (!this.state.draft) {
return <span/>;
}
return <ComposedComponent ref="composed" {...this.props} {...this.state} />;
}
};

View file

@ -1,36 +0,0 @@
import React, {Component} from 'react';
export default (ComposedComponent, {stores, getStateFromStores}) => class extends Component {
static displayName = ComposedComponent.displayName;
static containerRequired = false;
constructor(props) {
super(props);
this._unlisteners = [];
this.state = getStateFromStores(props);
}
componentDidMount() {
stores.forEach((store) => {
this._unlisteners.push(store.listen(() => {
this.setState(getStateFromStores(this.props));
}));
});
}
componentWillReceiveProps(nextProps) {
this.setState(getStateFromStores(nextProps));
}
componentWillUnmount() {
for (const unlisten of this._unlisteners) {
unlisten();
}
this._unlisteners = [];
}
render() {
return <ComposedComponent {...this.props} {...this.state} />;
}
};

View file

@ -0,0 +1,40 @@
import React, {Component} from 'react';
function ListensToFluxStore(ComposedComponent, {stores, getStateFromStores}) {
return class extends Component {
static displayName = ComposedComponent.displayName;
static containerRequired = false;
constructor(props) {
super(props);
this._unlisteners = [];
this.state = getStateFromStores(props);
}
componentDidMount() {
stores.forEach((store) => {
this._unlisteners.push(store.listen(() => {
this.setState(getStateFromStores(this.props));
}));
});
}
componentWillReceiveProps(nextProps) {
this.setState(getStateFromStores(nextProps));
}
componentWillUnmount() {
for (const unlisten of this._unlisteners) {
unlisten();
}
this._unlisteners = [];
}
render() {
return <ComposedComponent {...this.props} {...this.state} />;
}
};
}
export default ListensToFluxStore

View file

@ -2,6 +2,7 @@ import RegExpUtils from '../regexp-utils';
export function getFunctionArgs(func) { export function getFunctionArgs(func) {
const match = func.toString().match(RegExpUtils.functionArgs()); const match = func.toString().match(RegExpUtils.functionArgs());
if (!match) return null; if (!match) return [[]];
return match[1].split(/\s*,\s*/); const matchStr = match[1] || match[2]
return matchStr.split(/\s*,\s*/);
} }

View file

@ -158,8 +158,8 @@ RegExpUtils =
looseStyleTag: -> /<style/gim looseStyleTag: -> /<style/gim
# Regular expression matching javasript function arguments: # Regular expression matching javasript function arguments:
# https://regex101.com/r/pZ6zF0/1 # https://regex101.com/r/pZ6zF0/2
functionArgs: -> /\(\s*([^)]+?)\s*\)/ functionArgs: -> /(?:\(\s*([^)]+?)\s*\)|(\w+)\s?=>)/
illegalPathCharactersRegexp: -> illegalPathCharactersRegexp: ->
#https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx #https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx