{"version":3,"file":"hocuspocus-database.cjs","names":["Y"],"sources":["../src/Database.ts"],"sourcesContent":["import type {\n\tExtension,\n\tfetchPayload,\n\tonChangePayload,\n\tonLoadDocumentPayload,\n\tonStoreDocumentPayload,\n\tstorePayload,\n} from \"@hocuspocus/server\";\nimport * as Y from \"yjs\";\n\nexport interface DatabaseConfiguration {\n\t/**\n\t * Pass a Promise to retrieve updates from your database. The Promise should resolve to\n\t * an array of items with Y.js-compatible binary data.\n\t */\n\tfetch: (data: fetchPayload) => Promise<Uint8Array | null>;\n\t/**\n\t * Pass a function to store updates in your database.\n\t */\n\tstore: (data: storePayload) => Promise<void>;\n}\n\nexport class Database implements Extension {\n\t/**\n\t * Default configuration\n\t */\n\tconfiguration: DatabaseConfiguration = {\n\t\tfetch: async () => null,\n\t\tstore: async () => {},\n\t};\n\n\t/**\n\t * Constructor\n\t */\n\tconstructor(configuration: Partial<DatabaseConfiguration>) {\n\t\tthis.configuration = {\n\t\t\t...this.configuration,\n\t\t\t...configuration,\n\t\t};\n\t}\n\n\t/**\n\t * Get stored data from the database.\n\t */\n\tasync onLoadDocument(data: onLoadDocumentPayload): Promise<any> {\n\t\tconst update = await this.configuration.fetch(data);\n\n\t\tif (update) {\n\t\t\tY.applyUpdate(data.document, update);\n\t\t}\n\t}\n\n\t/**\n\t * Store new updates in the database.\n\t */\n\tasync onStoreDocument(data: onStoreDocumentPayload) {\n\t\tawait this.configuration.store({\n\t\t\t...data,\n\t\t\tstate: Buffer.from(Y.encodeStateAsUpdate(data.document)),\n\t\t});\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsBA,IAAa,WAAb,MAA2C;;;;CAY1C,YAAY,eAA+C;uBARpB;GACtC,OAAO,YAAY;GACnB,OAAO,YAAY;GACnB;AAMA,OAAK,gBAAgB;GACpB,GAAG,KAAK;GACR,GAAG;GACH;;;;;CAMF,MAAM,eAAe,MAA2C;EAC/D,MAAM,SAAS,MAAM,KAAK,cAAc,MAAM,KAAK;AAEnD,MAAI,OACH,KAAE,YAAY,KAAK,UAAU,OAAO;;;;;CAOtC,MAAM,gBAAgB,MAA8B;AACnD,QAAM,KAAK,cAAc,MAAM;GAC9B,GAAG;GACH,OAAO,OAAO,KAAKA,IAAE,oBAAoB,KAAK,SAAS,CAAC;GACxD,CAAC"}