Skip to content

Signals

Signals in JavaScript were first popularized by SolidJS. They are a way to invalidate state when a dependency changes. In cloudstate, we can use signals to invalidate methods when their dependencies change.

For example, let’s say we want to rerun the method getCount on the client every time the count changes. We’ll use the @signal decorator to mark count as a signal. Since getCount and count both depend on count, they will both be invalidated when count changes.

@cloudstate
class Counter {
static id = "counter" as const;
@signal count = 0;
increment() {
return ++this.count;
}
getCount() {
return this.count;
}
}

We can use a for await loop on the client to watch for methods becoming invalid. Notice that the getCount method is not automatically run when it becomes invalid. It’s up to you to call the method manually if you require it’s result. If your use case doesn’t gain from this back and forth communication, or you require a more server driven approach, see the streams api.

const counter = useCloud<typeof Counter>("counter");
for await (const getCount of counter.getCount) {
console.log(await getCount());
}