Skip to content
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

fix(plugins/plugin-client-common): Eval component throws console errors #4463

Merged
merged 1 commit into from
May 5, 2020
Merged
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
75 changes: 32 additions & 43 deletions plugins/plugin-client-common/src/components/Content/Eval.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,61 +75,50 @@ export default class Eval extends React.PureComponent<EvalProps, EvalState> {
super(props)

this.state = {
isLoading: false,
isLoading: true,
command: props.command,
react: undefined,
content: undefined,
contentType: props.contentType
}

this.startLoading()
}

public static getDerivedStateFromProps(props: EvalProps, state: EvalState) {
const newState = {
isLoading: state.isLoading,
command: props.command,
content: props.command === state.command ? state.content : undefined
private startLoading() {
const done = (content: ScalarResource) => {
debug('eval done', content)
this.setState({ isLoading: false, content })
}

return newState
if (typeof this.props.command === 'string') {
// command string
this.props.tab.REPL.qexec<ScalarResource>(this.props.command).then(done)
} else {
Promise.resolve(this.props.command(this.props.tab, this.props.response, this.props.args))
.then(content => {
if (isCommandStringContent(content)) {
return this.props.tab.REPL.qexec<ScalarResource | ScalarContent>(content.contentFrom)
} else {
return content
}
})
.then(content => {
if (isReactProvider(content)) {
this.setState({ isLoading: false, react: content })
} else if (isStringWithOptionalContentType(content)) {
this.setState({ isLoading: false, content: content.content, contentType: content.contentType })
} else if (isScalarContent(content)) {
done(content.content)
} else {
done(content)
}
})
}
}

public render() {
// console.error('!!Eval', this.state, this.props.command)
if (!this.state.content && !this.state.react) {
if (!this.state.isLoading) {
this.setState({ isLoading: true })

const done = (content: ScalarResource) => {
debug('eval done', content)
this.setState({ isLoading: false, content })
}

if (typeof this.props.command === 'string') {
// command string
this.props.tab.REPL.qexec<ScalarResource>(this.props.command).then(done)
} else {
Promise.resolve(this.props.command(this.props.tab, this.props.response, this.props.args))
.then(content => {
if (isCommandStringContent(content)) {
return this.props.tab.REPL.qexec<ScalarResource | ScalarContent>(content.contentFrom)
} else {
return content
}
})
.then(content => {
if (isReactProvider(content)) {
this.setState({ isLoading: false, react: content })
} else if (isStringWithOptionalContentType(content)) {
this.setState({ isLoading: false, content: content.content, contentType: content.contentType })
} else if (isScalarContent(content)) {
done(content.content)
} else {
done(content)
}
})
}
}

if (this.state.isLoading) {
return <Loading />
}

Expand Down