Skip to content

Commit 6314c3d

Browse files
christianalfoniSaraVieira
authored andcommitted
Live fixes (codesandbox#2432)
* Fix selection issue * Fix updated messages for modules and directories * Fix moving directories and refactor a bit * Fix deleting directory * Fix issue with live code updates and save
1 parent 4beb5c8 commit 6314c3d

File tree

7 files changed

+55
-41
lines changed

7 files changed

+55
-41
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class CodeSandboxOTClient extends Client {
5656
super.applyClient(operation);
5757
}
5858
applyServer(operation: any) {
59-
console.log(operation);
6059
super.applyServer(operation);
6160
}
6261
}
@@ -97,16 +96,11 @@ export default (
9796
sendOperation(
9897
moduleShortid,
9998
revision,
100-
operationToElixir(
101-
Array.isArray(operation) ? operation : operation.toJSON()
102-
)
99+
operationToElixir(operation.toJSON())
103100
);
104101
},
105102
operation => {
106-
applyOperation(
107-
moduleShortid,
108-
Array.isArray(operation) ? operation : operation.toJSON()
109-
);
103+
applyOperation(moduleShortid, operation);
110104
}
111105
);
112106
modules.set(moduleShortid, client);

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,31 +164,35 @@ export default {
164164
});
165165
},
166166
sendCodeUpdate(moduleShortid: string, currentCode: string, code: string) {
167+
if (currentCode === code) {
168+
return;
169+
}
170+
167171
const operation = getTextOperation(currentCode, code);
168172

169173
if (!operation) {
170174
return;
171175
}
172176

173177
try {
174-
clients.get(moduleShortid).applyClient(TextOperation.fromJSON(operation));
178+
clients.get(moduleShortid).applyClient(TextOperation.fromJSON());
175179
} catch (e) {
176180
// Something went wrong, probably a sync mismatch. Request new version
177181
console.error(
178182
'Something went wrong with applying OT operation',
179183
moduleShortid,
180184
operation
181185
);
182-
this.send('live:module_state', {});
186+
return this.send('live:module_state', {});
183187
}
184188
},
185189
sendUserCurrentModule(moduleShortid: string) {
186-
this.send('user:current-module', {
190+
return this.send('user:current-module', {
187191
moduleShortid,
188192
});
189193
},
190194
sendDirectoryCreated(directory: Directory) {
191-
this.send('directory:created', {
195+
return this.send('directory:created', {
192196
type: 'directory',
193197
module: directory,
194198
});
@@ -200,61 +204,65 @@ export default {
200204
});
201205
},
202206
sendModuleCreated(module: Module) {
203-
this.send('module:created', {
207+
return this.send('module:created', {
204208
type: 'module',
205209
moduleShortid: module.shortid,
206210
module,
207211
});
208212
},
209213
sendModuleDeleted(moduleShortid: string) {
210-
this.send('module:deleted', {
214+
return this.send('module:deleted', {
211215
type: 'module',
212216
moduleShortid,
213217
});
214218
},
215219
sendMassCreatedModules(modules: Module[], directories: Directory[]) {
216-
this.send('module:mass-created', {
220+
return this.send('module:mass-created', {
217221
directories,
218222
modules,
219223
});
220224
},
221225
sendLiveMode(mode: string) {
222-
this.send('live:mode', {
226+
return this.send('live:mode', {
223227
mode,
224228
});
225229
},
226230
sendEditorAdded(liveUserId: string) {
227-
this.send('live:add-editor', {
231+
return this.send('live:add-editor', {
228232
editor_user_id: liveUserId,
229233
});
230234
},
231235
sendEditorRemoved(liveUserId: string) {
232-
this.send('live:remove-editor', {
236+
return this.send('live:remove-editor', {
233237
editor_user_id: liveUserId,
234238
});
235239
},
236240
sendClosed() {
237-
this.send('live:close', {});
241+
return this.send('live:close', {});
238242
},
239243
sendChat(message: string) {
240-
this.send('chat', {
244+
return this.send('chat', {
241245
message,
242246
});
243247
},
244-
sendModuleSaved(moduleShortid: string) {
245-
this.send('module:saved', {
248+
sendModuleState() {
249+
return this.send('live:module_state', {});
250+
},
251+
sendModuleSaved(module: Module) {
252+
return this.send('module:saved', {
246253
type: 'module',
247-
moduleShortid,
254+
module,
255+
moduleShortid: module.shortid,
248256
});
249257
},
250258
sendChatEnabled(enabled: boolean) {
251-
this.send('live:chat_enabled', { enabled });
259+
return this.send('live:chat_enabled', { enabled });
252260
},
253261
sendModuleUpdateRequest() {
254-
this.send('live:module_state', {});
262+
return this.send('live:module_state', {});
255263
},
256264
sendUserSelection(moduleShortid: string, liveUserId: string, selection: any) {
257-
this.send('user:selection', {
265+
return this.send('user:selection', {
258266
liveUserId,
259267
moduleShortid,
260268
selection,
@@ -263,8 +271,18 @@ export default {
263271
getAllClients() {
264272
return clients.getAll();
265273
},
266-
getClient(moduleShortid: string) {
267-
return clients.get(moduleShortid);
274+
applyClient(moduleShortid: string, operation: any) {
275+
return clients
276+
.get(moduleShortid)
277+
.applyClient(TextOperation.fromJSON(operation));
278+
},
279+
applyServer(moduleShortid: string, operation: any) {
280+
return clients
281+
.get(moduleShortid)
282+
.applyServer(TextOperation.fromJSON(operation));
283+
},
284+
serverAck(moduleShortid: string) {
285+
return clients.get(moduleShortid).serverAck();
268286
},
269287
createClient(moduleShortid: string, revision: number) {
270288
return clients.create(moduleShortid, revision);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export const saveCode: AsyncAction<{
103103
}
104104

105105
if (state.live.isLive && state.live.isCurrentEditor) {
106-
effects.live.sendModuleUpdate(module);
106+
effects.live.sendModuleSaved(module);
107107
}
108108

109109
await actions.editor.internal.updateCurrentTemplate();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ export const onTransformMade: Action<{
7979
}
8080

8181
try {
82-
effects.live.getClient(moduleShortid).applyClient(operation);
82+
effects.live.applyClient(moduleShortid, operation);
8383
} catch (e) {
8484
// Something went wrong, probably a sync mismatch. Request new version
8585
console.error(
8686
'Something went wrong with applying OT operation',
8787
moduleShortid,
8888
operation
8989
);
90-
effects.live.send('live:module_state', {});
90+
effects.live.sendModuleState();
9191
}
9292
};
9393

@@ -102,10 +102,10 @@ export const applyTransformation: Action<{
102102

103103
if (existingPendingOperation) {
104104
pendingOperation = TextOperation.fromJSON(existingPendingOperation)
105-
.compose(TextOperation.fromJSON(operation))
105+
.compose(operation)
106106
.toJSON();
107107
} else {
108-
pendingOperation = operation;
108+
pendingOperation = operation.toJSON();
109109
}
110110

111111
state.editor.pendingOperations[moduleShortid] = pendingOperation;

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ export const disconnect: Action = ({ effects }) => {
4545
effects.live.disconnect();
4646
};
4747

48-
export const sendModuleSaved: Action<string> = ({ effects }, moduleShortid) => {
49-
effects.live.sendModuleSaved(moduleShortid);
50-
};
51-
5248
export const initialize: AsyncAction<string, Sandbox> = async (
5349
{ state, effects, actions },
5450
id
@@ -79,7 +75,7 @@ export const initialize: AsyncAction<string, Sandbox> = async (
7975
state.live.receivingCode = false;
8076
state.live.isLive = true;
8177
state.live.error = null;
82-
effects.live.send('live:module_state', {});
78+
effects.live.sendModuleState();
8379

8480
return sandbox;
8581
} catch (error) {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ export const onModuleSaved: Operator<
114114
moduleItem => moduleItem.shortid === data.moduleShortid
115115
);
116116
module.isNotSynced = false;
117+
118+
state.editor.changedModuleShortids.splice(
119+
state.editor.changedModuleShortids.indexOf(module.shortid),
120+
1
121+
);
122+
117123
actions.editor.internal.setModuleSavedCode({
118124
moduleShortid: data.moduleShortid,
119125
savedCode: data.savedCode,
@@ -364,10 +370,10 @@ export const onOperation: Operator<
364370
return;
365371
}
366372
if (_isOwnMessage) {
367-
effects.live.getClient(data.module_shortid).serverAck();
373+
effects.live.serverAck(data.module_shortid);
368374
} else {
369375
try {
370-
effects.live.getClient(data.module_shortid).applyServer(data.operation);
376+
effects.live.applyServer(data.module_shortid, data.operation);
371377
} catch (e) {
372378
// Something went wrong, probably a sync mismatch. Request new version
373379
console.error('Something went wrong with applying OT operation');

packages/app/src/app/store/providers/Live.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ export default Provider({
6868
} else {
6969
reject('Channel is not defined');
7070
}
71-
});
71+
}).then(value => value);
7272
},
7373
});

0 commit comments

Comments
 (0)