Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): Treat head as optional to avoid errors #12936

Merged
merged 6 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/datastore/__tests__/outbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ describe('Outbox tests', () => {
});
});

it('Should not throw errors when Outbox.dequeue is called on an empty outbox', async () => {
const modelData: ModelType = JSON.parse('{}');
const response = {
...modelData,
_version: 1,
_lastChangedAt: Date.now(),
_deleted: false,
};

await Storage.runExclusive(async s => {
const head = await outbox.peek(s);

expect(head).toBeFalsy();
await expect(outbox.dequeue(s, response, TransformerMutationType.CREATE))
.resolves;
});
});

it('Should sync the _version from a mutation response to other items with the same `id` in the queue', async () => {
const last = await DataStore.query(Model, modelId);

Expand Down
6 changes: 4 additions & 2 deletions packages/datastore/src/sync/outbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ class MutationEventOutbox {
await this.syncOutboxVersionsOnDequeue(storage, record, head, recordOp!);
}

await storage.delete(head);
if (head) {
await storage.delete(head);
}
this.inProgressMutationEventId = undefined!;

return head;
Expand Down Expand Up @@ -169,7 +171,7 @@ class MutationEventOutbox {
head: PersistentModel,
recordOp: string
): Promise<void> {
if (head.operation !== recordOp) {
if (head?.operation !== recordOp) {
return;
}

Expand Down
Loading