'Leaflet performance issues

I am listening to an EVENT on my map object, firing a function on this event. In this function body, I am calculating an angle which is needed outside the function, but can not reach it. So check how I did the trick.

Is that a good practice ? I think this leads to performance issue. What do you think ? How could I access my e.segment outside of the first loop ?

map.on('pm:drawstart', ({ workingLayer }) => {
    workingLayer.on('pm:snap', (e) => {
        if (e.shape === 'outlet') {
            if (e.segment) {
                const segment = e.segment
                workingLayer.setRotationAngle(angle(segment))

                map.on('pm:create', (e) => {
                    if (e.shape === 'outlet') {
                        console.log(e.layer)
                        e.layer.setRotationAngle(angle(segment))
                
                    }
                });
            }
        }
    })
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source