Skip to content

Commit 80b13bb

Browse files
authored
Remove need for running sandbox queries on joining a sandbox (codesandbox#3504)
1 parent 4cf821b commit 80b13bb

File tree

3 files changed

+11
-29
lines changed

3 files changed

+11
-29
lines changed

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ import {
33
LiveMessageEvent,
44
Module,
55
RoomInfo,
6-
Sandbox,
76
} from '@codesandbox/common/lib/types';
87
import _debug from '@codesandbox/common/lib/utils/debug';
98
import { camelizeKeys } from 'humps';
109
import { TextOperation } from 'ot';
1110
import { Socket, Channel } from 'phoenix';
1211
import uuid from 'uuid';
1312

14-
import { SandboxAPIResponse } from '../api/types';
15-
import { transformSandbox } from '../utils/sandbox';
1613
import clientsFactory from './clients';
1714
import { OPTIMISTIC_ID_PREFIX } from '../utils';
1815

@@ -22,17 +19,10 @@ type Options = {
2219
};
2320

2421
type JoinChannelResponse = {
25-
sandboxId: string;
26-
sandbox: SandboxAPIResponse;
27-
moduleState: object;
2822
liveUserId: string;
2923
roomInfo: RoomInfo;
3024
};
3125

32-
type JoinChannelTransformedResponse = JoinChannelResponse & {
33-
sandbox: Sandbox;
34-
};
35-
3626
declare global {
3727
interface Window {
3828
socket: any;
@@ -118,17 +108,15 @@ export default new (class Live {
118108
});
119109
}
120110

121-
joinChannel(roomId: string): Promise<JoinChannelTransformedResponse> {
111+
joinChannel(roomId: string): Promise<JoinChannelResponse> {
122112
return new Promise((resolve, reject) => {
123113
channel = this.getSocket().channel(`live:${roomId}`, {});
124114

125115
channel
126116
.join()
127117
.receive('ok', resp => {
128118
const result = camelizeKeys(resp) as JoinChannelResponse;
129-
// @ts-ignore
130-
result.sandbox = transformSandbox(result.sandbox);
131-
resolve(result as JoinChannelTransformedResponse);
119+
resolve(result);
132120
})
133121
.receive('error', resp => reject(camelizeKeys(resp)));
134122
});

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
121121
if (sandbox.owned && !state.live.isLive) {
122122
actions.files.internal.recoverFiles();
123123
} else if (state.live.isLive) {
124-
effects.live.sendModuleStateSyncRequest();
124+
await effects.live.sendModuleStateSyncRequest();
125125
}
126126

127127
effects.vscode.openModule(state.editor.currentModule);

packages/app/src/app/overmind/namespaces/live/internalActions.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,19 @@ export const initialize: AsyncAction<string, Sandbox | null> = async (
5959
state.live.isLoading = true;
6060

6161
try {
62-
const {
63-
roomInfo,
64-
liveUserId,
65-
sandbox,
66-
moduleState,
67-
} = await effects.live.joinChannel(id);
62+
const { roomInfo, liveUserId } = await effects.live.joinChannel(id);
6863

6964
state.live.roomInfo = roomInfo;
7065
state.live.liveUserId = liveUserId;
7166

72-
if (
73-
!state.editor.currentSandbox ||
74-
state.editor.currentSandbox.id !== sandbox.id
75-
) {
76-
state.editor.sandboxes[sandbox.id] = sandbox;
77-
state.editor.currentId = sandbox.id;
78-
}
67+
const sandboxId = roomInfo.sandboxId;
7968

80-
actions.live.internal.initializeModuleState(moduleState);
69+
let sandbox = state.editor.currentSandbox;
70+
if (!sandbox || sandbox.id !== sandboxId) {
71+
sandbox = await effects.api.getSandbox(sandboxId);
72+
state.editor.sandboxes[sandboxId] = sandbox;
73+
state.editor.currentId = sandboxId;
74+
}
8175

8276
effects.analytics.track('Live Session Joined', {});
8377
effects.live.listen(actions.live.liveMessageReceived);

0 commit comments

Comments
 (0)