Skip to content

FacetHandle

The object that ctx.facets.define returns. It represents the facet that the plugin has just defined, and it is the only thing the plugin needs in order to read, write and watch the values of that facet. The plugin never has to type the key of the facet again.

A handle has all the members of a FacetRef, which are of, set and clear, and two further members: on, which watches the value of the facet on one row, and dispose, which removes the facet.

const sessions = ctx.facets.define<number>({
key: 'com.example.pomodoro/sessions',
on: 'row',
merge: 'counter',
schema: { type: 'integer', minimum: 0 },
});
const count = sessions.of(row); // the value on a row, or undefined
await ctx.dispatch(sessions.set(row.id, 3)); // write the value
await ctx.dispatch(sessions.clear(row.id)); // remove the value
ctx.observe(sessions.on(row.id), { new: true }, change => {
ctx.log('info', `The count is now ${change.new}`);
});
Type Parameter Description
T the type of the values of the facet
readonly alias?: string;

A short name that searches can use for the facet, for example pomodoro in the search pomodoro>3.

FacetRef.alias


readonly key: string;

The name of the facet, written <plugin id>/<name>. The library saves each value under this name. Code normally uses the facet object and does not need to type the name.

FacetRef.key


readonly merge: MergeKind;

Determines the result when two devices change the value at the same time. lww, which stands for “last write wins”, keeps the later change. set keeps what both devices added to the set. text merges the two texts into one. counter adds up what each device added.

FacetRef.merge


readonly paths?: readonly string[];

The parts of the value that searches are able to look at quickly. The path . designates the whole value. A facet must declare its paths for a search to be able to use its index.

FacetRef.paths


readonly schema: Schema;

The description of the values that the facet accepts. The app checks every value against it when the value is written, and refuses a value that does not conform.

FacetRef.schema


readonly sync: boolean;

When false, the value stays on the device where it was written, and other devices never receive it. Otherwise the value is shared with the other devices of the person.

FacetRef.sync


readonly target: FacetTarget;

The kind of thing that the facet is attached to: a row, an outline or the library.

FacetRef.target

clear(id): BuiltIntent;

Builds the request that removes the value of the facet from a row or an outline. The request is not run by this call: pass it to ctx.dispatch, or return it from a hook as a follow-up.

Parameter Type Description
id string the id of the row or of the outline

BuiltIntent

the request

FacetRef.clear


dispose(): void;

Unregisters the facet. The app calls this automatically when the plugin is turned off, so a plugin rarely needs to call it. The handle must not be used afterwards.

void


of(entity): T | undefined;

Reads the value of the facet on a row or an outline, or on any other object that has a facets field.

Parameter Type Description
entity | { facets: Readonly<Record<string, unknown>>; } | null | undefined the row or outline whose value to read

T | undefined

the value, or undefined when the facet has no value there

FacetRef.of


on(id?): Key<T>;

Returns the key that identifies the value of the facet on one row or outline, or on the library when no id is given. Pass this key to ctx.observe to be notified each time the value changes.

Parameter Type Description
id? string the id of the row or of the outline. Omit it for a facet of the library

Key<T>

the key to pass to ctx.observe


set(id, value): BuiltIntent;

Builds the request that gives the facet a value on a row or an outline. The request is not run by this call: pass it to ctx.dispatch, or return it from a hook as a follow-up. For a facet of the library, pass the id of the library.

Parameter Type Description
id string the id of the row or of the outline
value T the new value

BuiltIntent

the request

FacetRef.set