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.
Example
Section titled “Example”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 undefinedawait ctx.dispatch(sessions.set(row.id, 3)); // write the valueawait ctx.dispatch(sessions.clear(row.id)); // remove the valuectx.observe(sessions.on(row.id), { new: true }, change => { ctx.log('info', `The count is now ${change.new}`);});Extends
Section titled “Extends”FacetRef<T>
Type Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
T |
the type of the values of the facet |
Properties
Section titled “Properties”alias?
Section titled “alias?”readonly alias?: string;A short name that searches can use for the facet, for example pomodoro in the search pomodoro>3.
Inherited from
Section titled “Inherited from”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.
Inherited from
Section titled “Inherited from”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.
Inherited from
Section titled “Inherited from”paths?
Section titled “paths?”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.
Inherited from
Section titled “Inherited from”schema
Section titled “schema”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.
Inherited from
Section titled “Inherited from”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.
Inherited from
Section titled “Inherited from”target
Section titled “target”readonly target: FacetTarget;The kind of thing that the facet is attached to: a row, an outline or the library.
Inherited from
Section titled “Inherited from”Methods
Section titled “Methods”clear()
Section titled “clear()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id |
string |
the id of the row or of the outline |
Returns
Section titled “Returns”the request
Inherited from
Section titled “Inherited from”dispose()
Section titled “dispose()”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.
Returns
Section titled “Returns”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
entity |
| { facets: Readonly<Record<string, unknown>>; } | null | undefined |
the row or outline whose value to read |
Returns
Section titled “Returns”T | undefined
the value, or undefined when the facet has no value there
Inherited from
Section titled “Inherited from”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id? |
string |
the id of the row or of the outline. Omit it for a facet of the library |
Returns
Section titled “Returns”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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id |
string |
the id of the row or of the outline |
value |
T |
the new value |
Returns
Section titled “Returns”the request
