Skip to content

Commit b507c17

Browse files
updating to plan
1 parent a379b0b commit b507c17

File tree

6 files changed

+58
-44
lines changed

6 files changed

+58
-44
lines changed

packages/app/src/app/graphql/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export type CodeReference = {
4444
code: Scalars['String'];
4545
head: Scalars['Int'];
4646
path: Scalars['String'];
47+
sandboxVersion: Scalars['Int'];
4748
};
4849

4950
export type CodeReferenceMetadata = {
@@ -52,6 +53,7 @@ export type CodeReferenceMetadata = {
5253
code: Scalars['String'];
5354
head: Scalars['Int'];
5455
path: Scalars['String'];
56+
sandboxVersion: Scalars['Int'];
5557
};
5658

5759
/** A collaborator on a sandbox */
@@ -867,7 +869,7 @@ export type CommentFragment = { __typename?: 'Comment' } & Pick<
867869
> & {
868870
metadata: { __typename?: 'CodeReferenceMetadata' } & Pick<
869871
CodeReferenceMetadata,
870-
'anchor' | 'code' | 'head' | 'path'
872+
'anchor' | 'code' | 'head' | 'path' | 'sandboxVersion'
871873
>;
872874
}
873875
>;

packages/app/src/app/overmind/effects/gql/comments/fragments.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const commentFragment = gql`
1515
code
1616
head
1717
path
18+
sandboxVersion
1819
}
1920
}
2021
resource
@@ -50,6 +51,7 @@ export const commentWithRepliesFragment = gql`
5051
code
5152
head
5253
path
54+
sandboxVersion
5355
}
5456
}
5557
resource

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
} from '@codesandbox/common/lib/utils/analytics/sentry';
1414
import _debug from '@codesandbox/common/lib/utils/debug';
1515
import { camelizeKeys } from 'humps';
16-
import { debounce } from 'lodash-es';
1716
import { SerializedTextOperation, TextOperation } from 'ot';
1817
import { Channel, Presence, Socket } from 'phoenix';
1918
import uuid from 'uuid';
@@ -422,11 +421,7 @@ class Live {
422421
viewRange: UserViewRange
423422
) {
424423
if (this.connectionsCount === 1) {
425-
return this.sendDebounced('user:view-range', {
426-
liveUserId,
427-
moduleShortid,
428-
viewRange,
429-
});
424+
return Promise.resolve();
430425
}
431426

432427
return this.send('user:view-range', {
@@ -442,19 +437,26 @@ class Live {
442437
selection: UserSelection
443438
) {
444439
if (this.connectionsCount === 1) {
445-
return this.sendDebounced('user:selection', {
446-
liveUserId,
447-
moduleShortid,
448-
selection,
449-
});
440+
return Promise.resolve();
450441
}
442+
451443
return this.send('user:selection', {
452444
liveUserId,
453445
moduleShortid,
454446
selection,
455447
});
456448
}
457449

450+
async saveModule(module: Module) {
451+
const client = clients.get(module.shortid);
452+
await client.awaitSynchronized.promise;
453+
454+
return this.send('save', {
455+
moduleShortid: module.shortid,
456+
revision: client.revision,
457+
});
458+
}
459+
458460
reset() {
459461
clients.clear();
460462
}
@@ -491,13 +493,6 @@ class Live {
491493
this.onApplyOperation
492494
);
493495
}
494-
495-
private sendDebounced = debounce<(event: string, payload: any) => void>(
496-
(...args) => {
497-
this.send(...args);
498-
},
499-
500
500-
);
501496
}
502497

503498
export default new Live();

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

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { Action, AsyncAction } from 'app/overmind';
1414
import { utcToZonedTime } from 'date-fns-tz';
1515

16+
import { captureException } from '@codesandbox/common/lib/utils/analytics/sentry';
1617
import { OPTIMISTIC_COMMENT_ID } from './state';
1718

1819
export const selectCommentsFilter: Action<CommentsFilterOption> = (
@@ -212,7 +213,7 @@ export const createComment: AsyncAction = async ({ state, effects }) => {
212213
}
213214

214215
const id = OPTIMISTIC_COMMENT_ID;
215-
const sandboxId = state.editor.currentSandbox.id;
216+
const sandbox = state.editor.currentSandbox;
216217
const now = utcToZonedTime(new Date().toISOString(), 'Etc/UTC');
217218
let codeReference: CodeReference | null = null;
218219
const selection = state.live.currentSelection;
@@ -228,6 +229,7 @@ export const createComment: AsyncAction = async ({ state, effects }) => {
228229
)
229230
: '',
230231
path: state.editor.currentModule.path,
232+
sandboxVersion: sandbox.version,
231233
};
232234
}
233235

@@ -258,11 +260,11 @@ export const createComment: AsyncAction = async ({ state, effects }) => {
258260
};
259261
const comments = state.comments.comments;
260262

261-
if (!comments[sandboxId]) {
262-
comments[sandboxId] = {};
263+
if (!comments[sandbox.id]) {
264+
comments[sandbox.id] = {};
263265
}
264266

265-
comments[sandboxId][id] = optimisticComment;
267+
comments[sandbox.id][id] = optimisticComment;
266268
// placeholder value until we know the correct values
267269
const {
268270
left,
@@ -299,17 +301,17 @@ export const addComment: AsyncAction<{
299301
}
300302

301303
const id = OPTIMISTIC_COMMENT_ID;
302-
const sandbox = state.editor.currentSandbox;
304+
const sandboxId = state.editor.currentSandbox.id;
303305
const now = utcToZonedTime(new Date().toISOString(), 'Etc/UTC');
304306
const comments = state.comments.comments;
305307

306-
if (!comments[sandbox.id]) {
307-
comments[sandbox.id] = {};
308+
if (!comments[sandboxId]) {
309+
comments[sandboxId] = {};
308310
}
309311

310312
let optimisticComment: CommentFragment;
311-
if (state.comments.comments[sandbox.id][id]) {
312-
optimisticComment = state.comments.comments[sandbox.id][id];
313+
if (state.comments.comments[sandboxId][id]) {
314+
optimisticComment = state.comments.comments[sandboxId][id];
313315
} else {
314316
optimisticComment = {
315317
parentComment: parentCommentId ? { id: parentCommentId } : null,
@@ -327,20 +329,25 @@ export const addComment: AsyncAction<{
327329
references: [],
328330
comments: [],
329331
};
330-
comments[sandbox.id][id] = optimisticComment;
332+
comments[sandboxId][id] = optimisticComment;
331333
}
332334

333335
if (optimisticComment.parentComment) {
334-
comments[sandbox.id][optimisticComment.parentComment.id].comments.push({
336+
comments[sandboxId][optimisticComment.parentComment.id].comments.push({
335337
id,
336338
});
337339
}
338340
state.comments.selectedCommentsFilter = CommentsFilterOption.OPEN;
339341

340-
try {
342+
// The server might be ahead on sandbox version, so we need to try to save
343+
// several times
344+
let tryCount = 0;
345+
346+
async function saveComment() {
347+
tryCount++;
341348
const response = await effects.gql.mutations.createComment({
342349
parentCommentId: parentCommentId || null,
343-
sandboxId: sandbox.id,
350+
sandboxId,
344351
content,
345352
codeReference: optimisticComment.references.length
346353
? optimisticComment.references[0].metadata
@@ -349,15 +356,15 @@ export const addComment: AsyncAction<{
349356

350357
const comment = response.createComment;
351358

352-
delete comments[sandbox.id][id];
353-
comments[sandbox.id][comment.id] = comment;
359+
delete comments[sandboxId][id];
360+
comments[sandboxId][comment.id] = comment;
354361

355362
if (parentCommentId) {
356-
comments[sandbox.id][parentCommentId].comments.push({
363+
comments[sandboxId][parentCommentId].comments.push({
357364
id: comment.id,
358365
});
359-
comments[sandbox.id][parentCommentId].comments.splice(
360-
comments[sandbox.id][parentCommentId].comments.findIndex(
366+
comments[sandboxId][parentCommentId].comments.splice(
367+
comments[sandboxId][parentCommentId].comments.findIndex(
361368
childComment => childComment.id === id
362369
),
363370
1
@@ -366,13 +373,21 @@ export const addComment: AsyncAction<{
366373

367374
if (state.comments.currentCommentId === OPTIMISTIC_COMMENT_ID) {
368375
state.comments.currentCommentId = comment.id;
369-
delete comments[sandbox.id][OPTIMISTIC_COMMENT_ID];
376+
delete comments[sandboxId][OPTIMISTIC_COMMENT_ID];
370377
}
378+
}
379+
try {
380+
await saveComment();
371381
} catch (error) {
372-
effects.notificationToast.error(
373-
'Unable to create your comment, please try again'
374-
);
375-
delete comments[sandbox.id][id];
382+
if (error.response?.data?.error === 'old_version' && tryCount < 3) {
383+
await saveComment();
384+
} else {
385+
captureException(error);
386+
effects.notificationToast.error(
387+
'Unable to create your comment, please try again'
388+
);
389+
delete comments[sandboxId][id];
390+
}
376391
}
377392
};
378393

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ export const saveClicked: AsyncAction = withOwnedSandbox(
448448
);
449449

450450
await Promise.all(
451-
changedModules.map(module => effects.live.saveModule(module.shortid))
451+
changedModules.map(module => effects.live.saveModule(module))
452452
);
453453

454454
if (

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

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

140140
try {
141-
await effects.live.saveModule(module.shortid);
141+
await effects.live.saveModule(module);
142142
const updatedModule = await effects.api.saveModuleCode(
143143
sandbox.id,
144144
module.shortid,

0 commit comments

Comments
 (0)