clay.js
  1. ClayJS
  2. Reference
  3. API
  4. Events

Events

What fires on document as a page boots, saves, goes dirty and receives live sync.

What fires on document as a page boots, saves, goes dirty and receives live sync.

Events#

Event (on document)Fires whendetail
clay:mutation-readythe mutation hub is installed, and clay.Mutation answers from inside the listener. Fires before clay:ready (mostly for plugins){Mutation}
clay:readyclayjs finished booting (same moment clay.ready resolves){clay}
clay:plugin-readya late plugin (source, sortable, menu, upgrade) finished loading after clay:ready. Once per plugin per page{plugin}
clay:plugin-faileda late plugin failed to load; the page continues without it{plugin}
clay:menu-readythe menu plugin mounted on an editable page and installed clay.menunone
clay:presence-readythe presence store loaded and clay.presence was installed. Once per page{presence}
clay:presence-quiescelive sync paused presence while the page changes mode, so this tab publishes no caret or target (mostly for plugins)none
clay:presence-resumepresence is allowed again, because the mode change finished or the live sync session ended (mostly for plugins)none
clay:peoplethe viewer, the host’s team or the document’s people registry changed (people plugin){me}
clay:save-savinga save has been in flight for 500ms (fast saves skip straight to the result){msg, msgType, timestamp}, with msgType empty
clay:save-savedthe server confirmed the write{msg, msgType, timestamp, etag, captureId}
clay:save-errorthe server answered with a problem. A failed save is not retried until the next edit or the browser’s online event{msg, msgType, timestamp, captureId, status, hadTimeout, code, reason, retryAfter}, with msgType empty. A refusal made in this tab, sent nothing, carries {code, status, sent: false} instead.
clay:save-offlinethe browser is offline (clayjs re-saves when the connection returns){msg, msgType, timestamp, captureId, status, hadTimeout, code, reason, retryAfter}, with msgType empty. A refusal made in this tab, sent nothing, carries {code, status, sent: false} instead.
clay:save-sizea real save attempt measured the exact bytes it was about to send, so near and over describe this page. clay.save.inspect never fires it{bytes, maxBytes, state}, with state one of unknown, ok, near or over
clay:dirtythe page holds work no save has written. It follows DOM mutations and user input; a script setting a [persist] control’s value programmatically is not observed. One event per state change, so a burst of keystrokes is one clay:dirty, and it fires before autosave does. See below.—
clay:cleana save (or a merge the loss oracle verified) has accounted for everything the gate saw, so nothing on the page is unwritten. unsaved is gone from the root by the time it fires—
clay:save-conflictthe host refused a save because the file changed since this tab loaded or last saved it (HTTP 412). Autosave pauses and a notice appears. See below.{msg, msgType, timestamp, changedBy, afterTimeout, etag, captureId}
clay:save-conflict-resolvedlive sync merged the version that beat this tab’s save, with nothing lost, so the refusal is answered. See below.{timestamp}
clay:save-conflict-releasedthe hold a conflict put on this tab’s saves was released: live sync merged the version the host refused, so autosave resumes and a notice can drop the refusal it was showing. It fires after clay:save-conflict-resolved, and on its own when there is still unsaved work a manual save has to send{etag}
clay:document-upgradeda saved file named a different template version than this page’s <meta name="hyper-version">, so the frame was refused and this tab holds its saves. The notice offers Reload{message}
clay:page-adoptedan incoming page file was merged with this page and applied to the article (page plugin){edited, created, deleted, moved}, plus displaced, conflicts or copy when that adoption carried them
clay:page-save-statethe page editor’s save state changed (page plugin). The indicator plugin reads it{state, code, message}, with code null and message empty when there is nothing to say. state is one of pending, journaled, acknowledged, checkpointed, retrying, blocked, unavailable, saved or error
clay:snapshot-readya snapshot has been cloned, before any stripping. Read detail.documentElement; do not change it. It is also where both change-detection baselines come from, and the change checks do not fire this event, so an edit made here is saved but never compared and the page reads dirty forever. Use clay.addDocumentTransform(fn) to change what gets saved. This clone is not the bytes a save sends: hooks, transforms and final serialization still run after it.{documentElement}
clay:sync-applieda live-sync update landed (sync plugin){seq, source, by, report, unresolved, conflictIds}, plus etag on a disk frame. See below.
clay:sync-conflicts-changedthe ledger changed. Read clay.conflicts.list() for the records{open, added, removedIds, updatedIds, reason}
clay:conflict-store-changedconflicts were removed from the conflict store, so a notice showing one should reread them (mostly for plugins)none
clay:notificationlive sync received a notification from the server{msgType, msg, action, persistent, data}
clay:sync-resumedone lane stopped holding incoming frames. The other lane may still be holding, so this tab is only back in step once neither is{lane, el}, with lane either live for a peer frame or external for a disk frame, and el null
clay:live-noticea live store gave up on an edit (live plugin). With code save-uncertain, a save ended with no answer, so its edits were not sent again and the store went back to the host’s version. With code op-too-large, one change was too large to share and was undone{name, code, message}, plus the lost edits in ops for save-uncertain
clay:sorteda drag-drop reorder landed (sortable plugin); fires on the container and bubbles{item, from, to, oldIndex, newIndex}
sap:views-updateda Sap pass changed the views it paints; fires on the app root and bubblesnone
clay:undo-refusedan undo or redo was refused because the document changed since the step was recorded, so nothing in it moved (undo plugin){ok: false, code, entryId, label, direction}
clay:undo-keptthe copy of a refused step is settled in the recovery store, so a reload can review it (undo plugin){ok, entryId, direction}, plus code when ok is false
clay:undo-changethe undo history changed: a step, an undo, a redo, a refusal, a discard or a clear (undo plugin){canUndo, canRedo, undo, redo}
clay:trashed / clay:restoredclay.trash moved a record to the trash or back; fires on the record and bubbles—
clay:upload-starta file starts being sent (upload plugin){name, size, taskId, attempt}
clay:upload-progressthe browser reports bytes sent. 100 is not success: the upload is still running until the host answers{loaded, total, percent, taskId, attempt}
clay:upload-donethe host confirmed it stored the file{uploads, taskId, attempt}
clay:upload-errorthe attempt ended with no stored file: the host refused it, the request failed, or it timed out{msg, code, taskId, attempt}
clay:modechangea mode transition committed, so clay.mode and clay.modeLifecycle.state already answer for the new mode. One event per committed transition, and an explicit edit prepared and enabled at boot is one of them. See below.—
clay:capabilitychangeclay.canEdit changed, because the host moved its own answer about the document, document.writable in /_/meta. html[canedit] follows itnone
clay:baseline-settledthe load-settle baseline was taken, and a live entry adopted the file it shows as its baselinenone
clay:upgrade-requestedthe person accepted the offer of a newer template version, from the menu item or the notice’s Upgrade button (upgrade plugin){version, html, sha256, latestUrl}
clay:unsent-changedthe set of kept unsent drafts changed (unsent plugin){count}
clay:unsent-restoreda control carrying unsent was redrawn and its kept text was put back; fires on the control and bubbles (unsent plugin){base, current, stale}
clay:view-save-attempta visitor clicked a [trigger-save] element in view mode; show your own notice—

Save events#

msgType is empty for clay:save-saving, clay:save-offline and clay:save-error; the shortcut shape {msg, timestamp} is the same detail without it. A failed save is not retried until the next edit or the browser’s online event.

Save events and recovery#

etag and captureId are additive fields, so a listener that reads msg, msgType and timestamp is unaffected. clay:save-saved carries both: etag is the version stamp the host accepted, or null when it named none, and captureId names the exact bytes this save captured before it went out. clay:save-error, clay:save-offline and clay:save-conflict carry captureId too, and a conflict’s etag is the stamp it was refused against.

That id is what a local recovery copy is judged against: a revision is cleared only when that capture’s own bytes contain it, so a save that was skipped, refused, conflicted or unknown clears nothing, and neither does a later save that never held the writing. clay:snapshot-ready cannot answer that question, because it fires on a clone taken before before-save hooks, document transforms and final serialization. See recovery.

clay:dirty#

The work is an edit in the DOM, or a [persist] control whose live value differs from what the file carries. It fires before autosave does, which is what lets a status row show unsaved state at the moment it appears. The root already carries unsaved="true" when the event fires, so a listener can read the state from the DOM or from the event.

clay:save-conflict#

The etag in the detail is the stamp the host refused the save against.

clay:save-conflict-resolved#

The version that beat this tab’s save has a stamp that matches the refusal’s etag. Autosave resumes and the notice hides. On a manual-save page with unsaved work the notice stays until the person saves. No save happened; clay:save-saved follows once the merged page is saved.

clay:sync-applied#

The shorter {seq, source, by, report, unresolved} is the same detail before conflictIds.

source is peer (another open copy) or disk (the file changed underneath you). by is the {id, name} the host stamped on the frame, or null on a frame nobody stamped; hosts stamp live-lane frames only, so a disk frame’s by is normally null.

report is hyper-morph’s merge report: conflicts lists every place both sides changed the same content, with the text this tab lost when the incoming side won. A lost conflict goes into clay.conflicts, a ledger of every place another edit replaced yours that you have not decided on yet. Each record has a stable id and the text this tab lost in local; clay.conflicts.recoveryOf(id) returns that frame’s apply, {id, source, seq, etag, ticket, domain, root, ids}, where root is a clone of the page as this tab had it just before the frame (domain sync for a peer frame, save for a disk frame, already prepared for saving), or null once every record from it is acknowledged. The ledger keeps the close warning up and is cleared only by clay.conflicts.acknowledge(ids, {reason}), which the notice calls when you choose Accept theirs or Revert to mine. No save, autosave, reconnect or later frame clears it. unresolved is every open ledger record after this frame — the engine conflict objects with the ledger’s own fields added, less the applies that ended as apply-incomplete — and conflictIds the ids this frame added. An editor that merges both sides itself (Writer) can claim its records so the notice does not list them, then acknowledge them with reason: 'reconciled' once its merged content is in the page.

A tab tells the other open copies about a save only once the host has accepted it, so every update it sends descends from the version they already hold. Its own merge base moves to that version at the same moment, not when its update reaches the others.

Known limitation: the host’s stamp is a hash of the file’s content, and relayed updates do not say which version they were built on. So an older update that arrives late can still revert another tab’s newer save without a conflict. It takes a tab saving the file back to bytes identical to an earlier version (an undo, then a save) while its previous update is still in transit. A page that loads in the instant between another tab’s save and that save’s update can also start from a stamp newer than the bytes it was served.

Live sync takes its merge base when it starts, then retakes it once the page settles (500 ms without changes, at most 3 s after load), so DOM that modules build at boot is part of it. It retakes it only on a tab nobody has clicked or typed in yet, and only if it took one at start. Content that changes in that window with no user action (a script restoring a draft, a postMessage, the browser restoring a [persist] field) is therefore treated as page setup: if a later incoming update still has the served version of that content, it wins.

clay:undo-refused and clay:undo-kept#

A refusal fires clay:undo-refused once, synchronously, from inside the undo() or redo() call that returned { ok: false }. It carries no document HTML and no private text: the copy of what the step would have restored goes to the recovery store, under the internal clay.undo channel, and never into the event.

clay:undo-kept fires afterwards, when that copy is settled. ok: true means a later session can review it; ok: false means the store took nothing and the step exists only in this tab, with code saying why. The refusal itself already stands either way. See undo.

clay:modechange#

It carries no detail. A listener reads the new mode from clay.mode, clay.isEditMode and clay.canEdit, and the rest of the mode facts from clay.modeLifecycle.state: phase, destination, generation and reason. Participants and engines that follow the session key their work on state.generation, which changes once per committed transition. A save is not part of it. A live switch on a <html clay-mode-switch="live"> document commits with no reload, and this tab announces clay:modechange itself. An undeclared document reloads instead: the reloaded tab announces its own mode when an explicit edit is prepared and enabled, while a tab that boots in view announces nothing.

Upload events#

taskId and attempt are on all four upload events, and are additive: a listener that already reads only msg, code and uploads is unaffected. taskId names the upload, the same id clay.upload.start gives the task, and attempt counts up only on an explicit retry(), so a listener can tell two uploads apart and ignore a callback from an attempt that has already ended. A single clay.upload(file) call has a task id too, because it is one attempt of one upload.

A cancel fires no event of its own: the attempt stops sending and the task’s state is cancelled. An attempt can also end with clay:upload-error and the code timeout, which is not evidence the file was not stored.