Skip to content

Commit 70c2b34

Browse files
authored
Add support for terminal messages from sse in embed (codesandbox#3460)
* Add support for terminal messages from sse in embed * Add await statements
1 parent 9a54f3a commit 70c2b34

File tree

1 file changed

+36
-5
lines changed
  • packages/app/src/embed/components/Content

1 file changed

+36
-5
lines changed

packages/app/src/embed/components/Content/index.tsx

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
resolveModule,
66
} from '@codesandbox/common/lib/sandbox/modules';
77
import getTemplate from '@codesandbox/common/lib/templates';
8+
import { ServerExecutor, IExecutor } from '@codesandbox/executors';
89
import { parseSandboxConfigurations } from '@codesandbox/common/lib/templates/configuration/parse-sandbox-configurations';
910
import { getPreviewTabs } from '@codesandbox/common/lib/templates/devtools';
1011
import {
@@ -34,6 +35,7 @@ import { CorrectionClearAction } from 'codesandbox-api/dist/types/actions/correc
3435
import * as React from 'react';
3536

3637
// borrow the menu icon from Header in case header is not shown
38+
import { dispatch } from 'codesandbox-api';
3739
import { MenuIcon } from '../legacy/Header/elements';
3840
import SplitPane from '../SplitPane';
3941
import { CodeEditor } from './CodeEditor';
@@ -84,6 +86,11 @@ type State = {
8486
// eslint-disable-next-line import/no-default-export
8587
export default class Content extends React.PureComponent<Props, State> {
8688
state: State;
89+
executor: IExecutor;
90+
errors: ModuleError[];
91+
corrections: ModuleCorrection[];
92+
editor?: Editor;
93+
preview?: BasePreview;
8794

8895
constructor(props: Props) {
8996
super(props);
@@ -104,8 +111,32 @@ export default class Content extends React.PureComponent<Props, State> {
104111

105112
this.errors = [];
106113
this.corrections = [];
114+
115+
this.executor = new ServerExecutor();
116+
this.initializeExecutor();
107117
}
108118

119+
/**
120+
* Initialize the interface responsible for talking with the server, managing
121+
* the terminal output
122+
*/
123+
initializeExecutor = async () => {
124+
const templateDefinition = getTemplate(this.props.sandbox.template);
125+
if (templateDefinition.isServer) {
126+
await this.executor.initialize({
127+
host: 'https://codesandbox.io',
128+
files: {},
129+
sandboxId: this.props.sandbox.id,
130+
});
131+
132+
await this.executor.setup();
133+
134+
this.executor.on('sandbox:log', message => {
135+
dispatch({ type: 'terminal:message', data: message.data });
136+
});
137+
}
138+
};
139+
109140
setPane = (pos: DevToolsTabPosition) => {
110141
this.setState({ currentDevToolPosition: pos });
111142
};
@@ -163,11 +194,6 @@ export default class Content extends React.PureComponent<Props, State> {
163194
return <StyledNotSyncedIcon show={undefined} />;
164195
};
165196

166-
errors: ModuleError[];
167-
corrections: ModuleCorrection[];
168-
editor?: Editor;
169-
preview?: BasePreview;
170-
171197
UNSAFE_componentWillReceiveProps(nextProps: Props) {
172198
if (this.props.currentModule !== nextProps.currentModule) {
173199
if (!this.state.tabs.some(x => x.id === nextProps.currentModule.id)) {
@@ -189,13 +215,18 @@ export default class Content extends React.PureComponent<Props, State> {
189215
this.setState({
190216
tabs: this.getInitTabs(nextProps),
191217
});
218+
this.initializeExecutor();
192219
}
193220
}
194221

195222
componentDidMount() {
196223
setTimeout(this.handleResize);
197224
}
198225

226+
componentWillUnmount() {
227+
this.executor.dispose();
228+
}
229+
199230
setProjectView = (id: string | undefined, view: boolean) => {
200231
this.setState({ isInProjectView: view });
201232
};

0 commit comments

Comments
 (0)