mirror of
https://github.com/bitbaum/writingkit.git
synced 2026-12-04 12:18:51 +01:00
composeArticle splits a dump into notes and links, reads the links through the host's reader, composes one cohesive article citing [S#], drops citations to unknown sources, repairs one bad answer, and reports every failure in words. Pure pieces exported for hosts with their own flow. Claude-Session: https://claude.ai/code/session_019MDtmsN9mz8QfGDCrNhY9p Co-authored-by: Claude <noreply@anthropic.com>
100 lines
3.1 KiB
JavaScript
100 lines
3.1 KiB
JavaScript
/**
|
|
* One operation owner per editing workspace. The host supplies transport and
|
|
* a synchronous apply callback; only a response for the current revision applies.
|
|
* This is a browser/editing guard, never a replacement for server authorization
|
|
* or an optimistic database revision check.
|
|
*/
|
|
export class WritingSession {
|
|
current;
|
|
epoch = 0;
|
|
pending = null;
|
|
closed = false;
|
|
constructor(base) {
|
|
this.current = Object.freeze({ ...base });
|
|
}
|
|
get busy() {
|
|
return this.pending !== null;
|
|
}
|
|
/** Call on every committed revision, including changes later reverted. */
|
|
update(next) {
|
|
if (this.closed) {
|
|
return false;
|
|
}
|
|
const switched = next.documentId !== this.current.documentId;
|
|
if (!switched && next.revision === this.current.revision) {
|
|
return false;
|
|
}
|
|
this.current = Object.freeze({ ...next });
|
|
this.epoch++;
|
|
if (switched) {
|
|
this.cancel();
|
|
}
|
|
return true;
|
|
}
|
|
/**
|
|
* A provider may ignore AbortSignal: identity/revision checks still reject its
|
|
* answer. Keep apply synchronous so no await separates validation from editing.
|
|
*/
|
|
async run(work, apply) {
|
|
if (this.closed) {
|
|
return { status: "cancelled" };
|
|
}
|
|
if (this.pending) {
|
|
return { status: "busy" };
|
|
}
|
|
const controller = new AbortController();
|
|
const operation = {
|
|
base: this.current,
|
|
epoch: this.epoch,
|
|
signal: controller.signal,
|
|
controller,
|
|
};
|
|
this.pending = operation;
|
|
try {
|
|
const value = await work({
|
|
base: operation.base,
|
|
signal: operation.signal,
|
|
});
|
|
const status = this.status(operation);
|
|
if (status !== "current") {
|
|
return { status };
|
|
}
|
|
apply(value, operation.base);
|
|
return { status: "applied" };
|
|
}
|
|
catch (error) {
|
|
const status = this.status(operation);
|
|
return status === "current" ? { status: "failed", error } : { status };
|
|
}
|
|
finally {
|
|
// An old cancellation/completion must never clear the next operation.
|
|
if (this.pending === operation) {
|
|
this.pending = null;
|
|
}
|
|
}
|
|
}
|
|
cancel() {
|
|
const pending = this.pending;
|
|
this.pending = null;
|
|
pending?.controller.abort();
|
|
}
|
|
/** Dispose synchronously when the editor/account/draft is removed. */
|
|
dispose() {
|
|
this.closed = true;
|
|
this.cancel();
|
|
}
|
|
status(operation) {
|
|
if (this.closed || this.pending !== operation || operation.signal.aborted) {
|
|
return "cancelled";
|
|
}
|
|
return operation.epoch === this.epoch ? "current" : "stale";
|
|
}
|
|
}
|
|
export * from "./assist.js";
|
|
export * from "./text-edit.js";
|
|
export * from "./drafts.js";
|
|
export * from "./chart.js";
|
|
export * from "./suggest.js";
|
|
export * from "./media.js";
|
|
export * from "./compose.js";
|