Live values
Publish values that looks, expressions and other plugins read.
plugin.state publishes values that looks, expressions and other plugins read as
<namespace>.<id> (the namespace is the name without sdeck-plugin-):
const temperature = plugin.state.number("temperature", "Temperature", {
unit: "°C",
// Read while something shows it…
poll: { everyMs: 60_000, read: () => fetchTemperature() },
});
const playing = plugin.state.boolean("playing", "Playing", {
// …or pushed while watched: return a function that stops.
start: (set) => player.onChange((p) => set(p.playing)),
});Both run only while something watches the value. What you get back is how the rest of the plugin
uses it: temperature.set(21) publishes one directly, get() reads it, use() follows it in a
component, template is {{weather.temperature}} for texts and key is weather.temperature for
expressions.
When several values come from one connection or request, put it in a Shared: it starts with the
first that listens, and stops after the last. following makes a value of each part, and
useShared follows it in a component:
const player = new Shared<Player | undefined>((emit) => connect(emit)); // returns how to stop
const track = plugin.state.string("track", "Track", { start: following(player, (p) => p.track) });
const volume = plugin.state.number("volume", "Volume", {
start: following(player, (p) => p.volume),
});To follow other plugins' values, context.state.watch(["spotify.uri"], listener) keeps
them up to date and calls you on every change (it needs state, as does get).