FlexLayout icon indicating copy to clipboard operation
FlexLayout copied to clipboard

Adding 'visible' event listener doesn't trigger first time hidden tab becomes visible

Open ChillyBots opened this issue 4 years ago • 2 comments

When you adding an event listener to a tab:

 if (node.getComponent() === 'myComponent') {
        node.setEventListener('visibility', () => {
           setLoadedMyComponent(true);
        });
}

This will only fire the second time the component becomes visible if the component starts off as a hidden tab. I can see from consoling the node.GetComp() the listener is added straight away. However the event handler doesn't seem to understand it has become visible

ChillyBots avatar Mar 24 '21 20:03 ChillyBots

The problem is that the callback is called from the layout method, before the component is actually rendered (via a call to the factory), so by the time the factory method is called it's too late to register the handler for the first callback.

I hadn't realised this before, may need to change how this works.

One way around it is to register the listener against the model using something like:

model.visitNodes((node: Node, level: number) => {
    if (node instanceof TabNode) {
        node.setEventListener("visibility", function(p){console.log("visibility", node.getName(), p);});
    }
});

nealus avatar Mar 25 '21 08:03 nealus

This sounds good, is a it a big change? I've actually come across a second use case for this today.

I'll see if I can use the model in the mean time

EDIT: Works a treat, thanks @nealus

ChillyBots avatar Apr 06 '21 10:04 ChillyBots