Known problem? I construct message objects for a m...
# javascript
h
Known problem? I construct message objects for a messaging system with builder constructs. I first build an update, then use this update in a payload builder to send off over http. When this is compiled to JS, the same variable name is used over, which is fine by me, but since I use builders that return self (this) in a lot of functions, the variable changes contents before use, resulting in a classcast exception. I will post an example in a thread here.
Here is what I write in my kotlin code:
Copy code
val update = UpdateBuilder()
        .customer(customer)          // returns the UpdateBuilder
        .jsEntities(listOf(product)) // returns the UpdateBuilder
        .what(Actions.BROWSE)        // returns the UpdateBuilder
        .build()                     // returns an update object

    val payload = PayloadBuilder()   
        .service(service)            // returns the PayloadBuilder
        .updates(update)             // returns the PayloadBuilder
        .build()                     // returns a payload object
... and here is the generated JS code (broken up in lines in order to comment):
Copy code
c = (new y)
        .customer_61zpoe$(ya)           // returns the UpdateBuilder
        .jsEntities_mhpeer$(v(a))       // returns the UpdateBuilder
        .what_61zpoe$(p.Actions.BROWSE) // returns the UpdateBuilder
        .build(),                       // returns an update object

    c = (new ba)                        // wait a sec... c is now a PayloadBuilder
        .service_61zpoe$(Ib)            // returns the PayloadBuilder
        .updates_7f32tz$([c])           // ... so submitting c here will throw!
        .build(),
a
This looks like code, processed by some minifier. Could you provide the original js code please?
h
Ah, @anton.bannykh, you're right! Here's what the kotlin transpiler produced:
Copy code
var update = (new UpdateBuilder()).customer_61zpoe$(customer).jsEntities_mhpeer$(listOf(product)).what_61zpoe$(messageUtilities.Actions.BROWSE).build();
    var payload = (new PayloadBuilder()).service_61zpoe$(service).updates_7f32tz$([update]).build();
... so my new prime suspect is the google closure compiler!
a
😃
h
Thanks.
a
If you do find a code pattern that results in closure compiler misbehaviour, please do share it with us. 😃
h
Like above? Because it was indeed the closure compiler that did it ...
a
I am just curious )
Like, maybe it is possible to avoid generating a particular code pattern (if the bug doesn't get fixed)
h
Yes, I think maybe there could be some annotation to add for it. I looked it up, but there was too much documentation, so I just re-wrote my code to make it work instead.