UNPKG

wed

Version:

Wed is a schema-aware editor for XML documents.

122 lines 4.44 kB
/** * Data saving functionality, using localforage. Note that this saver is mainly * designed for demonstration purposes. * * @author Louis-Dominique Dubeau * @license MPL 2.0 * @copyright Mangalam Research Center for Buddhist Languages */ var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; define(["require", "exports", "localforage", "wed"], function (require, exports, localforage, wed_1) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); localforage = __importStar(localforage); /** * Create a localforage store instance. If you have code that needs to * access the store that this saver uses. You need to call this * function first. * * @returns A configured localforage instance. */ function config() { return localforage.createInstance({ name: "wed", storeName: "files", }); } exports.config = config; /** * Utility function used to make file records. * * @param name The name of the file. * * @param data The data to save. * * @returns The record. */ function makeFileRecord(name, data) { const ret = Object.create(null); ret.version = 1; ret.name = name; ret.data = data; ret.uploaded = new Date(); ret.saved = "never"; ret.downloaded = "never"; return ret; } exports.makeFileRecord = makeFileRecord; /** * Defines a saver that uses localforage to save documents. * * This saver stores the document as a "file" into a localforage instance. The * objects are not really files but similar to files. Henceforth, the name * "file" will be used without quotes to refer to the objects stored. */ class Saver extends wed_1.saver.Saver { /** * @param runtime The runtime under which this saver is created. * * @param version The version of wed for which this object is created. * * @param dataUpdater The updater that the editor created for its data tree. * * @param dataTree The editor's data tree. * * @param options The options specific to this class. */ constructor(runtime, version, dataUpdater, dataTree, options) { super(runtime, version, dataUpdater, dataTree, options); this.initPromise = Promise.resolve(); this.initialized = true; this.failed = false; this.name = options.name; this.store = config(); this.setAutosaveInterval(5 * 60 * 1000); } init() { // It is initialized from the get-go. return this.initPromise; } _save(autosave) { return Promise.resolve().then(() => { if (!this.initialized) { return; } return this._update(this.name, this.getData(), autosave, this.currentGeneration) // All save errors produced by this saver are handled with this._fail. .catch(() => undefined); }); } _update(name, data, autosave, savingGeneration) { return this.store.getItem(name).then(((rec) => { if (rec.version !== 1) { throw new Error(`unexpected record version number: ${rec.version}`); } rec.data = data; rec.saved = new Date(); return this.store.setItem(name, rec).then(() => { this._saveSuccess(autosave, savingGeneration); }).catch(() => { const error = { type: undefined, msg: "Failed to save!" }; this._fail(error); throw new Error("save failed"); }); // tslint:disable-next-line:no-any })); } _recover() { return this._save(false) .then(() => true) .catch(() => false); } } exports.Saver = Saver; }); // LocalWords: localforage MPL runtime //# sourceMappingURL=localforage.js.map