Created
May 14, 2025 13:30
-
-
Save amb26/f631b81ae4dec4ae6e7f17f90a615510 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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