Skip to content

Sources

Sources are the primitive containers for your application state. They hold values and notify dependents when those values change.

In Pulse v3, for complex objects and deep reactivity, it is highly recommended to use the new pulse() API instead of wrapping objects in a source().

Use the source() function to create a new reactive unit.

import { source } from '@pulse-js/core';
// Primitive
const count = source(0);
// Object
const user = source({ name: 'Alice', id: 1 });

You can provide a name as the second argument. This is crucial for debugging with Pulse Tools.

const count = source(0, { name: 'global-count' });

Returns a getter function. Calling the getter inside a Guard or Computed automatically tracks the dependency.

const value = count(); // Read value

Updates the value and notifies all subscribers.

count.set(5);

Updates the value based on the previous state.

count.update(n => n + 1);

Listen for changes manually. Returns an unsubscribe function.

const unsub = count.subscribe(val => {
console.log('New value:', val);
});
// Later
unsub();