UNPKG

@samepage/internal

Version:

Utilities used across modules - not meant for use by users directly

79 lines 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const parseZodError_1 = tslib_1.__importDefault(require("../utils/parseZodError")); const unwrapSchema_1 = tslib_1.__importDefault(require("../utils/unwrapSchema")); const apiClient_1 = tslib_1.__importDefault(require("./apiClient")); const binaryToBase64_1 = tslib_1.__importDefault(require("./binaryToBase64")); const dispatchAppEvent_1 = tslib_1.__importDefault(require("./dispatchAppEvent")); const sendExtensionError_1 = tslib_1.__importDefault(require("./sendExtensionError")); const types_1 = require("./types"); const automerge_1 = tslib_1.__importDefault(require("automerge")); const saveAndApply = ({ notebookPageId, doc, applyState, decodeState = async (id, state) => applyState === null || applyState === void 0 ? void 0 : applyState(id, state.$body), properties = {}, }) => { const docToApply = (0, unwrapSchema_1.default)(doc); return types_1.zSamePageSchema .safeParseAsync(docToApply) .then((parseResult) => { if (parseResult.success) { return decodeState(notebookPageId, { $body: parseResult.data, ...properties, }); } else { // let's not throw yet - let's see how many emails this generates first - can revisit this in a few months // This is the previous behavior (0, sendExtensionError_1.default)({ type: `State received from other notebook was corrupted`, data: { error: parseResult.error, message: (0, parseZodError_1.default)(parseResult.error), input: docToApply, }, }); return decodeState(notebookPageId, { $body: docToApply, ...properties, }); } }) .then(async () => { if (!automerge_1.default.isFrozen(doc)) { // I think it's safe to say that if another change comes in, freezing this doc, it's outdated and not worth saving? // this could have bad implications on history though - TODO // - not that bad, because currently our document stores full history. await (0, apiClient_1.default)({ method: "save-page-version", notebookPageId, state: (0, binaryToBase64_1.default)(automerge_1.default.save(doc)), }).catch((e) => { (0, dispatchAppEvent_1.default)({ type: "log", id: "update-version-failure", content: `Failed to broadcast new version: ${e.message}`, intent: "warning", }); }); } (0, dispatchAppEvent_1.default)({ type: "log", id: "update-success", content: `Applied update`, intent: "debug", }); }) .catch(async (e) => { const data = await (0, sendExtensionError_1.default)({ type: `Failed to apply change`, error: e, }); (0, dispatchAppEvent_1.default)({ type: "log", id: "update-failure", content: `Failed to apply new change - Error report ${data.messageId} has been sent to support@samepage.network`, intent: "warning", }); }); }; exports.default = saveAndApply; //# sourceMappingURL=saveAndApply.js.map