Viewer: async functions when loading multiple models

Hi @alex
I come back to this sandbox you wrote to load and show/hide multiple versions of a model :
When I import just before a function from a ts extensions, it runs multiple times (as many as the versions loaded). Example :

const menu = viewer.createExtension(Menu);
  viewer.on(ViewerEvent.LoadComplete, async () => {
    menu.addMenu();
  });

I guess it’s because it is async? Have you an idea how to bypass this? Because I still need the viewer to be loaded before running the function.
Thanks,
Raphaël

Hi @RaphaelVouilloz

ViewerEvent.LoadComplete’s event handler has an argument that is the object id that finished loading.

const menu = viewer.createExtension(Menu);
viewer.on(ViewerEvent.LoadComplete, async (objId) => {
  console.log(`Object ${objId} finished loading`)
  menu.addMenu();
});

For each loadObject call you make, a ViewerEvent.LoadComplete event will get emitted with the corresponding object id.

So I guess that if you want to know when all objects that you asked the viewer to load have finished loading, you just need to keep track of them in the ViewerEvent.LoadComplete events by looking at which object finished, and so determining if there are any others waiting to finish loading

Cheers

1 Like

Hi @alex
Thanks ok nice I understand. I keep track of the loaded versions so :

let loadedVersions: number = 0
  viewer.on(ViewerEvent.LoadComplete, async () => {
    loadedVersions = loadedVersions + 1
    /** Functions to run one time for all versions */
    if (loadedVersions === 1) {
      menu.addMenu();
      navigation.addNavigation(cameraController);
    } 
    /** Functions to run for each version */
    nominations.addNominations();
    sets.buildSetsTree();
  });

Best,
Raphaël

1 Like