Skip to content

Commit

Permalink
fix(@aws-amplify/datastore): use runExclusive when enqueuing (#6828)
Browse files Browse the repository at this point in the history
  • Loading branch information
amhinson authored Sep 18, 2020
1 parent 5651365 commit 26ce5df
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
86 changes: 44 additions & 42 deletions packages/datastore/src/sync/outbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,56 @@ class MutationEventOutbox {
storage: Storage,
mutationEvent: MutationEvent
): Promise<void> {
const mutationEventModelDefinition = this.schema.namespaces[SYNC].models[
'MutationEvent'
];

const predicate = ModelPredicateCreator.createFromExisting<MutationEvent>(
mutationEventModelDefinition,
c =>
c
.modelId('eq', mutationEvent.modelId)
.id('ne', this.inProgressMutationEventId)
);
storage.runExclusive(async s => {
const mutationEventModelDefinition = this.schema.namespaces[SYNC].models[
'MutationEvent'
];

const [first] = await storage.query(this.MutationEvent, predicate);
const predicate = ModelPredicateCreator.createFromExisting<MutationEvent>(
mutationEventModelDefinition,
c =>
c
.modelId('eq', mutationEvent.modelId)
.id('ne', this.inProgressMutationEventId)
);

if (first === undefined) {
await storage.save(mutationEvent, undefined, this.ownSymbol);
return;
}
const [first] = await s.query(this.MutationEvent, predicate);

const { operation: incomingMutationType } = mutationEvent;
if (first === undefined) {
await s.save(mutationEvent, undefined, this.ownSymbol);
return;
}

if (first.operation === TransformerMutationType.CREATE) {
if (incomingMutationType === TransformerMutationType.DELETE) {
// delete all for model
await storage.delete(this.MutationEvent, predicate);
const { operation: incomingMutationType } = mutationEvent;

if (first.operation === TransformerMutationType.CREATE) {
if (incomingMutationType === TransformerMutationType.DELETE) {
// delete all for model
await s.delete(this.MutationEvent, predicate);
} else {
// first gets updated with incoming's data, condition intentionally skiped
await s.save(
this.MutationEvent.copyOf(first, draft => {
draft.data = mutationEvent.data;
}),
undefined,
this.ownSymbol
);
}
} else {
// first gets updated with incoming's data, condition intentionally skiped
await storage.save(
this.MutationEvent.copyOf(first, draft => {
draft.data = mutationEvent.data;
}),
undefined,
this.ownSymbol
);
}
} else {
const { condition: incomingConditionJSON } = mutationEvent;
const incomingCondition = JSON.parse(incomingConditionJSON);

// If no condition
if (Object.keys(incomingCondition).length === 0) {
// delete all for model
await storage.delete(this.MutationEvent, predicate);
}
const { condition: incomingConditionJSON } = mutationEvent;
const incomingCondition = JSON.parse(incomingConditionJSON);

// If no condition
if (Object.keys(incomingCondition).length === 0) {
// delete all for model
await s.delete(this.MutationEvent, predicate);
}

// Enqueue new one
await storage.save(mutationEvent, undefined, this.ownSymbol);
}
// Enqueue new one
await s.save(mutationEvent, undefined, this.ownSymbol);
}
});
}

public async dequeue(storage: StorageFacade): Promise<MutationEvent> {
Expand Down
2 changes: 1 addition & 1 deletion packages/datastore/src/sync/processors/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class MutationProcessor {
: null,
});
} catch (err) {
logger.warn("failed to execute errorHandler", err);
logger.warn('failed to execute errorHandler', err);
} finally {
// Return empty tuple, dequeues the mutation
return error.data
Expand Down

0 comments on commit 26ce5df

Please sign in to comment.