fabricjs-document-engine 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -10
- package/dist/document/document-format.d.cts +1 -0
- package/dist/document/document-format.d.ts +1 -0
- package/dist/document/validate-document.cjs +1 -0
- package/dist/document/validate-document.js +1 -0
- package/dist/engine/create-document-engine.cjs +27 -14
- package/dist/engine/create-document-engine.d.cts +11 -5
- package/dist/engine/create-document-engine.d.ts +11 -5
- package/dist/engine/create-document-engine.js +27 -14
- package/dist/engine/errors.cjs +5 -0
- package/dist/engine/errors.d.cts +4 -1
- package/dist/engine/errors.d.ts +4 -1
- package/dist/engine/errors.js +5 -1
- package/dist/history/create-history.cjs +3 -1
- package/dist/history/create-history.js +3 -1
- package/dist/index.cjs +3 -0
- package/dist/index.d.cts +8 -3
- package/dist/index.d.ts +8 -3
- package/dist/index.js +3 -2
- package/dist/save/autosave-scheduler.cjs +28 -0
- package/dist/save/autosave-scheduler.d.cts +6 -0
- package/dist/save/autosave-scheduler.d.ts +6 -0
- package/dist/save/autosave-scheduler.js +28 -0
- package/dist/save/retry.cjs +33 -0
- package/dist/save/retry.d.cts +7 -0
- package/dist/save/retry.d.ts +7 -0
- package/dist/save/retry.js +31 -0
- package/dist/save/save-controller.cjs +148 -0
- package/dist/save/save-controller.d.cts +20 -0
- package/dist/save/save-controller.d.ts +20 -0
- package/dist/save/save-controller.js +148 -0
- package/dist/save/unsaved-changes-warning.cjs +12 -0
- package/dist/save/unsaved-changes-warning.d.cts +6 -0
- package/dist/save/unsaved-changes-warning.d.ts +6 -0
- package/dist/save/unsaved-changes-warning.js +12 -0
- package/dist/storage/key-value-storage.cjs +35 -0
- package/dist/storage/key-value-storage.d.cts +14 -0
- package/dist/storage/key-value-storage.d.ts +14 -0
- package/dist/storage/key-value-storage.js +35 -0
- package/dist/storage/local-storage.cjs +24 -0
- package/dist/storage/local-storage.d.cts +8 -0
- package/dist/storage/local-storage.d.ts +8 -0
- package/dist/storage/local-storage.js +24 -0
- package/dist/storage/memory-storage.cjs +17 -0
- package/dist/storage/memory-storage.d.cts +4 -0
- package/dist/storage/memory-storage.d.ts +4 -0
- package/dist/storage/memory-storage.js +17 -0
- package/dist/storage/storage-contract.d.cts +14 -0
- package/dist/storage/storage-contract.d.ts +14 -0
- package/dist/storage.cjs +7 -0
- package/dist/storage.d.cts +5 -0
- package/dist/storage.d.ts +5 -0
- package/dist/storage.js +4 -0
- package/package.json +14 -3
package/dist/index.cjs
CHANGED
|
@@ -4,9 +4,12 @@ const require_validate_document = require("./document/validate-document.cjs");
|
|
|
4
4
|
const require_errors = require("./engine/errors.cjs");
|
|
5
5
|
const require_create_document_engine = require("./engine/create-document-engine.cjs");
|
|
6
6
|
const require_keyboard_shortcuts = require("./history/keyboard-shortcuts.cjs");
|
|
7
|
+
const require_unsaved_changes_warning = require("./save/unsaved-changes-warning.cjs");
|
|
7
8
|
exports.CURRENT_SCHEMA_VERSION = require_document_format.CURRENT_SCHEMA_VERSION;
|
|
8
9
|
exports.DocumentEngineError = require_errors.DocumentEngineError;
|
|
9
10
|
exports.bindKeyboardShortcuts = require_keyboard_shortcuts.bindKeyboardShortcuts;
|
|
11
|
+
exports.bindUnsavedChangesWarning = require_unsaved_changes_warning.bindUnsavedChangesWarning;
|
|
12
|
+
exports.createConflictError = require_errors.createConflictError;
|
|
10
13
|
exports.createDocumentEngine = require_create_document_engine.createDocumentEngine;
|
|
11
14
|
exports.isDocumentEngineError = require_errors.isDocumentEngineError;
|
|
12
15
|
exports.validateDocument = require_validate_document.validateDocument;
|
package/dist/index.d.cts
CHANGED
|
@@ -2,9 +2,14 @@ import { CURRENT_SCHEMA_VERSION, DocumentCanvas, DocumentInfo, FabricDocument, S
|
|
|
2
2
|
import { NewDocumentOptions } from "./document/create-document.cjs";
|
|
3
3
|
import { CustomObjectDefinition } from "./fabric/object-registry.cjs";
|
|
4
4
|
import { HistoryOptions, HistoryState } from "./history/create-history.cjs";
|
|
5
|
-
import {
|
|
5
|
+
import { AutosaveOptions } from "./save/autosave-scheduler.cjs";
|
|
6
|
+
import { RetryOptions } from "./save/retry.cjs";
|
|
7
|
+
import { DocumentEngineError, DocumentEngineErrorDetails, DocumentErrorCode, DocumentIssue, createConflictError, isDocumentEngineError } from "./engine/errors.cjs";
|
|
8
|
+
import { DocumentStorage, SaveContext, SaveResult } from "./storage/storage-contract.cjs";
|
|
9
|
+
import { SaveOptions, SaveRetryEvent, SaveState, SaveStatus } from "./save/save-controller.cjs";
|
|
6
10
|
import { Unsubscribe } from "./engine/event-emitter.cjs";
|
|
7
|
-
import { DocumentEngine, DocumentEngineEvents, DocumentEngineOptions,
|
|
11
|
+
import { DocumentEngine, DocumentEngineEvents, DocumentEngineOptions, LoadOptions, createDocumentEngine } from "./engine/create-document-engine.cjs";
|
|
8
12
|
import { validateDocument } from "./document/validate-document.cjs";
|
|
9
13
|
import { KeyboardShortcutOptions, UndoRedoTarget, bindKeyboardShortcuts } from "./history/keyboard-shortcuts.cjs";
|
|
10
|
-
|
|
14
|
+
import { UnsavedChangesSource, bindUnsavedChangesWarning } from "./save/unsaved-changes-warning.cjs";
|
|
15
|
+
export { type AutosaveOptions, CURRENT_SCHEMA_VERSION, type CustomObjectDefinition, type DocumentCanvas, type DocumentEngine, DocumentEngineError, type DocumentEngineErrorDetails, type DocumentEngineEvents, type DocumentEngineOptions, type DocumentErrorCode, type DocumentInfo, type DocumentIssue, type DocumentStorage, type FabricDocument, type HistoryOptions, type HistoryState, type KeyboardShortcutOptions, type LoadOptions, type NewDocumentOptions, type RetryOptions, type SaveContext, type SaveOptions, type SaveResult, type SaveRetryEvent, type SaveState, type SaveStatus, type SerializedFabricObject, type UndoRedoTarget, type UnsavedChangesSource, type Unsubscribe, bindKeyboardShortcuts, bindUnsavedChangesWarning, createConflictError, createDocumentEngine, isDocumentEngineError, validateDocument };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,9 +2,14 @@ import { CURRENT_SCHEMA_VERSION, DocumentCanvas, DocumentInfo, FabricDocument, S
|
|
|
2
2
|
import { NewDocumentOptions } from "./document/create-document.js";
|
|
3
3
|
import { CustomObjectDefinition } from "./fabric/object-registry.js";
|
|
4
4
|
import { HistoryOptions, HistoryState } from "./history/create-history.js";
|
|
5
|
-
import {
|
|
5
|
+
import { AutosaveOptions } from "./save/autosave-scheduler.js";
|
|
6
|
+
import { RetryOptions } from "./save/retry.js";
|
|
7
|
+
import { DocumentEngineError, DocumentEngineErrorDetails, DocumentErrorCode, DocumentIssue, createConflictError, isDocumentEngineError } from "./engine/errors.js";
|
|
8
|
+
import { DocumentStorage, SaveContext, SaveResult } from "./storage/storage-contract.js";
|
|
9
|
+
import { SaveOptions, SaveRetryEvent, SaveState, SaveStatus } from "./save/save-controller.js";
|
|
6
10
|
import { Unsubscribe } from "./engine/event-emitter.js";
|
|
7
|
-
import { DocumentEngine, DocumentEngineEvents, DocumentEngineOptions,
|
|
11
|
+
import { DocumentEngine, DocumentEngineEvents, DocumentEngineOptions, LoadOptions, createDocumentEngine } from "./engine/create-document-engine.js";
|
|
8
12
|
import { validateDocument } from "./document/validate-document.js";
|
|
9
13
|
import { KeyboardShortcutOptions, UndoRedoTarget, bindKeyboardShortcuts } from "./history/keyboard-shortcuts.js";
|
|
10
|
-
|
|
14
|
+
import { UnsavedChangesSource, bindUnsavedChangesWarning } from "./save/unsaved-changes-warning.js";
|
|
15
|
+
export { type AutosaveOptions, CURRENT_SCHEMA_VERSION, type CustomObjectDefinition, type DocumentCanvas, type DocumentEngine, DocumentEngineError, type DocumentEngineErrorDetails, type DocumentEngineEvents, type DocumentEngineOptions, type DocumentErrorCode, type DocumentInfo, type DocumentIssue, type DocumentStorage, type FabricDocument, type HistoryOptions, type HistoryState, type KeyboardShortcutOptions, type LoadOptions, type NewDocumentOptions, type RetryOptions, type SaveContext, type SaveOptions, type SaveResult, type SaveRetryEvent, type SaveState, type SaveStatus, type SerializedFabricObject, type UndoRedoTarget, type UnsavedChangesSource, type Unsubscribe, bindKeyboardShortcuts, bindUnsavedChangesWarning, createConflictError, createDocumentEngine, isDocumentEngineError, validateDocument };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CURRENT_SCHEMA_VERSION } from "./document/document-format.js";
|
|
2
2
|
import { validateDocument } from "./document/validate-document.js";
|
|
3
|
-
import { DocumentEngineError, isDocumentEngineError } from "./engine/errors.js";
|
|
3
|
+
import { DocumentEngineError, createConflictError, isDocumentEngineError } from "./engine/errors.js";
|
|
4
4
|
import { createDocumentEngine } from "./engine/create-document-engine.js";
|
|
5
5
|
import { bindKeyboardShortcuts } from "./history/keyboard-shortcuts.js";
|
|
6
|
-
|
|
6
|
+
import { bindUnsavedChangesWarning } from "./save/unsaved-changes-warning.js";
|
|
7
|
+
export { CURRENT_SCHEMA_VERSION, DocumentEngineError, bindKeyboardShortcuts, bindUnsavedChangesWarning, createConflictError, createDocumentEngine, isDocumentEngineError, validateDocument };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/save/autosave-scheduler.ts
|
|
2
|
+
function createAutosaveScheduler(run, options = {}) {
|
|
3
|
+
const delay = options.delay ?? 1e3;
|
|
4
|
+
const maxWait = Math.max(delay, options.maxWait ?? 1e4);
|
|
5
|
+
let timer;
|
|
6
|
+
let firstRequestAt;
|
|
7
|
+
function cancel() {
|
|
8
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
9
|
+
timer = void 0;
|
|
10
|
+
firstRequestAt = void 0;
|
|
11
|
+
}
|
|
12
|
+
function fire() {
|
|
13
|
+
cancel();
|
|
14
|
+
run();
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
schedule() {
|
|
18
|
+
const now = Date.now();
|
|
19
|
+
firstRequestAt ?? (firstRequestAt = now);
|
|
20
|
+
const timeLeftBeforeMaxWait = maxWait - (now - firstRequestAt);
|
|
21
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
22
|
+
timer = setTimeout(fire, Math.max(0, Math.min(delay, timeLeftBeforeMaxWait)));
|
|
23
|
+
},
|
|
24
|
+
cancel
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
exports.createAutosaveScheduler = createAutosaveScheduler;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/save/autosave-scheduler.ts
|
|
2
|
+
function createAutosaveScheduler(run, options = {}) {
|
|
3
|
+
const delay = options.delay ?? 1e3;
|
|
4
|
+
const maxWait = Math.max(delay, options.maxWait ?? 1e4);
|
|
5
|
+
let timer;
|
|
6
|
+
let firstRequestAt;
|
|
7
|
+
function cancel() {
|
|
8
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
9
|
+
timer = void 0;
|
|
10
|
+
firstRequestAt = void 0;
|
|
11
|
+
}
|
|
12
|
+
function fire() {
|
|
13
|
+
cancel();
|
|
14
|
+
run();
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
schedule() {
|
|
18
|
+
const now = Date.now();
|
|
19
|
+
firstRequestAt ?? (firstRequestAt = now);
|
|
20
|
+
const timeLeftBeforeMaxWait = maxWait - (now - firstRequestAt);
|
|
21
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
22
|
+
timer = setTimeout(fire, Math.max(0, Math.min(delay, timeLeftBeforeMaxWait)));
|
|
23
|
+
},
|
|
24
|
+
cancel
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { createAutosaveScheduler };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/save/retry.ts
|
|
2
|
+
function isRetryable(error) {
|
|
3
|
+
const hints = error ?? {};
|
|
4
|
+
if (hints.code === "SAVE_CONFLICT" || hints.code === "SAVE_CANCELLED") return false;
|
|
5
|
+
return hints.retryable !== false;
|
|
6
|
+
}
|
|
7
|
+
function retryDelay(attempt, options = {}) {
|
|
8
|
+
const baseDelay = options.baseDelay ?? 500;
|
|
9
|
+
const maxDelay = options.maxDelay ?? 8e3;
|
|
10
|
+
const ceiling = Math.min(maxDelay, baseDelay * 2 ** attempt);
|
|
11
|
+
return ceiling / 2 + Math.random() * (ceiling / 2);
|
|
12
|
+
}
|
|
13
|
+
function wait(milliseconds, signal) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
if (signal.aborted) {
|
|
16
|
+
reject(/* @__PURE__ */ new Error("aborted"));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const stop = () => {
|
|
20
|
+
clearTimeout(timer);
|
|
21
|
+
reject(/* @__PURE__ */ new Error("aborted"));
|
|
22
|
+
};
|
|
23
|
+
const timer = setTimeout(() => {
|
|
24
|
+
signal.removeEventListener("abort", stop);
|
|
25
|
+
resolve();
|
|
26
|
+
}, milliseconds);
|
|
27
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.isRetryable = isRetryable;
|
|
32
|
+
exports.retryDelay = retryDelay;
|
|
33
|
+
exports.wait = wait;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/save/retry.ts
|
|
2
|
+
function isRetryable(error) {
|
|
3
|
+
const hints = error ?? {};
|
|
4
|
+
if (hints.code === "SAVE_CONFLICT" || hints.code === "SAVE_CANCELLED") return false;
|
|
5
|
+
return hints.retryable !== false;
|
|
6
|
+
}
|
|
7
|
+
function retryDelay(attempt, options = {}) {
|
|
8
|
+
const baseDelay = options.baseDelay ?? 500;
|
|
9
|
+
const maxDelay = options.maxDelay ?? 8e3;
|
|
10
|
+
const ceiling = Math.min(maxDelay, baseDelay * 2 ** attempt);
|
|
11
|
+
return ceiling / 2 + Math.random() * (ceiling / 2);
|
|
12
|
+
}
|
|
13
|
+
function wait(milliseconds, signal) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
if (signal.aborted) {
|
|
16
|
+
reject(/* @__PURE__ */ new Error("aborted"));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const stop = () => {
|
|
20
|
+
clearTimeout(timer);
|
|
21
|
+
reject(/* @__PURE__ */ new Error("aborted"));
|
|
22
|
+
};
|
|
23
|
+
const timer = setTimeout(() => {
|
|
24
|
+
signal.removeEventListener("abort", stop);
|
|
25
|
+
resolve();
|
|
26
|
+
}, milliseconds);
|
|
27
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { isRetryable, retryDelay, wait };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
const require_errors = require("../engine/errors.cjs");
|
|
2
|
+
const require_autosave_scheduler = require("./autosave-scheduler.cjs");
|
|
3
|
+
const require_retry = require("./retry.cjs");
|
|
4
|
+
//#region src/save/save-controller.ts
|
|
5
|
+
function cancelledError() {
|
|
6
|
+
return new require_errors.DocumentEngineError("SAVE_CANCELLED", "The save was cancelled because another document was opened");
|
|
7
|
+
}
|
|
8
|
+
function toSaveError(error, signal) {
|
|
9
|
+
if (require_errors.isDocumentEngineError(error)) return error;
|
|
10
|
+
if (signal.aborted) return cancelledError();
|
|
11
|
+
const hints = error ?? {};
|
|
12
|
+
const reason = typeof hints.message === "string" ? hints.message : String(error);
|
|
13
|
+
if (hints.code === "SAVE_CONFLICT") return new require_errors.DocumentEngineError("SAVE_CONFLICT", reason, { cause: error });
|
|
14
|
+
return new require_errors.DocumentEngineError("SAVE_FAILED", `Storage could not save the document: ${reason}`, {
|
|
15
|
+
cause: error,
|
|
16
|
+
retryable: require_retry.isRetryable(error)
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
function createSaveController(options) {
|
|
20
|
+
const attempts = Math.max(0, options.retry?.attempts ?? 3);
|
|
21
|
+
let contentVersion = 0;
|
|
22
|
+
let savedVersion = 0;
|
|
23
|
+
let revision = 0;
|
|
24
|
+
let lastSavedAt;
|
|
25
|
+
let lastError;
|
|
26
|
+
let saving = false;
|
|
27
|
+
let session = 0;
|
|
28
|
+
let sessionAbort = new AbortController();
|
|
29
|
+
let inFlight = null;
|
|
30
|
+
let queued = null;
|
|
31
|
+
const isDirty = () => contentVersion !== savedVersion;
|
|
32
|
+
function state() {
|
|
33
|
+
let status = isDirty() ? "unsaved" : "saved";
|
|
34
|
+
if (lastError) status = lastError.code === "SAVE_CONFLICT" ? "conflict" : "error";
|
|
35
|
+
if (saving) status = "saving";
|
|
36
|
+
return {
|
|
37
|
+
status,
|
|
38
|
+
isDirty: isDirty(),
|
|
39
|
+
isSaving: saving,
|
|
40
|
+
revision,
|
|
41
|
+
lastSavedAt,
|
|
42
|
+
error: lastError
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const publish = () => options.onStateChange(state());
|
|
46
|
+
const autosave = options.autosave === false || options.autosave === void 0 ? null : require_autosave_scheduler.createAutosaveScheduler(() => {
|
|
47
|
+
if (isDirty()) save().catch(() => void 0);
|
|
48
|
+
}, options.autosave);
|
|
49
|
+
async function sendWithRetries(storage, document, expectedRevision, signal) {
|
|
50
|
+
for (let attempt = 0;; attempt += 1) try {
|
|
51
|
+
return await storage.saveDocument(document, {
|
|
52
|
+
expectedRevision,
|
|
53
|
+
signal
|
|
54
|
+
});
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (signal.aborted || !require_retry.isRetryable(error) || attempt >= attempts) throw error;
|
|
57
|
+
const delay = require_retry.retryDelay(attempt, options.retry);
|
|
58
|
+
options.onRetry({
|
|
59
|
+
attempt: attempt + 1,
|
|
60
|
+
delay,
|
|
61
|
+
error
|
|
62
|
+
});
|
|
63
|
+
await require_retry.wait(delay, signal);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function runSave(saveOptions) {
|
|
67
|
+
const storage = options.getStorage();
|
|
68
|
+
const sessionAtStart = session;
|
|
69
|
+
const { signal } = sessionAbort;
|
|
70
|
+
const versionAtSnapshot = contentVersion;
|
|
71
|
+
const document = {
|
|
72
|
+
...options.createDocument(),
|
|
73
|
+
revision: revision + 1
|
|
74
|
+
};
|
|
75
|
+
const expectedRevision = saveOptions.overwrite === true ? null : revision;
|
|
76
|
+
saving = true;
|
|
77
|
+
lastError = void 0;
|
|
78
|
+
publish();
|
|
79
|
+
options.onStart(document);
|
|
80
|
+
try {
|
|
81
|
+
const result = await sendWithRetries(storage, document, expectedRevision, signal);
|
|
82
|
+
if (sessionAtStart !== session) return document;
|
|
83
|
+
revision = result?.revision ?? document.revision;
|
|
84
|
+
document.revision = revision;
|
|
85
|
+
savedVersion = versionAtSnapshot;
|
|
86
|
+
lastSavedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
87
|
+
options.onSuccess(document);
|
|
88
|
+
return document;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const saveError = toSaveError(error, signal);
|
|
91
|
+
if (sessionAtStart === session) {
|
|
92
|
+
lastError = saveError;
|
|
93
|
+
options.onError(saveError);
|
|
94
|
+
}
|
|
95
|
+
throw saveError;
|
|
96
|
+
} finally {
|
|
97
|
+
if (sessionAtStart === session) {
|
|
98
|
+
saving = false;
|
|
99
|
+
publish();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function save(saveOptions = {}) {
|
|
104
|
+
if (inFlight === null) {
|
|
105
|
+
inFlight = runSave(saveOptions).finally(() => {
|
|
106
|
+
inFlight = null;
|
|
107
|
+
});
|
|
108
|
+
return inFlight;
|
|
109
|
+
}
|
|
110
|
+
if (queued === null) {
|
|
111
|
+
const sessionWhenQueued = session;
|
|
112
|
+
queued = inFlight.catch(() => void 0).then(() => {
|
|
113
|
+
queued = null;
|
|
114
|
+
if (sessionWhenQueued !== session) throw cancelledError();
|
|
115
|
+
return save(saveOptions);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return queued;
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
save,
|
|
122
|
+
noteContentChange() {
|
|
123
|
+
contentVersion += 1;
|
|
124
|
+
autosave?.schedule();
|
|
125
|
+
publish();
|
|
126
|
+
},
|
|
127
|
+
startSession(newRevision) {
|
|
128
|
+
session += 1;
|
|
129
|
+
sessionAbort.abort();
|
|
130
|
+
sessionAbort = new AbortController();
|
|
131
|
+
autosave?.cancel();
|
|
132
|
+
saving = false;
|
|
133
|
+
savedVersion = contentVersion;
|
|
134
|
+
revision = newRevision;
|
|
135
|
+
lastSavedAt = void 0;
|
|
136
|
+
lastError = void 0;
|
|
137
|
+
publish();
|
|
138
|
+
},
|
|
139
|
+
state,
|
|
140
|
+
destroy() {
|
|
141
|
+
session += 1;
|
|
142
|
+
sessionAbort.abort();
|
|
143
|
+
autosave?.cancel();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
//#endregion
|
|
148
|
+
exports.createSaveController = createSaveController;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DocumentEngineError } from "../engine/errors.cjs";
|
|
2
|
+
//#region src/save/save-controller.d.ts
|
|
3
|
+
export type SaveStatus = "saved" | "unsaved" | "saving" | "error" | "conflict";
|
|
4
|
+
export interface SaveState {
|
|
5
|
+
status: SaveStatus;
|
|
6
|
+
isDirty: boolean;
|
|
7
|
+
isSaving: boolean;
|
|
8
|
+
revision: number;
|
|
9
|
+
lastSavedAt: string | undefined;
|
|
10
|
+
error: DocumentEngineError | undefined;
|
|
11
|
+
}
|
|
12
|
+
export interface SaveOptions {
|
|
13
|
+
overwrite?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface SaveRetryEvent {
|
|
16
|
+
attempt: number;
|
|
17
|
+
delay: number;
|
|
18
|
+
error: unknown;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DocumentEngineError } from "../engine/errors.js";
|
|
2
|
+
//#region src/save/save-controller.d.ts
|
|
3
|
+
export type SaveStatus = "saved" | "unsaved" | "saving" | "error" | "conflict";
|
|
4
|
+
export interface SaveState {
|
|
5
|
+
status: SaveStatus;
|
|
6
|
+
isDirty: boolean;
|
|
7
|
+
isSaving: boolean;
|
|
8
|
+
revision: number;
|
|
9
|
+
lastSavedAt: string | undefined;
|
|
10
|
+
error: DocumentEngineError | undefined;
|
|
11
|
+
}
|
|
12
|
+
export interface SaveOptions {
|
|
13
|
+
overwrite?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface SaveRetryEvent {
|
|
16
|
+
attempt: number;
|
|
17
|
+
delay: number;
|
|
18
|
+
error: unknown;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { DocumentEngineError, isDocumentEngineError } from "../engine/errors.js";
|
|
2
|
+
import { createAutosaveScheduler } from "./autosave-scheduler.js";
|
|
3
|
+
import { isRetryable, retryDelay, wait } from "./retry.js";
|
|
4
|
+
//#region src/save/save-controller.ts
|
|
5
|
+
function cancelledError() {
|
|
6
|
+
return new DocumentEngineError("SAVE_CANCELLED", "The save was cancelled because another document was opened");
|
|
7
|
+
}
|
|
8
|
+
function toSaveError(error, signal) {
|
|
9
|
+
if (isDocumentEngineError(error)) return error;
|
|
10
|
+
if (signal.aborted) return cancelledError();
|
|
11
|
+
const hints = error ?? {};
|
|
12
|
+
const reason = typeof hints.message === "string" ? hints.message : String(error);
|
|
13
|
+
if (hints.code === "SAVE_CONFLICT") return new DocumentEngineError("SAVE_CONFLICT", reason, { cause: error });
|
|
14
|
+
return new DocumentEngineError("SAVE_FAILED", `Storage could not save the document: ${reason}`, {
|
|
15
|
+
cause: error,
|
|
16
|
+
retryable: isRetryable(error)
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
function createSaveController(options) {
|
|
20
|
+
const attempts = Math.max(0, options.retry?.attempts ?? 3);
|
|
21
|
+
let contentVersion = 0;
|
|
22
|
+
let savedVersion = 0;
|
|
23
|
+
let revision = 0;
|
|
24
|
+
let lastSavedAt;
|
|
25
|
+
let lastError;
|
|
26
|
+
let saving = false;
|
|
27
|
+
let session = 0;
|
|
28
|
+
let sessionAbort = new AbortController();
|
|
29
|
+
let inFlight = null;
|
|
30
|
+
let queued = null;
|
|
31
|
+
const isDirty = () => contentVersion !== savedVersion;
|
|
32
|
+
function state() {
|
|
33
|
+
let status = isDirty() ? "unsaved" : "saved";
|
|
34
|
+
if (lastError) status = lastError.code === "SAVE_CONFLICT" ? "conflict" : "error";
|
|
35
|
+
if (saving) status = "saving";
|
|
36
|
+
return {
|
|
37
|
+
status,
|
|
38
|
+
isDirty: isDirty(),
|
|
39
|
+
isSaving: saving,
|
|
40
|
+
revision,
|
|
41
|
+
lastSavedAt,
|
|
42
|
+
error: lastError
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const publish = () => options.onStateChange(state());
|
|
46
|
+
const autosave = options.autosave === false || options.autosave === void 0 ? null : createAutosaveScheduler(() => {
|
|
47
|
+
if (isDirty()) save().catch(() => void 0);
|
|
48
|
+
}, options.autosave);
|
|
49
|
+
async function sendWithRetries(storage, document, expectedRevision, signal) {
|
|
50
|
+
for (let attempt = 0;; attempt += 1) try {
|
|
51
|
+
return await storage.saveDocument(document, {
|
|
52
|
+
expectedRevision,
|
|
53
|
+
signal
|
|
54
|
+
});
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (signal.aborted || !isRetryable(error) || attempt >= attempts) throw error;
|
|
57
|
+
const delay = retryDelay(attempt, options.retry);
|
|
58
|
+
options.onRetry({
|
|
59
|
+
attempt: attempt + 1,
|
|
60
|
+
delay,
|
|
61
|
+
error
|
|
62
|
+
});
|
|
63
|
+
await wait(delay, signal);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async function runSave(saveOptions) {
|
|
67
|
+
const storage = options.getStorage();
|
|
68
|
+
const sessionAtStart = session;
|
|
69
|
+
const { signal } = sessionAbort;
|
|
70
|
+
const versionAtSnapshot = contentVersion;
|
|
71
|
+
const document = {
|
|
72
|
+
...options.createDocument(),
|
|
73
|
+
revision: revision + 1
|
|
74
|
+
};
|
|
75
|
+
const expectedRevision = saveOptions.overwrite === true ? null : revision;
|
|
76
|
+
saving = true;
|
|
77
|
+
lastError = void 0;
|
|
78
|
+
publish();
|
|
79
|
+
options.onStart(document);
|
|
80
|
+
try {
|
|
81
|
+
const result = await sendWithRetries(storage, document, expectedRevision, signal);
|
|
82
|
+
if (sessionAtStart !== session) return document;
|
|
83
|
+
revision = result?.revision ?? document.revision;
|
|
84
|
+
document.revision = revision;
|
|
85
|
+
savedVersion = versionAtSnapshot;
|
|
86
|
+
lastSavedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
87
|
+
options.onSuccess(document);
|
|
88
|
+
return document;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const saveError = toSaveError(error, signal);
|
|
91
|
+
if (sessionAtStart === session) {
|
|
92
|
+
lastError = saveError;
|
|
93
|
+
options.onError(saveError);
|
|
94
|
+
}
|
|
95
|
+
throw saveError;
|
|
96
|
+
} finally {
|
|
97
|
+
if (sessionAtStart === session) {
|
|
98
|
+
saving = false;
|
|
99
|
+
publish();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function save(saveOptions = {}) {
|
|
104
|
+
if (inFlight === null) {
|
|
105
|
+
inFlight = runSave(saveOptions).finally(() => {
|
|
106
|
+
inFlight = null;
|
|
107
|
+
});
|
|
108
|
+
return inFlight;
|
|
109
|
+
}
|
|
110
|
+
if (queued === null) {
|
|
111
|
+
const sessionWhenQueued = session;
|
|
112
|
+
queued = inFlight.catch(() => void 0).then(() => {
|
|
113
|
+
queued = null;
|
|
114
|
+
if (sessionWhenQueued !== session) throw cancelledError();
|
|
115
|
+
return save(saveOptions);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return queued;
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
save,
|
|
122
|
+
noteContentChange() {
|
|
123
|
+
contentVersion += 1;
|
|
124
|
+
autosave?.schedule();
|
|
125
|
+
publish();
|
|
126
|
+
},
|
|
127
|
+
startSession(newRevision) {
|
|
128
|
+
session += 1;
|
|
129
|
+
sessionAbort.abort();
|
|
130
|
+
sessionAbort = new AbortController();
|
|
131
|
+
autosave?.cancel();
|
|
132
|
+
saving = false;
|
|
133
|
+
savedVersion = contentVersion;
|
|
134
|
+
revision = newRevision;
|
|
135
|
+
lastSavedAt = void 0;
|
|
136
|
+
lastError = void 0;
|
|
137
|
+
publish();
|
|
138
|
+
},
|
|
139
|
+
state,
|
|
140
|
+
destroy() {
|
|
141
|
+
session += 1;
|
|
142
|
+
sessionAbort.abort();
|
|
143
|
+
autosave?.cancel();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
//#endregion
|
|
148
|
+
export { createSaveController };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/save/unsaved-changes-warning.ts
|
|
2
|
+
function bindUnsavedChangesWarning(engine, target = globalThis.window) {
|
|
3
|
+
const handleBeforeUnload = (event) => {
|
|
4
|
+
if (!engine.isDirty()) return;
|
|
5
|
+
event.preventDefault();
|
|
6
|
+
event.returnValue = "";
|
|
7
|
+
};
|
|
8
|
+
target.addEventListener("beforeunload", handleBeforeUnload);
|
|
9
|
+
return () => target.removeEventListener("beforeunload", handleBeforeUnload);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
exports.bindUnsavedChangesWarning = bindUnsavedChangesWarning;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/save/unsaved-changes-warning.ts
|
|
2
|
+
function bindUnsavedChangesWarning(engine, target = globalThis.window) {
|
|
3
|
+
const handleBeforeUnload = (event) => {
|
|
4
|
+
if (!engine.isDirty()) return;
|
|
5
|
+
event.preventDefault();
|
|
6
|
+
event.returnValue = "";
|
|
7
|
+
};
|
|
8
|
+
target.addEventListener("beforeunload", handleBeforeUnload);
|
|
9
|
+
return () => target.removeEventListener("beforeunload", handleBeforeUnload);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { bindUnsavedChangesWarning };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const require_errors = require("../engine/errors.cjs");
|
|
2
|
+
//#region src/storage/key-value-storage.ts
|
|
3
|
+
function readRevision(json) {
|
|
4
|
+
if (json === null) return 0;
|
|
5
|
+
const revision = JSON.parse(json).revision;
|
|
6
|
+
return typeof revision === "number" ? revision : 0;
|
|
7
|
+
}
|
|
8
|
+
function createKeyValueStorage(store, prefix) {
|
|
9
|
+
const keyFor = (id) => `${prefix}${id}`;
|
|
10
|
+
return {
|
|
11
|
+
async loadDocument(id) {
|
|
12
|
+
const json = store.read(keyFor(id));
|
|
13
|
+
if (json === null) throw new require_errors.DocumentEngineError("DOCUMENT_NOT_FOUND", `No document is stored with id "${id}"`);
|
|
14
|
+
return JSON.parse(json);
|
|
15
|
+
},
|
|
16
|
+
async saveDocument(document, { expectedRevision }) {
|
|
17
|
+
const storedRevision = readRevision(store.read(keyFor(document.id)));
|
|
18
|
+
if (expectedRevision !== null && storedRevision !== expectedRevision) throw require_errors.createConflictError(document.id, expectedRevision, storedRevision);
|
|
19
|
+
const revision = expectedRevision === null ? storedRevision + 1 : document.revision ?? storedRevision + 1;
|
|
20
|
+
store.write(keyFor(document.id), JSON.stringify({
|
|
21
|
+
...document,
|
|
22
|
+
revision
|
|
23
|
+
}));
|
|
24
|
+
return { revision };
|
|
25
|
+
},
|
|
26
|
+
async listDocuments() {
|
|
27
|
+
return store.keys().filter((key) => key.startsWith(prefix)).map((key) => key.slice(prefix.length));
|
|
28
|
+
},
|
|
29
|
+
async deleteDocument(id) {
|
|
30
|
+
store.remove(keyFor(id));
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
exports.createKeyValueStorage = createKeyValueStorage;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { DocumentStorage } from "./storage-contract.cjs";
|
|
2
|
+
//#region src/storage/key-value-storage.d.ts
|
|
3
|
+
export interface KeyValueStore {
|
|
4
|
+
read(key: string): string | null;
|
|
5
|
+
write(key: string, value: string): void;
|
|
6
|
+
remove(key: string): void;
|
|
7
|
+
keys(): string[];
|
|
8
|
+
}
|
|
9
|
+
export interface ManagedDocumentStorage extends DocumentStorage {
|
|
10
|
+
listDocuments(): Promise<string[]>;
|
|
11
|
+
deleteDocument(id: string): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare function createKeyValueStorage(store: KeyValueStore, prefix: string): ManagedDocumentStorage;
|
|
14
|
+
//#endregion
|