UNPKG

@wordpress/sync

Version:
164 lines (162 loc) 5.89 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/sync/src/manager.ts var manager_exports = {}; __export(manager_exports, { createSyncManager: () => createSyncManager }); module.exports = __toCommonJS(manager_exports); var Y = __toESM(require("yjs")); var import_config = require("./config"); var import_persistence = require("./persistence"); var import_providers = require("./providers"); var import_undo_manager = require("./undo-manager"); var import_utils = require("./utils"); function createSyncManager() { const entityStates = /* @__PURE__ */ new Map(); const undoManager = (0, import_undo_manager.createUndoManager)(); async function loadEntity(syncConfig, objectType, objectId, record, handlers) { const providerCreators = (0, import_providers.getProviderCreators)(); if (0 === providerCreators.length) { return; } const entityId = getEntityId(objectType, objectId); if (entityStates.has(entityId)) { return; } const ydoc = (0, import_utils.createYjsDoc)({ objectType }); const recordMap = ydoc.getMap(import_config.CRDT_RECORD_MAP_KEY); const unload = () => { providerResults.forEach((result) => result.destroy()); recordMap.unobserveDeep(onRecordUpdate); ydoc.destroy(); entityStates.delete(entityId); }; const onRecordUpdate = (_events, transaction) => { if (transaction.local && !(transaction.origin instanceof Y.UndoManager)) { return; } void updateEntityRecord(objectType, objectId); }; undoManager.addToScope(recordMap); const entityState = { handlers, objectId, objectType, syncConfig, unload, ydoc }; entityStates.set(entityId, entityState); const providerResults = await Promise.all( providerCreators.map( (create) => create(objectType, objectId, ydoc) ) ); recordMap.observeDeep(onRecordUpdate); const isInvalid = applyPersistedCrdtDoc(syncConfig, ydoc, record); if (isInvalid) { ydoc.transact(() => { syncConfig.applyChangesToCRDTDoc(ydoc, record); }, import_config.LOCAL_SYNC_MANAGER_ORIGIN); const meta = createEntityMeta(objectType, objectId); handlers.editRecord({ meta }); handlers.saveRecord(); } } function unloadEntity(objectType, objectId) { entityStates.get(getEntityId(objectType, objectId))?.unload(); } function getEntityId(objectType, objectId) { return `${objectType}_${objectId}`; } function applyPersistedCrdtDoc(syncConfig, targetDoc, record) { if (!syncConfig.supports?.crdtPersistence) { return true; } const tempDoc = (0, import_persistence.getPersistedCrdtDoc)(record); if (!tempDoc) { return true; } const update = Y.encodeStateAsUpdateV2(tempDoc); targetDoc.transact(() => { Y.applyUpdateV2(targetDoc, update); }, import_config.LOCAL_SYNC_MANAGER_ORIGIN); const changes = syncConfig.getChangesFromCRDTDoc(tempDoc, record); tempDoc.destroy(); return Object.keys(changes).length > 0; } function updateCRDTDoc(objectType, objectId, changes, origin) { const entityId = getEntityId(objectType, objectId); const entityState = entityStates.get(entityId); if (!entityState) { return; } const { syncConfig, ydoc } = entityState; ydoc.transact(() => { syncConfig.applyChangesToCRDTDoc(ydoc, changes); }, origin); } async function updateEntityRecord(objectType, objectId) { const entityId = getEntityId(objectType, objectId); const entityState = entityStates.get(entityId); if (!entityState) { return; } const { handlers, syncConfig, ydoc } = entityState; const changes = syncConfig.getChangesFromCRDTDoc( ydoc, await handlers.getEditedRecord() ); if (0 === Object.keys(changes).length) { return; } handlers.editRecord(changes); } function createEntityMeta(objectType, objectId) { const entityId = getEntityId(objectType, objectId); const entityState = entityStates.get(entityId); if (!entityState?.syncConfig.supports?.crdtPersistence) { return {}; } return (0, import_persistence.createPersistedCRDTDoc)(entityState.ydoc); } return { createMeta: createEntityMeta, load: loadEntity, undoManager, unload: unloadEntity, update: updateCRDTDoc }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createSyncManager }); //# sourceMappingURL=manager.js.map