The rule is evaluated with test(index, target) whenever rs-x decides if a nested value should remain observed. Returning true keeps recursive observation active for that member path.
Without a watch rule, rs-x still tracks root assignments and collection membership mutations. But nested member/property changes under leaf values are only tracked when the rule allows them.
Current runtime behavior uses factory-managed rules keyed by (context, index) pairs to keep watch-rule identity stable and disposable.
Where To Pass The Rule
Use it either at expression binding time (leafIndexWatchRule) or directly in state manager (watchState(..., { indexWatchRule })).
IndexWatchRuleFactory creates lightweight rules that match exactly one context/index pair. This is the default watch-rule implementation used by state-manager.
import{IndexWatchRuleFactory}from'@rs-x/state-manager';constmodel={user:{profile:{name:'Ada'}}};constfactory=newIndexWatchRuleFactory();// The runtime creates a rule for one (context, index) pair.// Rule semantics: test(nextIndex, nextTarget) is true only when// nextIndex === index && nextTarget === context.construle=factory.create(model,'user');// Use it in watchState/rsx APIs, then dispose when done.stateManager.watchState(model,'user',{indexWatchRule:rule});rule.dispose();
constmodel={items:[{label:'A',qty:1,note:'keep'},{label:'B',qty:2,note:'keep'},],};constwatchRule={context:{trackedItemProperty:'qty'},test(index,target){if(target===model&&index==='items'){returntrue;}if(Array.isArray(target)){returntrue;// watch each array slot}returnString(index)===this.context.trackedItemProperty;},};
Map Example
Tracks only the admin key branch and enabled property changes.