Skip to content

Commit

Permalink
Add onLoadDocument appluUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed Aug 19, 2024
1 parent 897a7b5 commit ef984a3
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions server/src/plugins/crdt/collaboration-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { isDeployed } from '@app/config/env';
import { getRedisExtension } from '@app/plugins/crdt/redis';
import { isNotNull } from '@app/functions/guards';
import { getApiExtension } from '@app/plugins/crdt/api/extension';
import { getDocument } from '@app/plugins/crdt/get-document';
import { applyUpdate } from 'yjs';

const log = getLogger('collaboration');

Expand Down Expand Up @@ -35,5 +37,30 @@ export const collaborationServer = Server.configure({
}
},

onLoadDocument: async ({ context, document }) => {
if (!isConnectionContext(context)) {
log.error({ msg: 'Tried to load document without context' });
throw new Error('Invalid context');
}

if (!document.isEmpty('content')) {
log.info({ msg: 'Document already loaded' });

return document;
}

const { behandlingId, dokumentId, req } = context;

const res = await getDocument(req, behandlingId, dokumentId);

log.info({ msg: 'Fetched document', data: { behandlingId, dokumentId } });

const state = Buffer.from(res.data, 'base64');

applyUpdate(document, state);

return document;
},

extensions: isDeployed ? [getRedisExtension(), getApiExtension()].filter(isNotNull) : [getApiExtension()],
});

0 comments on commit ef984a3

Please sign in to comment.