Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { anchor, createObserver, type Immutable } from '@anchorlib/core';
import { onCleanup } from 'solid-js';
import { REF_REGISTRY } from './reactive.js';
import type { ConstantRef, StateRef } from './types.js';
/**
* @deprecated use `effect()` instead.
* Creates a reactive reference that automatically updates when its dependencies change.
*
* This function creates an observable reference that tracks dependencies and automatically
* updates its value when any of the dependencies change. It uses the anchor library's
* observer system to track reactivity and integrates with Solid's cleanup system.
*
* @template R - The type of value being observed
* @param observe - A function that returns the value to be observed. This function
* will be tracked for dependencies and re-run when they change.
* @returns A ConstantRef object with a reactive value property that updates automatically
*/
export function observedRef<R>(observe: () => R): ConstantRef<Immutable<R>> {
const observer = createObserver(() => {
valueRef.value = observer.run(() => observe());
});
const valueRef = anchor(
{
value: observer.run(() => observe()),
},
{ recursive: false }
) as StateRef<R>;
const stateRef = {
get value() {
return valueRef.value;
},
};
REF_REGISTRY.add(stateRef);
onCleanup(() => {
observer.destroy();
anchor.destroy(valueRef);
REF_REGISTRY.delete(stateRef);
});
return stateRef as ConstantRef<Immutable<R>>;
}
|