B4J Question [BANAno] [SOLVED] How to pass a global variable to a callback successfully?

Mashiane

Expert
Licensed User
Longtime User
Ola

I have a variable defined in process globals.

B4X:
Public VuexState As Map

I initialize this with CreateMap() in Initialize and then use it on

B4X:
Sub SetVuexMutation(Module As Object, MutationName As String, Payload As Map)
    MutationName = MutationName.ToLowerCase
    Dim cb As BANanoObject = BANano.CallBack(Module, MutationName, Array(VuexState, Payload))
    VuexMutations.Put(MutationName, cb)
End Sub

Simple project attached... i'm getting

B4X:
Uncaught SyntaxError: Unexpected token '.'
 

Attachments

  • CallBackGlobalVariable.zip
    234 KB · Views: 180

alwaysbusy

Expert
Licensed User
Longtime User
In this case you can't, as the .CallBack line is just making a callback definition. I can't see how it is used further in your example, but as this is just a definition, I suspect this would work with dimming a temp variable. (this is only needed because the B4J compiler will complain of the use of an un-dimmed variable).

Transpiled to this definition:
B4X:
_cb=function(_tmpstate,_payload) {if (typeof _module[(_mutationname).toLowerCase()]==="function") {return _module[(_mutationname).toLowerCase()](_tmpstate,_payload,_B)};};

When the cb is called, it can use the global variable as parameter, although this looks like a weird construction as it would be able to access the vuexState variable anyway. And as vuexState is an object, it is passed byRef, and the result would be the same.

B4X:
Sub SetVuexMutation(Module As Object, MutationName As String, Payload As Map)
    MutationName = MutationName.ToLowerCase
    Dim tmpState As Map ' dimmed here just because B4J would else give an error in the next line
    Dim cb As BANanoObject = BANano.CallBack(Module, MutationName, Array(tmpState, Payload))
    VuexMutations.Put(MutationName, cb)
End Sub

Alwaysbusy
 
Upvote 0
Top