Skip to content

Commit a339feb

Browse files
committed
Don't lose the isOperationInProgress state when the user switches tabs
1 parent a0c8ca5 commit a339feb

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

packages/browser-repl/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"depcheck": "depcheck",
3636
"compile": "tsc -p tsconfig.json",
3737
"prettier": "prettier",
38-
"reformat": "npm run prettier -- --write . && npm run eslint --fix"
38+
"reformat": "npm run prettier -- --write . && npm run eslint --fix",
39+
"sync-to-compass": "node scripts/sync-to-compass.js"
3940
},
4041
"config": {
4142
"unsafe-perm": true
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* eslint-disable no-console */
2+
'use strict';
3+
const fs = require('fs');
4+
const path = require('path');
5+
const child_process = require('child_process');
6+
const { debounce } = require('lodash');
7+
8+
if (!process.env.COMPASS_HOME) {
9+
throw new Error('Missing required environment variable $COMPASS_HOME.');
10+
}
11+
12+
const packageDir = path.resolve(__dirname, '..');
13+
const srcDir = path.resolve(__dirname, '..', 'src');
14+
const libDir = path.resolve(__dirname, '..', 'lib');
15+
16+
const destDir = path.dirname(
17+
child_process.execFileSync(
18+
'node',
19+
['-e', "console.log(require.resolve('@mongosh/browser-repl'))"],
20+
{ cwd: process.env.COMPASS_HOME, encoding: 'utf-8' }
21+
)
22+
);
23+
24+
console.log({ packageDir, srcDir, libDir, destDir });
25+
26+
const compileAndCopy = debounce(
27+
function () {
28+
child_process.execFileSync('npm', ['run', 'compile'], { cwd: packageDir });
29+
fs.cpSync(libDir, destDir, { recursive: true });
30+
console.log('done.');
31+
},
32+
1_000,
33+
{
34+
leading: true,
35+
trailing: true,
36+
}
37+
);
38+
39+
const srcWatcher = fs.watch(
40+
srcDir,
41+
{ recursive: true },
42+
function (eventType, filename) {
43+
console.log(eventType, filename);
44+
compileAndCopy();
45+
}
46+
);
47+
48+
function cleanup() {
49+
srcWatcher.close();
50+
}
51+
52+
for (const evt of ['SIGINT', 'SIGTERM']) {
53+
process.on(evt, cleanup);
54+
}
55+
56+
// do an initial copy on startup
57+
compileAndCopy();

packages/browser-repl/src/components/shell-input.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ export class ShellInput extends Component<ShellInputProps, ShellInputState> {
110110

111111
private onEnter = async (): Promise<void> => {
112112
if (this.props.onInput) {
113-
await this.props.onInput(this.state.currentValue);
113+
const value = this.state.currentValue;
114+
// clear the value before evaluating the input because it could take a
115+
// long time
116+
this.setState({ currentValue: '' });
117+
await this.props.onInput(value);
114118
}
115-
116-
this.setState({ currentValue: '' });
117119
};
118120

119121
render(): JSX.Element {

packages/browser-repl/src/components/shell.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,17 @@ interface ShellProps {
127127
*/
128128
initialHistory: readonly string[];
129129

130+
/**
131+
* A boolean so we can keep the isOperationInProgress state between sessions.
132+
*/
133+
isOperationInProgress?: boolean;
134+
130135
darkMode?: boolean;
131136

132137
className?: string;
133138
}
134139

135140
interface ShellState {
136-
operationInProgress: boolean;
137141
output: readonly ShellOutputEntry[];
138142
history: readonly string[];
139143
passwordPrompt: string;
@@ -153,14 +157,6 @@ const normalizeInitialEvaluate = (initialEvaluate: string | string[]) => {
153157
});
154158
};
155159

156-
const isInitialEvaluateEmpty = (
157-
initialEvaluate?: string | string[] | undefined
158-
) => {
159-
return (
160-
!initialEvaluate || normalizeInitialEvaluate(initialEvaluate).length === 0
161-
);
162-
};
163-
164160
/**
165161
* The browser-repl Shell component
166162
*/
@@ -175,6 +171,7 @@ export class _Shell extends Component<ShellProps, ShellState> {
175171
initialInput: '',
176172
initialOutput: [],
177173
initialHistory: [],
174+
isOperationInProgress: false,
178175
};
179176

180177
private shellInputElement: HTMLElement | null = null;
@@ -183,7 +180,6 @@ export class _Shell extends Component<ShellProps, ShellState> {
183180
private onCancelPasswordPrompt: () => void = noop;
184181

185182
readonly state: ShellState = {
186-
operationInProgress: !isInitialEvaluateEmpty(this.props.initialEvaluate),
187183
output: this.props.initialOutput.slice(-this.props.maxOutputLength),
188184
history: this.props.initialHistory.slice(0, this.props.maxHistoryLength),
189185
passwordPrompt: '',
@@ -357,17 +353,16 @@ export class _Shell extends Component<ShellProps, ShellState> {
357353

358354
let output = this.addEntriesToOutput([inputLine]);
359355
this.setState({
360-
operationInProgress: true,
361356
output,
362357
});
363358
this.props.onOutputChanged(output);
364359

360+
// TODO: what if we switch away from the shell while this is ongoing?
365361
const outputLine = await this.evaluate(code);
366362

367363
output = this.addEntriesToOutput([outputLine]);
368364
const history = this.addEntryToHistory(code);
369365
this.setState({
370-
operationInProgress: false,
371366
output,
372367
history,
373368
});
@@ -411,7 +406,7 @@ export class _Shell extends Component<ShellProps, ShellState> {
411406

412407
private onSigInt = (): Promise<boolean> => {
413408
if (
414-
this.state.operationInProgress &&
409+
this.props.isOperationInProgress &&
415410
(this.props.runtime as WorkerRuntime).interrupt
416411
) {
417412
return (this.props.runtime as WorkerRuntime).interrupt();
@@ -440,7 +435,7 @@ export class _Shell extends Component<ShellProps, ShellState> {
440435
history={this.state.history}
441436
onClearCommand={this.onClearCommand}
442437
onInput={this.onInput}
443-
operationInProgress={this.state.operationInProgress}
438+
operationInProgress={this.props.isOperationInProgress}
444439
editorRef={this.setEditor}
445440
onSigInt={this.onSigInt}
446441
/>

0 commit comments

Comments
 (0)