Select to view content in your preferred language

Easier way to track (and test) changes to SnappingOptions?

362
0
04-09-2024 03:03 PM
ToddRunstein
Emerging Contributor

I'm trying to persist the settings as a User changes things like SnappingOptions:

Screenshot 2024-04-08 at 3.37.16 PM.png

Here's the gist of how I've attempted to do this:

const optionsObject = new SnappingOptions(initialSettings);
const listenForFeatureEnabled = optionsObject.watch('featureEnabled', () => {
    persistSnappingSettings(optionsObject); 
});
const listenForSelfEnabled = optionsObject.watch('selfEnabled', () => {
    persistSnappingSettings(optionsObject); 
});
// ... more code ...
optionsObject.addHandles([ 
     listenForFeatureEnabled, 
     listenForSelfEnabled,
     // ... more listeners ...
])

 

Things start to get ugly with the FeatureSnappingLayerSource objects.

const listenForFeatureSourceChanges = optionsObject.featureSources.on('change', (event) => {
    // This is helpful when a new layer is added to the MapView
    persistSnappingSettings(optionsObject);

    // @ts-ignore
    event.target.forEach((fs: FeatureSnappingLayerSource) => {
      if (!fs.get(isBeingTrackedIndicator)) {
        fs.set(isBeingTrackedIndicator, true);
        reactiveUtils.watch(() => fs.enabled, () => {
          persistSnappingSettings(optionsObject);
        });
      }
    });
  });

 

I have a 3 part question:

  1. Is there an easier way to watch these properties, or is this the "Esri way"?
  2. Could ignoring the handles returned on line 9 cause issues? Should I be doing an adding those handles to the optionsObject?
  3. How can I write unit tests for this? I can verify the optionsObject hasHandles(), but that's not too useful. Can I get ahold of those handles somehow and execute them, or is there some other approach that would let me test that functionality?
0 Kudos
0 Replies