Skip to content

Commit

Permalink
feat: add pushUpdates strands to queue at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
acaldas committed Jun 5, 2024
1 parent 6297e12 commit b334196
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions api/src/modules/document-drive/drive-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,55 +370,50 @@ export const pushUpdates = mutationField('pushUpdates', {
strands: list(nonNull(InputStrandUpdate)),
},
resolve: async (_parent, { strands }, ctx: Context) => {
logger.info('pushUpdates')
logger.info('pushUpdates');
if (!strands || strands?.length === 0) return [];

try {
const listenerRevisions: IListenerRevision[] = [];

const sortedStrands = strands.reduce(
(acc, curr) =>
curr.documentId ? [...acc, curr] : [curr, ...acc],
[] as typeof strands
);

for (const s of sortedStrands) {
const operations =
s.operations?.map((o) => ({
...o,
input: JSON.parse(o.input),
skip: o.skip ?? 0,
scope: s.scope as OperationScope,
branch: "main",
})) ?? [];
const listenerRevisions: IListenerRevision[] = await Promise.all(strands.map(async (s) => {
const operations = s.operations?.map((o) => ({
...o,
input: JSON.parse(o.input),
skip: o.skip ?? 0,
scope: s.scope as OperationScope,
branch: "main",
})) ?? [];

const result = await ctx.prisma.document.pushUpdates(
s.driveId,
operations as Operation<DocumentDriveAction>[],
s.documentId ?? undefined
s.documentId ?? undefined,
);

if (result.status !== "SUCCESS") logger.error(result.error);

const revision =
result.document?.operations[s.scope as OperationScope]
.slice()
.pop()?.index ?? -1;
const revision = result.document?.operations[s.scope as OperationScope]
.slice()
.pop()?.index ?? -1;

listenerRevisions.push({
return {
revision,
branch: s.branch,
documentId: s.documentId ?? "",
driveId: s.driveId,
scope: s.scope as OperationScope,
status: result.status,
error: result.error?.message,
});
}
};
}));

return listenerRevisions;
} catch (e: any) {
throw new DocumentDriveError({ code: 500, message: e.message ?? "Failed to push updates", logging: true, context: e })
throw new DocumentDriveError({
code: 500,
message: e.message ?? "Failed to push updates",
logging: true,
context: e
});
}
},
});
Expand Down

0 comments on commit b334196

Please sign in to comment.