Skip to content

Commit 02e5372

Browse files
authored
Getting overmind ready (codesandbox#2443)
* Fixes * Remove console.log * Push * fix linting issues * added live user * Fix LiveUser erro * Fix type error * Apply suggestions from code review Co-Authored-By: Michaël De Boey <[email protected]> * Apply type styling fix * Fix types
1 parent b51e00f commit 02e5372

File tree

35 files changed

+286
-194
lines changed

35 files changed

+286
-194
lines changed

.eslintrc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@
4141
}
4242
],
4343
"no-param-reassign": ["error", { "props": false }],
44-
"camelcase": [
45-
"error",
46-
{
47-
"allow": ["UNSAFE_componentDidMount", "_id$", "^UNSAFE_"],
48-
"properties": "never"
49-
}
50-
],
44+
"camelcase": 0,
5145
"react-hooks/rules-of-hooks": "error",
5246
"react-hooks/exhaustive-deps": "warn",
5347
"jsx-a11y/href-no-hash": "off",

packages/app/.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
src/app/overmind/**/*
21
src/app/vscode/**/*

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ export class VSCode extends React.Component<Props> implements Editor {
8787
sandbox: Props['sandbox'];
8888
currentModule: Props['currentModule'];
8989
currentTitle: string;
90-
currentDirectoryShortid: string | undefined;
90+
currentDirectoryShortid: string | null;
9191
settings: Props['settings'];
9292
dependencies: Props['dependencies'] | undefined;
9393
tsconfig: Props['tsconfig'] | undefined;
9494
disposeInitializer?: Function;
95-
lintWorker: Worker | undefined;
95+
lintWorker: Worker | null;
9696
editor?: any;
9797
monaco?: any;
9898
receivingCode: boolean = false;
9999
codeSandboxAPIListener: () => void;
100-
sizeProbeInterval: number | null;
100+
sizeProbeInterval: number | undefined;
101101

102102
modelSelectionListener: {
103103
dispose: () => void;
@@ -121,7 +121,7 @@ export class VSCode extends React.Component<Props> implements Editor {
121121
this.tsconfig = props.tsconfig;
122122

123123
this.lintWorker = null;
124-
this.sizeProbeInterval = null;
124+
this.sizeProbeInterval = undefined;
125125

126126
this.resizeEditor = debounce(this.resizeEditorInstantly, 150);
127127
this.commitLibChanges = debounce(this.commitLibChangesInstantly, 300);
@@ -562,7 +562,9 @@ export class VSCode extends React.Component<Props> implements Editor {
562562
// Something went wrong while composing the operation, so we're opting for a full sync
563563
console.error(e);
564564

565-
this.props.onModuleStateMismatch();
565+
if (this.props.onModuleStateMismatch) {
566+
this.props.onModuleStateMismatch();
567+
}
566568
}
567569

568570
requestAnimationFrame(() => {
@@ -650,7 +652,11 @@ export class VSCode extends React.Component<Props> implements Editor {
650652
pushStack = false,
651653
model = this.editor.getActiveCodeEditor().getModel()
652654
) => {
653-
const results = [];
655+
const results: Array<{
656+
range: unknown;
657+
text: string;
658+
forceMoveMarkers?: boolean;
659+
}> = [];
654660
let index = 0;
655661
const currentEOLLength = model.getEOL().length;
656662
let eolChanged = false;
@@ -723,9 +729,15 @@ export class VSCode extends React.Component<Props> implements Editor {
723729
Object.keys(operationsJSON).forEach(moduleShortid => {
724730
const operation = TextOperation.fromJSON(operationsJSON[moduleShortid]);
725731

726-
const moduleId = this.sandbox.modules.find(
732+
const foundModule = this.sandbox.modules.find(
727733
m => m.shortid === moduleShortid
728-
).id;
734+
);
735+
736+
if (!foundModule) {
737+
return;
738+
}
739+
740+
const moduleId = foundModule.id;
729741

730742
const modulePath =
731743
'/sandbox' +
@@ -754,7 +766,9 @@ export class VSCode extends React.Component<Props> implements Editor {
754766
}
755767
} catch (e) {
756768
// Something went wrong while applying
757-
this.props.onModuleStateMismatch();
769+
if (this.props.onModuleStateMismatch) {
770+
this.props.onModuleStateMismatch();
771+
}
758772
}
759773
} else {
760774
this.liveOperationCode = '';
@@ -766,7 +780,7 @@ export class VSCode extends React.Component<Props> implements Editor {
766780
model.object.textEditorModel
767781
);
768782

769-
if (this.props.onChange) {
783+
if (this.props.onChange && module) {
770784
this.props.onChange(
771785
model.object.textEditorModel.getValue(),
772786
module.shortid
@@ -878,7 +892,7 @@ export class VSCode extends React.Component<Props> implements Editor {
878892
if (!this.lintWorker) {
879893
this.lintWorker = new LinterWorker();
880894

881-
this.lintWorker.addEventListener('message', event => {
895+
this.lintWorker!.addEventListener('message', event => {
882896
const { markers, version } = event.data;
883897

884898
requestAnimationFrame(() => {
@@ -1033,7 +1047,7 @@ export class VSCode extends React.Component<Props> implements Editor {
10331047
return;
10341048
}
10351049

1036-
const mode = await getMode(title, this.monaco);
1050+
const mode = (await getMode(title, this.monaco)) || '';
10371051
if (this.settings.lintEnabled) {
10381052
if (
10391053
['javascript', 'typescript', 'typescriptreact', 'vue'].includes(mode)

packages/app/src/app/components/CodeEditor/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export interface Editor {
1212
changeSandbox?: (
1313
sandbox: Sandbox,
1414
newCurrentModule: Module,
15-
dependencies: Object
15+
dependencies: { [name: string]: string }
1616
) => Promise<any>;
1717
setErrors?: (errors: Array<ModuleError>) => any;
1818
setCorrections?: (corrections: Array<ModuleCorrection>) => any;
1919
updateModules?: () => any;
2020
changeSettings?: (settings: Settings) => any;
21-
changeDependencies?: (deps: Object) => any;
21+
changeDependencies?: (deps: { [name: string]: string }) => any;
2222
changeModule?: (
2323
module: Module,
2424
errors?: Array<ModuleError>,

packages/app/src/app/overmind/effects/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export default {
250250
},
251251
massCreateModules(
252252
sandboxId: string,
253-
directoryShortid: string,
253+
directoryShortid: string | null,
254254
modules: Module[],
255255
directories: Directory[]
256256
): Promise<{

packages/app/src/app/overmind/effects/connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const listeners = new Map();
44

55
export default {
66
addListener(listener: (connected: boolean) => void) {
7-
const disposer = addListener(listener ? listener : () => {});
7+
const disposer = addListener(listener);
88
listeners.set(listener, disposer);
99
},
1010
removeListener(listener: (connected: boolean) => void) {

packages/app/src/app/overmind/effects/fsSync.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ async function syncDependencyTypings(
9292
if (process.env.NODE_ENV === 'development') {
9393
console.warn('Trouble fetching types for ' + depName);
9494
}
95-
return {};
9695
}
9796
})
9897
);
@@ -139,28 +138,36 @@ export default {
139138
sendFiles();
140139

141140
try {
142-
fs.stat('/sandbox/package.json', (e, stat) => {
143-
if (e) {
141+
fs.stat('/sandbox/package.json', (packageJsonError, stat) => {
142+
if (packageJsonError) {
144143
return;
145144
}
146145

147146
if (stat.mtime.toString() !== lastMTime.toString()) {
148147
lastMTime = stat.mtime;
149148

150-
fs.readFile('/sandbox/package.json', async (err, rv) => {
151-
if (e) {
152-
console.error(e);
153-
return;
149+
fs.readFile(
150+
'/sandbox/package.json',
151+
async (packageJsonReadError, rv) => {
152+
if (packageJsonReadError) {
153+
console.error(packageJsonReadError);
154+
return;
155+
}
156+
157+
fs.stat('/sandbox/tsconfig.json', (tsConfigError, result) => {
158+
// If tsconfig exists we want to sync the types
159+
syncDependencyTypings(
160+
rv.toString(),
161+
Boolean(tsConfigError) || !result
162+
);
163+
});
154164
}
155-
156-
fs.stat('/sandbox/tsconfig.json', (err, result) => {
157-
// If tsconfig exists we want to sync the types
158-
syncDependencyTypings(rv.toString(), Boolean(err) || !result);
159-
});
160-
});
165+
);
161166
}
162167
});
163-
} catch (e) {}
168+
} catch (e) {
169+
// Do nothing
170+
}
164171
}, 1000);
165172

166173
self.addEventListener('message', evt => {

packages/app/src/app/overmind/effects/git/export-to-github.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { Sandbox } from '@codesandbox/common/lib/types';
12
import JSZip from 'jszip';
23
import { createZip, BLOB_ID } from '../zip/create-zip';
3-
import { Sandbox } from '@codesandbox/common/lib/types';
44

55
export default async function deploy(sandbox: Sandbox) {
66
// We first get the zip file, this is what we essentially need to have deployed.

packages/app/src/app/overmind/effects/live/clients.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client, TextOperation } from 'ot';
1+
import { Client } from 'ot';
22

33
export type SendOperation = (
44
moduleShortid: string,
@@ -88,9 +88,9 @@ export default (
8888

8989
return client;
9090
},
91-
create(moduleShortid, revision) {
91+
create(moduleShortid, initialRevision) {
9292
const client = new CodeSandboxOTClient(
93-
revision,
93+
initialRevision,
9494
moduleShortid,
9595
(revision, operation) => {
9696
sendOperation(

packages/app/src/app/overmind/effects/live/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Directory,
99
RoomInfo,
1010
Sandbox,
11+
LiveMessageEvent,
1112
} from '@codesandbox/common/lib/types';
1213
import { getTextOperation } from '@codesandbox/common/lib/utils/diff';
1314
import clientsFactory from './clients';
@@ -110,7 +111,7 @@ export default {
110111
// TODO: Need to take an action here
111112
listen(
112113
action: (payload: {
113-
event: string;
114+
event: LiveMessageEvent;
114115
_isOwnMessage: boolean;
115116
data: object;
116117
}) => {}
@@ -183,7 +184,7 @@ export default {
183184
moduleShortid,
184185
operation
185186
);
186-
return this.send('live:module_state', {});
187+
this.send('live:module_state', {});
187188
}
188189
},
189190
sendUserCurrentModule(moduleShortid: string) {

0 commit comments

Comments
 (0)