Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component's event subscriber resolves callback on subscription #1693

Open
TitouanGisle opened this issue Oct 11, 2024 · 0 comments
Open

Component's event subscriber resolves callback on subscription #1693

TitouanGisle opened this issue Oct 11, 2024 · 0 comments

Comments

@TitouanGisle
Copy link

Description

It seems to me that when a subscriber to an event is created on a component, the callback to this subscriber is instantaneously resolved if said component had previously fired the event.

Context

In my application, I tried to implement a button to create a sectionPlane from a surface :

  • my application fires the 'picked' event on the Viewer's SectionPlanePlugin when a element is clicked in the Scene:
    handleSelectionClick(pickResult = null) {
    
        if (!pickResult) {
            return;
        }
    
        console.log('Firing "picked" event');
        this.sectionPlanesPlugin.fire('picked', pickResult);
        
        // Do other stuff related to picking viewer elements
    }
  • When my user clicks the "create a section plane from a surface" button, I create a subscriber to the SectionPlanePlugin 'picked' event, using the once() function :
    prepareSectionPlaneFromSurface()
    {
        this.sectionPlanesPlugin.once('picked', this.createSectionPlaneFromSurface);
        console.log("Ready to pick surface");
    }
  • The callback to this subscriber creates a SectionPlane :
    createSectionPlaneFromSurface(pickResult)
    {
        console.log("Creating plane from picked surface");
        let plane = this.sectionPlanesPlugin.createSectionPlane({pos: pickResult.worldPos, dir: pickResult.worldNormal});
        this.sectionPlanesPlugin.showControl(plane.id);
    }

Current behavior

  • When clicking the "create a section plane from a surface" button for the first time, then clicking the model, a section plane is successfully created.
  • When clicking the "create a section plane from a surface" button a second time, a section plane is immediately created, and another section plane is created when the model is clicked.
  • On a fresh Viewer/SectionPlanePlugin, if the user clicked a model, and a 'picked' event was fired, a section plane is immediately created when the user clicks the "create a section plane from a surface" button.

Expected behavior

Resolving the callback to a subscriber on its creation if the event is "already" in the Component seems like a strange behavior to me.

Local correction

I managed to correct this behaviour by deleting the registered 'picked' event from the SectionPlanesPlugin :

    prepareSectionPlaneFromSurface()
    {
        delete this.sectionPlugin._events.picked;

        this.sectionPlugin.once('picked', this.createSectionPlaneFromSurface);
    }

** Xeokit Version :**
2.6.69

Desktop :

  • OS: Win11
  • Browser: Firefox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant