feat(babel6): Fix spread operator

This commit is contained in:
Evan Morikawa 2016-05-04 18:38:29 -07:00
parent 8f4e30329c
commit e717a44581
5 changed files with 127 additions and 119 deletions

View file

@ -1,98 +1,101 @@
import React from 'react';
import {DraftStore, Actions, Utils} from 'nylas-exports';
export default ComposedComponent => class extends React.Component {
static displayName = ComposedComponent.displayName;
static propTypes = {
draftClientId: React.PropTypes.string,
onDraftReady: React.PropTypes.func,
}
static defaultProps = {
onDraftReady: () => {},
}
static containerRequired = false;
function InflateDraftClientId(ComposedComponent) {
return class extends React.Component {
static displayName = ComposedComponent.displayName;
static propTypes = {
draftClientId: React.PropTypes.string,
onDraftReady: React.PropTypes.func,
}
static defaultProps = {
onDraftReady: () => {},
}
static containerRequired = false;
constructor(props) {
super(props);
this.state = {
session: null,
draft: null,
};
}
constructor(props) {
super(props);
this.state = {
session: null,
draft: null,
};
}
componentWillMount() {
this._unmounted = false;
this._prepareForDraft(this.props.draftClientId);
}
componentWillMount() {
this._unmounted = false;
this._prepareForDraft(this.props.draftClientId);
}
componentWillUnmount() {
this._unmounted = true;
this._teardownForDraft();
this._deleteDraftIfEmpty();
}
componentWillReceiveProps(newProps) {
if (newProps.draftClientId !== this.props.draftClientId) {
componentWillUnmount() {
this._unmounted = true;
this._teardownForDraft();
this._prepareForDraft(newProps.draftClientId);
this._deleteDraftIfEmpty();
}
}
_prepareForDraft(draftClientId) {
if (!draftClientId) {
return;
componentWillReceiveProps(newProps) {
if (newProps.draftClientId !== this.props.draftClientId) {
this._teardownForDraft();
this._prepareForDraft(newProps.draftClientId);
}
}
DraftStore.sessionForClientId(draftClientId).then((session) => {
if (this._unmounted) {
_prepareForDraft(draftClientId) {
if (!draftClientId) {
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;
}
if (this.state.draft.pristine) {
Actions.destroyDraft(this.props.draftClientId);
}
}
this._sessionUnlisten = session.listen(() => {
this.setState({draft: session.draft()});
// 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(() => {
});
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;
render() {
if (!this.state.draft) {
return <span/>;
}
return <ComposedComponent ref="composed" {...this.props} {...this.state} />;
}
if (this.state.draft.pristine) {
Actions.destroyDraft(this.props.draftClientId);
}
}
// 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} />;
}
};
};
}
export default InflateDraftClientId

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) {
const match = func.toString().match(RegExpUtils.functionArgs());
if (!match) return null;
return match[1].split(/\s*,\s*/);
if (!match) return [[]];
const matchStr = match[1] || match[2]
return matchStr.split(/\s*,\s*/);
}

View file

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