Skip to content

How a change goes through

When the person deletes a row, the app does not delete it immediately. The change first goes through a sequence of steps, and a plugin can take part in several of them.

The following plugin refuses to delete any row that carries the tag #keep. It is used as the running example on this page:

ctx.hooks.shouldDelete({ on: 'row', onTimeout: 'veto' }, delta => {
if (delta.kind !== 'deleted') return allow();
const tags = ctx.facets.core.tags.tags.of({ facets: delta.value.facets ?? {} }) ?? [];
return tags.some(t => t.toLowerCase() === 'keep')
? veto('protected', 'Tagged #keep: it cannot be deleted')
: allow();
});

A change is anything that modifies the library: adding, editing, moving or deleting a row, for example. A change is made of deltas, and each delta describes a single modification, such as “this row was deleted” or “this title changed from A to B”.

A hook is a function of your plugin that the app calls at a particular step. A topic is the name of one kind of delta, for example row.delete or row.modify, and it is how a hook states which deltas it wants to receive.

A change goes through the following steps, in this order.

  1. Availability. Before an action is offered, for example to decide whether a menu item should be greyed out, the app asks whether the action can be performed in the current context. A plugin answers through the can function of a command, with available() or unavailable('reason').
  2. Description. The change is described as a list of deltas.
  3. Hooks for each delta. For every delta, the app calls the matching hook: shouldInsert, shouldModify, shouldMove or shouldDelete. Such a hook can allow the delta, refuse it, replace it, or ask the app to wait.
  4. Hook for the whole change. The app calls shouldUpdate once, with all the deltas together. It accepts the same four answers.
  5. Last inspection. The app calls willUpdate. A hook can add deltas at this point, but it gives no answer.
  6. Application. The app applies the change. It then repairs any data that became inconsistent, by calling the normalize function of every plugin that registered one.
  7. didUpdate. The change is now applied in the memory of the app. A hook can request further changes.
  8. willWrite. The change is about to be saved to disk. A hook can only observe it.
  9. didWrite. The change has been saved. A hook can request further changes.

A hook is registered through ctx.hooks, for example ctx.hooks.shouldDelete(options, hook).

A topic consists of a kind of thing and the modification that happened to it, separated by a dot. The four modifications are insert, modify, move and delete, which gives topics such as row.insert, row.delete and outline.modify.

The options of a hook state which topics it wants. topics: ['row.delete'] selects a single topic. on: 'row' selects every topic about rows, and it is equivalent to topics: ['row.*']. topics: ['*'] selects every topic.

A should hook answers with one of four objects, each of which is built by a function of the SDK.

Function Meaning
allow() The change may go through.
veto(code, message) The change is refused. The person sees the message, and the code is a short name that other code can compare with.
amend(newChange, note?) The change is replaced by another one.
defer(promise, reason) The app waits for the promise, which must produce one of the three other answers. The screen remains responsive while the app waits.

The following rules apply to these answers.

  • The first veto stops the change. Whoever made the change is told which plugin refused it, and why.
  • A plugin may replace a change once, and only for things that belong to the plugin. Replacing anything else requires the amend:outside permission.
  • If the library changes while a defer is pending, the app checks the change again before applying it.

The SDK provides further functions for other kinds of answers.

Function Where it is used
available(label?, state?) and unavailable(reason) In the can function of a command.
replace(text, selection?) In ctx.editing.shouldChangeText, to put another text in place of the text being typed.
valid(), coerced(value) and invalid(issue) In the validate function of a facet, to state that a value is acceptable, that another value must be kept instead, or that the value is refused.

As an example, veto('protected', 'Tagged #keep') returns the object { veto: { code: 'protected', message: 'Tagged #keep' } }.

A hook receives copies of the data, which it cannot modify. It has 4 milliseconds to answer. If it exceeds this time three times within 60 seconds, the app turns the hook off and informs the person.

When a hook throws an error or exceeds its time, the app allows the change by default. The option onTimeout: 'veto' reverses this behavior, so that the change is refused instead. A plugin that protects data should choose it, as the example at the top of this page does.

A hook that needs more time should return defer. Slow work that does not need to delay the change belongs in didWrite or in a job (ctx.jobs).

The didUpdate and didWrite hooks can return { followUps: [intent, intent] }, where each intent describes another change that should be made.

The app runs each follow-up as a separate change, and it joins the undo step of the change that caused it, so that one undo reverses both. The app also verifies that the plugin holds the permission that each follow-up requires. The follow-ups of a follow-up run afterwards, level after level, and the chain stops at a depth of 8.

Every change has an origin, which determines which steps apply to it.

Origin Meaning What hooks can do
User, Plugin The person, or a plugin. Everything, including defer.
Undo, Redo The person pressed undo or redo. A hook can refuse the change but cannot replace it. defer is allowed.
Automation, Import A program acting for the person, or an import. defer is not allowed, because nothing can wait.
Remote, OtherTab, Migration, System A change that has already happened elsewhere: on another device, in another browser tab, in an upgrade of the data, or in the app itself. There is nothing to refuse. The app applies the change as soon as it arrives.

The six ways in which the app passes information

Section titled “The six ways in which the app passes information”

The app uses six different mechanisms to pass information between its parts, and ctx exposes five of them.

Mechanism Purpose In ctx
Hooks Deciding about a change. ctx.hooks
Watching a value Being told when a single value changes. ctx.observe(key, options, handler)
Events One side sends an event and any number of others listen to it. ctx.notifications (declare, post, subscribe)
One question, one answer The plugin asks and waits for a single answer. ctx.ui.confirm, ctx.ui.prompt, ctx.ui.pick, ctx.dispatch, ctx.net.fetch
A function that answers for something the plugin created The app asks the function about a view, a panel or an outline type that the plugin added. ctx.query.view, ctx.ui.panel, ctx.ui.outlineType
A stream Data that keeps arriving, which the app sends to the screen. Plugins do not use it directly. They use hooks and events.

A DraftBuilder is passed to the propose function of a command and to normalize functions. It lets you describe the modifications that you want to make to the library. Calling its methods does not change anything by itself. When your function returns, the app validates the draft and applies it like any other change.

The DraftBuilder provides these methods:

  • update(id, fields) modifies fields of a row, an outline or a saved search, such as title or note. For an outline, kind switches between standard and journal; the Inbox cannot change its kind.
  • facet(id, handle).set(value) and facet(id, handle).remove() modify the value of a facet.
  • createRows(next, rows) creates rows below the row next, at the same level, and returns their ids. Each row has a title and may start with facets.
  • deleteRow(id) deletes a row together with the rows under it, and deleteSearch(id) deletes a saved search.
  • now() returns the current time and newId() returns a new unique id. Both come from the clock and the random number source of the app, so a test produces the same result each time it runs.

When a draft modifies the same row several times, the app records a single delta for that row, holding the value before the first modification and the value after the last one.

A draft cannot move rows or create outlines. To do either, call the corresponding command with ctx.dispatch, or return it as a follow-up from didUpdate or didWrite.