Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Wiaderek authored and Tomasz Wiaderek committed Jan 15, 2024
1 parent 880e087 commit 1408854
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ErrorLogMessage, Loggable, LogMessage, ValidationErrorLogMessage } from '@src/core/logger';

export class MongoTransactionErrorLoggable implements Loggable {
constructor(private readonly error: Error) {}
private error: Error | undefined;

constructor(private readonly err: unknown) {
if (err instanceof Error) {
this.error = err;
}
}

getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage {
return {
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/modules/tldraw/repo/tldraw-board.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ describe('TldrawBoardRepo', () => {
jest.useFakeTimers();
});

afterEach(() => {
jest.resetAllMocks();
});

afterAll(async () => {
await app.close();
});
Expand Down
23 changes: 21 additions & 2 deletions apps/server/src/modules/tldraw/repo/tldraw.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ describe('TldrawRepo', () => {

describe('create', () => {
describe('when called', () => {
it('should create new drawing node', async () => {
const setup = async () => {
const drawing = tldrawEntityFactory.build();

await repo.create(drawing);
em.clear();

return {
drawing,
};
};

it('should create new drawing node', async () => {
const { drawing } = await setup();

const result = await em.find(TldrawDrawing, {});

expect(result[0]._id).toEqual(drawing._id);
Expand Down Expand Up @@ -84,10 +93,20 @@ describe('TldrawRepo', () => {

describe('delete', () => {
describe('when finding by docName and deleting all records', () => {
it('should delete all records', async () => {

Check failure on line 96 in apps/server/src/modules/tldraw/repo/tldraw.repo.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

Delete `⏎`
const setup = async () => {
const drawing = tldrawEntityFactory.build();

await repo.create(drawing);

return {
drawing,
};
};

it('should delete all records', async () => {
const { drawing } = await setup();

const results = await repo.findByDocName(drawing.docName);
await repo.delete(results);

Expand Down
32 changes: 19 additions & 13 deletions apps/server/src/modules/tldraw/repo/y-mongodb.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,22 @@ describe('YMongoDb', () => {
const setup = () => {
const applyUpdateSpy = jest.spyOn(Yjs, 'applyUpdate').mockReturnValue();

const drawing1 = tldrawEntityFactory.build({ clock: 1, part: undefined });
const drawing2 = tldrawEntityFactory.build({ clock: 2, part: undefined });
const drawing3 = tldrawEntityFactory.build({ clock: 3, part: undefined });
const drawing4 = tldrawEntityFactory.build({ clock: 4, part: undefined });

return {
applyUpdateSpy,
drawing1,
drawing2,
drawing3,
drawing4,
};
};

it('should merge multiple documents with the same name in the database into two (one main document and one with update)', async () => {
const { applyUpdateSpy } = setup();
const drawing1 = tldrawEntityFactory.build({ clock: 1, part: undefined });
const drawing2 = tldrawEntityFactory.build({ clock: 2, part: undefined });
const drawing3 = tldrawEntityFactory.build({ clock: 3, part: undefined });
const drawing4 = tldrawEntityFactory.build({ clock: 4, part: undefined });
const { applyUpdateSpy, drawing1, drawing2, drawing3, drawing4 } = setup();

await em.persistAndFlush([drawing1, drawing2, drawing3, drawing4]);
em.clear();
Expand Down Expand Up @@ -155,16 +160,20 @@ describe('YMongoDb', () => {
const setup = () => {
const applyUpdateSpy = jest.spyOn(Yjs, 'applyUpdate').mockReturnValue();

const drawing1 = tldrawEntityFactory.build({ clock: 1, part: 1 });
const drawing2 = tldrawEntityFactory.build({ clock: 1, part: 2 });
const drawing3 = tldrawEntityFactory.build({ clock: 2, part: 1 });

return {
applyUpdateSpy,
drawing1,
drawing2,
drawing3,
};
};

it('should return ydoc from the database', async () => {
const { applyUpdateSpy } = setup();
const drawing1 = tldrawEntityFactory.build({ clock: 1, part: 1 });
const drawing2 = tldrawEntityFactory.build({ clock: 1, part: 2 });
const drawing3 = tldrawEntityFactory.build({ clock: 2, part: 1 });
const { applyUpdateSpy, drawing1, drawing2, drawing3 } = setup();

await em.persistAndFlush([drawing1, drawing2, drawing3]);
em.clear();
Expand All @@ -175,10 +184,7 @@ describe('YMongoDb', () => {
});

it('should not return ydoc if part is missing', async () => {
const { applyUpdateSpy } = setup();
const drawing1 = tldrawEntityFactory.build({ clock: 1, part: 1 });
const drawing2 = tldrawEntityFactory.build({ clock: 1, part: 3 });
const drawing3 = tldrawEntityFactory.build({ clock: 1, part: 4 });
const { applyUpdateSpy, drawing1, drawing2, drawing3 } = setup();

await em.persistAndFlush([drawing1, drawing2, drawing3]);
em.clear();
Expand Down
44 changes: 30 additions & 14 deletions apps/server/src/modules/tldraw/repo/y-mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ export class YMongodb {
let currentPartId: number | undefined = doc.part;
for (let i = docIndex + 1; i < docs.length; i += 1) {
const part = docs[i];
if (part.clock === doc.clock) {
if (part.part === undefined || currentPartId !== part.part - 1) {
throw new Error('Could not merge updates together because a part is missing');
}
parts.push(Buffer.from(part.value.buffer));
currentPartId = part.part;
} else {

if (!this.isSameClock(part, doc)) {
break;
}

this.checkIfCurrentPartId(part, currentPartId);

parts.push(Buffer.from(part.value.buffer));
currentPartId = part.part;
}

return parts;
Expand Down Expand Up @@ -165,7 +165,7 @@ export class YMongodb {
}

private async getCurrentUpdateClock(docName: string): Promise<number> {
return this.getMongoBulkData(
const updates = await this.getMongoBulkData(
{
...this.createDocumentUpdateKey(docName, 0),
clock: {
Expand All @@ -174,12 +174,11 @@ export class YMongodb {
},
},
{ reverse: true, limit: 1 }
).then((updates) => {
if (updates.length === 0 || updates[0].clock == null) {
return -1;
}
return updates[0].clock;
});
);

const clock = this.extractClock(updates);

return clock;
}

private async writeStateVector(docName: string, sv: Uint8Array, clock: number): Promise<void> {
Expand Down Expand Up @@ -251,6 +250,23 @@ export class YMongodb {
return clock;
}

private isSameClock(doc1: TldrawDrawing, doc2: TldrawDrawing): boolean {
return doc1.clock === doc2.clock;
}

private checkIfCurrentPartId(part: TldrawDrawing, currentPartId: number | undefined) {
if (part.part === undefined || currentPartId !== part.part - 1) {
throw new Error('Could not merge updates together because a part is missing');
}
}

private extractClock(updates: TldrawDrawing[]) {
if (updates.length === 0 || updates[0].clock == null) {
return -1;
}
return updates[0].clock;
}

/**
* Create a unique key for a update message.
*/
Expand Down

0 comments on commit 1408854

Please sign in to comment.