Skip to content

Instantly share code, notes, and snippets.

@amb26
Created May 14, 2025 13:30
Show Gist options
  • Select an option

  • Save amb26/f631b81ae4dec4ae6e7f17f90a615510 to your computer and use it in GitHub Desktop.

Select an option

Save amb26/f631b81ae4dec4ae6e7f17f90a615510 to your computer and use it in GitHub Desktop.
/**
* Creates a single source effect that listens to changes in a computed signal and invokes a callback
* with the old and new values whenever the computed signal changes.
*
* @param {Computed} aComputed - The computed signal to observe.
* @param {Function} fn - A callback function with the signature (oldValue, newValue).
* @return {Effect} The created effect.
*/
fluid.singleSourceEffect = function (aComputed, fn) {
let oldValue = aComputed.value; // Initialize with the current value of the computed signal.
return effect(() => {
const newValue = aComputed.value; // Get the updated value of the computed signal.
if (oldValue !== newValue) {
fn(oldValue, newValue); // Notify the callback with old and new values.
oldValue = newValue; // Update the old value for the next change.
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment