isapir / lucee-websocket

Enables server WebSockets for Lucee via JSR-356 compliant servlet containers (e.g. Tomcat 8, Jetty 9.1, etc.)
GNU Lesser General Public License v2.1
17 stars 6 forks source link

Application is defined while applicationScope is passed into listener methods #18

Closed redtopia closed 5 years ago

redtopia commented 5 years ago

I thought that the Application scope could only be accessed via the applicationScope argument in the listener methods as described in the docs/api. However, I just realized that I can access the Application scope directly. For example, the application scope argument is named applicationScope in the onOpen() method, yet I can dump the application variable and it's what I expect it to be. Is this expected behavior, or is this some weird side effect?

isapir commented 5 years ago

It's not reliable in all circumstances. I recommend typing the 5 extra characters. If you use the scope in many places in the method then you can set an alias to it at the top, e.g.

function onOpen(...) {
    var app = arguments.applicationScope;
    // now use app
}
redtopia commented 5 years ago

Thanks Igal. Could you elaborate under which circumstances would it be unreliable? I'm just trying to get a feel for why/how.

isapir commented 5 years ago

I honestly do not remember the details and do not have the time to dig into the code. I just remember that there is a reason for it, most likely because the events are asynchronous. It's possible that in onOpen() it would work but in other event handlers it would not, so it's more consistent to do it that way.