Skip to content

Commit

Permalink
feat: added registerListener
Browse files Browse the repository at this point in the history
  • Loading branch information
froid1911 committed Jan 23, 2024
1 parent 1b05ed8 commit ce7a7ee
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
31 changes: 31 additions & 0 deletions api/src/modules/DocumentDrive/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import type { Prisma } from "@prisma/client";
import {
DocumentDriveServer,
DriveInput,
Listener,
ListenerRevision,
MemoryStorage,
PrismaStorage,
generateUUID,
} from "document-drive";
import * as DocumentModelsLibs from "document-model-libs/document-models";
import { module as DocumentModelLib } from "document-model/document-model";
import { DocumentModel, Operation } from "document-model/dist/browser/document";
import { PullResponderTransmitter } from "document-drive/src/transmitter/pull-responder";
import {
utils as DocumentDriveUtils,
ListenerFilter,
actions,
reducer,
} from "document-model-libs/dist/document-drive";

export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
const documentModels = [
DocumentModelLib,
Expand Down Expand Up @@ -144,5 +153,27 @@ export function getDocumentDriveCRUD(prisma: Prisma.TransactionClient) {
revisions
);
},

registerListener: async (driveId: string, filter: ListenerFilter) => {
const uuid = generateUUID();
const listener: Listener = {
block: false,
callInfo: {
data: "",
name: "PullResponder",
transmitterType: "PullResponder",
},
filter,
label: `Pullresponder #${uuid}`,
listenerId: uuid,
system: false,
};
const action = actions.addListener({
listener,
});

await driveServer.addDriveOperations(driveId, [action]);
return listener;
},
};
}
54 changes: 54 additions & 0 deletions api/src/modules/DocumentDrive/mutations/registerListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { list, mutationField, nonNull } from "nexus";
import { InputStrandUpdate, ListenerRevision } from "../definitions";
import { OperationScope } from "document-model/document";
import {
ListenerRevision as IListenerRevision,
UpdateStatus,
} from "document-drive";

export const registerListener = mutationField("registerListener", {
type: Listener,
args: {
filter: list(nonNull(InputStrandUpdate)),
},
resolve: async (_parent, { filter }, ctx) => {
//@todo: get connect drive server from ctx and apply updates

if (!strands || strands?.length === 0) return [];
const listenerRevisions: IListenerRevision[] = [];
const results = await Promise.all(
strands.map(async (s) => {
const operations = s.operations?.map((o) => {
const op = {
scope: s.scope as OperationScope,
branch: s.branch,
...o,
input: JSON.parse(o.input),
};

return op;
});
try {
const result = await ctx.prisma.document.registerListener(
s.driveId,
s.documentId,
operations
);

listenerRevisions.push({
branch: s.branch,
documentId: s.documentId,
driveId: s.driveId,
revision: result.operations.pop().revision,
scope: s.scope as OperationScope,
status: (result.error ? "ERROR" : "SUCCESS") as UpdateStatus,
});
} catch (e) {
console.log(e);
}
})
);

return listenerRevisions;
},
});

0 comments on commit ce7a7ee

Please sign in to comment.