Subscriptions
Graphlet allows you to subscribe to graph changes.
Subscriptions are framework-agnostic. They work without React, Svelte, Vue or any UI library.
React users usually do not need to call subscribe() directly because the React adapter provides useGraph(graph), but subscriptions are the low-level mechanism behind it.
Basic usage
import { Graph } from "@graphlet/core";
const graph = new Graph(
[
["step1", ["step2"]],
["step2", []]
] as const,
{
initial: "step1"
}
);
const unsubscribe = graph.subscribe((snapshot, event) => {
console.log(snapshot);
console.log(event);
});The listener receives two arguments:
(snapshot, event) => voidSnapshot
The snapshot describes the current graph state.
{
current: "step1",
next: ["step2"],
context: undefined,
history: ["step1"]
}Snapshot includes:
{
current: Node;
next: Node[];
context: Context;
history: Node[];
}Event
The event describes why the subscription was triggered.
Graphlet currently emits these event types:
"init"
"transition"
"context"
"back"
"history.clear"
"reset"Init event
subscribe() calls the listener immediately with an init event.
{
type: "init",
current: "step1"
}This makes subscriptions convenient for UI adapters because they can render the initial state immediately.
Transition event
A successful goTo() emits a transition event.
graph.goTo("step2", {
source: "button"
});Subscriber receives:
{
type: "transition",
from: "step1",
to: "step2",
payload: {
source: "button"
}
}The snapshot already contains the new current node.
{
current: "step2",
next: [],
context: undefined,
history: ["step1", "step2"]
}Failed transitions do not notify subscribers.
Context event
setContext() emits a context event.
graph.setContext((ctx) => ({
...ctx,
completed: ["step1"]
}));Subscriber receives:
{
type: "context",
current: "step1",
previousContext: {
completed: []
},
context: {
completed: ["step1"]
}
}Back event
back() emits a back event when it succeeds.
graph.back();Subscriber receives:
{
type: "back",
from: "step2",
to: "step1"
}If history is empty, back() returns a failure result and does not notify subscribers.
History clear event
clearHistory() emits a history.clear event.
graph.clearHistory();Subscriber receives:
{
type: "history.clear",
current: "step2"
}Reset event
reset() emits a reset event.
graph.reset();Subscriber receives:
{
type: "reset",
from: "step2",
to: "step1"
}The snapshot contains the initial node and reset history.
{
current: "step1",
next: ["step2"],
context: undefined,
history: ["step1"]
}Unsubscribe
subscribe() returns an unsubscribe function.
const unsubscribe = graph.subscribe((snapshot, event) => {
console.log(snapshot, event);
});
unsubscribe();After calling unsubscribe, the listener will no longer be called.
React usage
In React, prefer useGraph(graph).
import { useGraph } from "@graphlet/react";
function DebugPanel({ graph }) {
const { snapshot, event } = useGraph(graph);
return (
<pre>
{JSON.stringify({ snapshot, event }, null, 2)}
</pre>
);
}Use low-level subscribe() directly only when you need custom integrations.