{
  "version": 3,
  "sources": ["../src/utils.ts"],
  "sourcesContent": ["/**\n * External dependencies\n */\nimport * as Y from 'yjs';\nimport * as buffer from 'lib0/buffer';\n\n/**\n * Internal dependencies\n */\nimport {\n\tCRDT_DOC_META_PERSISTENCE_KEY,\n\tCRDT_DOC_VERSION,\n\tCRDT_STATE_MAP_KEY,\n\tCRDT_STATE_MAP_SAVED_AT_KEY as SAVED_AT_KEY,\n\tCRDT_STATE_MAP_SAVED_BY_KEY as SAVED_BY_KEY,\n\tCRDT_STATE_MAP_VERSION_KEY as VERSION_KEY,\n} from './config';\nimport type { CRDTDoc } from './types';\n\n// An object representation of CRDT document metadata.\ntype DocumentMeta = Record< string, DocumentMetaValue >;\ntype DocumentMetaValue = boolean | number | string;\n\n/**\n * Creates a new Y.Doc instance with the given document metadata.\n *\n * @param {DocumentMeta} documentMeta Optional metadata to associate with the\n *                                    document. Metadata is not persisted.\n */\nexport function createYjsDoc( documentMeta: DocumentMeta = {} ): CRDTDoc {\n\t// Convert the object representation of CRDT document metadata to a map.\n\t// Document metadata is passed to the Y.Doc constructor and stored in its\n\t// `meta` property. It is not synced to peers or persisted with the document.\n\t// It is just a place to store transient information about this doc instance.\n\tconst metaMap = new Map< string, DocumentMetaValue >(\n\t\tObject.entries( documentMeta )\n\t);\n\n\t// IMPORTANT: Do not add update the document itself to avoid generating updates\n\t// before observers are attached. Add initial updates in `initializeYjsDoc`.\n\treturn new Y.Doc( { meta: metaMap } );\n}\n\n/**\n * Initializes a Y.Doc instance with the necessary CRDT state for our use case.\n *\n * @param {Y.Doc} ydoc Y.Doc instance to initialize.\n */\nexport function initializeYjsDoc( ydoc: CRDTDoc ): void {\n\tconst stateMap = ydoc.getMap( CRDT_STATE_MAP_KEY );\n\tstateMap.set( VERSION_KEY, CRDT_DOC_VERSION );\n}\n\n/**\n * Record that the entity was saved (persisted to the database) in the CRDT\n * document record metadata.\n *\n * @param {CRDTDoc} ydoc CRDT document.\n */\nexport function markEntityAsSaved( ydoc: CRDTDoc ): void {\n\tconst recordMeta = ydoc.getMap( CRDT_STATE_MAP_KEY );\n\trecordMeta.set( SAVED_AT_KEY, Date.now() );\n\trecordMeta.set( SAVED_BY_KEY, ydoc.clientID );\n}\n\nfunction pseudoRandomID(): number {\n\treturn Math.floor( Math.random() * 1000000000 );\n}\n\nexport function serializeCrdtDoc( crdtDoc: CRDTDoc ): string {\n\treturn JSON.stringify( {\n\t\tdocument: buffer.toBase64( Y.encodeStateAsUpdateV2( crdtDoc ) ),\n\t\tupdateId: pseudoRandomID(), // helps with debugging\n\t} );\n}\n\nexport function deserializeCrdtDoc(\n\tserializedCrdtDoc: string\n): CRDTDoc | null {\n\ttry {\n\t\tconst { document } = JSON.parse( serializedCrdtDoc );\n\n\t\t// Mark this document as from persistence.\n\t\tconst docMeta: DocumentMeta = {\n\t\t\t[ CRDT_DOC_META_PERSISTENCE_KEY ]: true,\n\t\t};\n\n\t\t// Apply the document as an update against a new (temporary) Y.Doc.\n\t\tconst ydoc = createYjsDoc( docMeta );\n\t\tconst yupdate = buffer.fromBase64( document );\n\t\tY.applyUpdateV2( ydoc, yupdate );\n\n\t\t// Overwrite the client ID (which is from a previous session) with a random\n\t\t// client ID. Deserialized documents should not be used directly. Instead,\n\t\t// their state should be applied to another in-use document.\n\t\tydoc.clientID = pseudoRandomID();\n\n\t\treturn ydoc;\n\t} catch {\n\t\treturn null;\n\t}\n}\n"],
  "mappings": ";AAGA,YAAY,OAAO;AACnB,YAAY,YAAY;AAKxB;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA,+BAA+B;AAAA,EAC/B,+BAA+B;AAAA,EAC/B,8BAA8B;AAAA,OACxB;AAaA,SAAS,aAAc,eAA6B,CAAC,GAAa;AAKxE,QAAM,UAAU,IAAI;AAAA,IACnB,OAAO,QAAS,YAAa;AAAA,EAC9B;AAIA,SAAO,IAAM,MAAK,EAAE,MAAM,QAAQ,CAAE;AACrC;AAOO,SAAS,iBAAkB,MAAsB;AACvD,QA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