Skip to content

Commit 500c853

Browse files
authored
Fix live regressions (codesandbox#2213)
Fixes codesandbox#2212 Fixes codesandbox#2208 Fixes issues with: - Need to sign in to join live session - Executors (server) not working live - File opening causing desynchronization - Live session not setting workspace defaults and settings
1 parent ffbcd88 commit 500c853

File tree

4 files changed

+49
-33
lines changed

4 files changed

+49
-33
lines changed

packages/app/src/app/components/CodeEditor/VSCode/index.tsx

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ function getSelection(lines, selection) {
6666
};
6767
}
6868

69+
type UserSelection =
70+
| {
71+
userId: string;
72+
selection: null;
73+
}
74+
| {
75+
userId: string;
76+
name: string;
77+
selection: any;
78+
color: number[];
79+
};
80+
6981
class MonacoEditor extends React.Component<Props> implements Editor {
7082
static defaultProps = {
7183
width: '100%',
@@ -406,6 +418,12 @@ class MonacoEditor extends React.Component<Props> implements Editor {
406418
this.currentModule.code !== undefined &&
407419
activeEditor.getValue(1) !== this.currentModule.code
408420
) {
421+
// Don't send these changes over live, since these changes can also be made by someone else and
422+
// we don't want to keep singing these changes
423+
// TODO: a better long term solution would be to store the changes of someone else in a model, even if the
424+
// model is not opened in an editor.
425+
426+
this.receivingCode = true;
409427
// This means that the file in Cerebral is dirty and has changed,
410428
// VSCode only gets saved contents. In this case we manually set the value correctly.
411429
const model = activeEditor.getModel();
@@ -415,6 +433,7 @@ class MonacoEditor extends React.Component<Props> implements Editor {
415433
range: model.getFullModelRange(),
416434
},
417435
]);
436+
this.receivingCode = false;
418437
}
419438

420439
this.modelSelectionListener = activeEditor.onDidChangeCursorSelection(
@@ -557,20 +576,7 @@ class MonacoEditor extends React.Component<Props> implements Editor {
557576

558577
userClassesGenerated = {};
559578
userSelectionDecorations = {};
560-
updateUserSelections = (
561-
userSelections: Array<
562-
| {
563-
userId: string;
564-
selection: null;
565-
}
566-
| {
567-
userId: string;
568-
name: string;
569-
selection: any;
570-
color: number[];
571-
}
572-
>
573-
) => {
579+
updateUserSelections = (userSelections: UserSelection[]) => {
574580
if (this.editor.getActiveCodeEditor()) {
575581
updateUserSelections(
576582
this.monaco,
@@ -730,23 +736,24 @@ class MonacoEditor extends React.Component<Props> implements Editor {
730736
// Something went wrong while applying
731737
this.props.onModuleStateMismatch();
732738
}
733-
return;
734-
}
735-
736-
this.liveOperationCode = '';
739+
} else {
740+
this.liveOperationCode = '';
737741

738-
modelEditor.textModelReference.then(model => {
739-
this.applyOperationToModel(
740-
operation,
741-
false,
742-
model.object.textEditorModel
743-
);
742+
modelEditor.textModelReference.then(model => {
743+
this.applyOperationToModel(
744+
operation,
745+
false,
746+
model.object.textEditorModel
747+
);
744748

745-
this.props.onChange(
746-
model.object.textEditorModel.getValue(1),
747-
module.shortid
748-
);
749-
});
749+
if (this.props.onChange) {
750+
this.props.onChange(
751+
model.object.textEditorModel.getValue(1),
752+
module.shortid
753+
);
754+
}
755+
});
756+
}
750757
});
751758
};
752759

packages/app/src/app/pages/Live/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Centered from '@codesandbox/common/lib/components/flex/Centered';
1313
import Skeleton from 'app/components/Skeleton';
1414
import Padding from '@codesandbox/common/lib/components/spacing/Padding';
1515
import SignInButton from 'app/pages/common/SignInButton';
16+
import { hasAuthToken } from 'app/utils/user';
1617

1718
import Editor from '../Sandbox/Editor';
1819
import BlinkingDot from './BlinkingDot';
@@ -36,7 +37,7 @@ class LivePage extends React.Component {
3637
}
3738

3839
initializeLive = () => {
39-
if (this.props.store.hasLogIn) {
40+
if (hasAuthToken()) {
4041
this.loggedIn = true;
4142
this.props.signals.live.roomJoined({
4243
roomId: this.props.match.params.id,
@@ -47,7 +48,7 @@ class LivePage extends React.Component {
4748
componentDidUpdate(prevProps) {
4849
if (
4950
prevProps.match.params.id !== this.props.match.params.id ||
50-
(this.props.store.hasLogIn && !this.loggedIn)
51+
(hasAuthToken() && !this.loggedIn)
5152
) {
5253
this.disconnectLive();
5354
this.initializeLive();
@@ -57,7 +58,7 @@ class LivePage extends React.Component {
5758
getContent = () => {
5859
const { store } = this.props;
5960

60-
if (!store.hasLogIn) {
61+
if (!hasAuthToken()) {
6162
return (
6263
<React.Fragment>
6364
<div

packages/app/src/app/store/sequences.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ export const setSandbox = [
130130
when(
131131
state`editor.currentId`,
132132
props`sandbox.id`,
133-
(currentId, newId) => currentId === newId
133+
state`live.isLoading`,
134+
// If we don't add the live check we will never initialize this state, since the roomJoined sequence also initializes
135+
// editor.currentId to sandbox.id, which causes this check to always resolve to true
136+
(currentId, newId, isLoadingLive) => currentId === newId && !isLoadingLive
134137
),
135138
{
136139
true: [],

packages/app/src/app/utils/user.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import store from 'store/dist/store.modern';
2+
3+
export function hasAuthToken() {
4+
return !!store.get('jwt');
5+
}

0 commit comments

Comments
 (0)