wed
Version:
Wed is a schema-aware editor for XML documents.
72 lines • 2.79 kB
JavaScript
define(["require", "exports", "wed"], function (require, exports, wed_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Defines a saver that uses IndexedDB to save documents.
*
* This saver stores the document as a "file" into an IndexedDB 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.
*
* @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.
*/
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 = options.getStore();
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.put(name, data).then(() => {
this._saveSuccess(autosave, savingGeneration);
}).catch(() => {
this._fail({ type: undefined, msg: "Failed to save!" });
throw new Error("save failed");
});
}
_recover() {
return this._save(false)
.then(() => true)
.catch(() => false);
}
}
exports.Saver = Saver;
});
// LocalWords: IndexedDB MPL runtime
//# sourceMappingURL=indexeddb.js.map