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 asource().
Creating a Source
Section titled “Creating a Source”Use the source() function to create a new reactive unit.
import { source } from '@pulse-js/core';
// Primitiveconst count = source(0);
// Objectconst user = source({ name: 'Alice', id: 1 });Naming (Optional but Recommended)
Section titled “Naming (Optional but Recommended)”You can provide a name as the second argument. This is crucial for debugging with Pulse Tools.
const count = source(0, { name: 'global-count' });API Reference
Section titled “API Reference”source()
Section titled “source()”Returns a getter function. Calling the getter inside a Guard or Computed automatically tracks the dependency.
const value = count(); // Read value.set(newValue)
Section titled “.set(newValue)”Updates the value and notifies all subscribers.
count.set(5);.update(fn)
Section titled “.update(fn)”Updates the value based on the previous state.
count.update(n => n + 1);.subscribe(fn)
Section titled “.subscribe(fn)”Listen for changes manually. Returns an unsubscribe function.
const unsub = count.subscribe(val => { console.log('New value:', val);});
// Laterunsub();