Skip to content

PluginHooks

Registers hooks. A hook is a function that the app calls when a change to the library is about to happen or has just happened. The person can make a change, and so can a plugin. Synchronization with another device, undo and import make changes as well, and all of them go through the same steps.

Each method below registers a hook for one step. It takes options (see HookOptions) and the hook function, and it returns an object with a dispose() method, which removes the hook. The app also removes the hooks of a plugin when the plugin is turned off.

A hook whose name starts with should has to answer immediately, because the app needs the answer before it applies the change. A hook that needs time, for example to ask the person a question, returns defer.

A hook receives copies of the data, so it cannot modify the library directly. A hook has 4 milliseconds to answer. When it is too slow three times within 60 seconds, the app turns it off. Registering a hook requires the read permission, because a hook receives data from the library.

The page “How a change goes through” of the guide describes the steps in order, and the origins that a change can have.

Refuse to delete the rows that carry the tag #keep

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();
});
didUpdate(options, hook): object;

Registers a hook that the app calls after the change has been applied in the memory of the app.

The hook can return { followUps }, a list of requests for more changes. The app runs each request as a separate change, and it adds the request to the undo step of the change that caused it, so that one undo reverses both. The app runs the requests one level after the other and stops after 8 levels. The plugin needs the permission that each request needs.

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook (change) => void | DidResult receives the change, and returns nothing or { followUps }

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void

didWrite(options, hook): object;

Registers a hook that the app calls after it has saved a group of changes on the disk. Like didUpdate, the hook can return { followUps }. This is the place for slow work, because it does not delay a change.

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook (receipt) => void | DidResult receives the receipt of the write, and returns nothing or { followUps }

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void

shouldDelete(options, hook): object;

Registers a hook that the app calls once for each row or outline that the change deletes (topics row.delete, outline.delete). The delta holds the value that the thing had.

A plugin that protects data passes onTimeout: 'veto', so that the deletion is refused when the hook itself fails.

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook DeltaHook answers with allow(), veto(code, message), amend(draft) or defer(promise, reason)

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void

shouldInsert(options, hook): object;

Registers a hook that the app calls once for each row or outline that the change creates (topics row.insert, outline.insert, and so on), before the change is applied.

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook DeltaHook answers with allow(), veto(code, message), amend(draft) or defer(promise, reason)

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void

shouldModify(options, hook): object;

Registers a hook that the app calls once for each thing that the change modifies: a title, a note, a facet, and so on (topics row.modify, outline.modify). The delta holds the value before the change and the value after it.

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook DeltaHook answers with allow(), veto(code, message), amend(draft) or defer(promise, reason)

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void

shouldMove(options, hook): object;

Registers a hook that the app calls once for each row that the change moves to another place (topic row.move).

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook DeltaHook answers with allow(), veto(code, message), amend(draft) or defer(promise, reason)

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void

shouldUpdate(options, hook): object;

Registers a hook that the app calls once for the whole change, with all its deltas together. It is meant for a rule that depends on several deltas at once, for example “ask the person when more than 30 rows are deleted”.

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook ProposalHook receives all the deltas of the change as a draft, and answers like the other should hooks

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void

willUpdate(options, hook): object;

Registers a hook that the app calls just before it applies the change. The hook can add deltas to the draft that it receives, and it gives no answer.

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook (draft, origin) => void receives the draft and the origin of the change (User, Plugin, Remote, and so on)

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void

willWrite(options, hook): object;

Registers a hook that the app calls just before it saves a group of changes on the disk. The hook can look at the group, and it gives no answer.

Parameter Type Description
options HookOptions the topics to listen to, and what happens when the hook fails
hook (batch) => void receives the group of changes that is about to be saved

object

an object with dispose(), which removes the hook

Name Type
dispose() () => void