Defold: Implement JsToDef functionality as part of `html5` module

Created on 27 Dec 2020  路  3Comments  路  Source: defold/defold

Task (REQUIRED):
It would be nice to implement JsToDef functionality as part of html5 module.

On Lua side:

local function js_listener(self, message)
...
end

html5.add_listener("listener_id", js_listener)
--or
html5.add_listener(hash("listener_id"), js_listener)

html5.remove_listener("listener_id", js_listener)
--or
html5.remove_listener(hash("listener_id"), js_listener)

html5.remove_all_listeners("listener_id")
--or
html5.remove_all_listeners(hash("listener_id"))

On JS side:

//Module or some other module
Module.send("listener_id", "custom message");
Module.send("listener_id", {foo:"bar", num:16, isAv:true});
Module.send("listener_id", 19.2);
Module.send("listener_id", 18);
Module.send("listener_id", "custom string");
Module.send("listener_id");
Module.send("listener_id", true);
Module.send("listener_id", false);

Expected outcome (REQUIRED):
JsToDef provides functionality that's is really important and basic for html5 developers. It should be part of the engine as a method in html5 module.

feature design feature request html5 task

Most helpful comment

Ah, yes, good point with the multiple callbacks to more easily support multiple modules. Ok, so. no html5.set_listener(). Instead we should have html5.add_listener() and html5.remove_listener().

All 3 comments

I agree with you @AGulev. I think this is really useful functionality.

We need to discuss the API though. On the JS side I think we should have a Defold.* namespace and not hide it in Module if possible.

I'd also like to discuss the "listener_id", is this needed? And should it be html5.add_listener() and html5remove_listener() or simply html5.set_listener()?

Perhaps more closely replicating on_message(self, message_id, message):

html5.set_listener(function(self, message_id, message)
end)
Defold.post("ad_ready", { placement: 1 })

Yes, maybe.
Now jstodef supports multiple callbacks. The idea was to create an easier way of separated logic for different modules.

For example if we connect 2 different SDK on JS side, probably we want to use two different script/lua files on Defold side.
Also, it would be easier to publish such integration as separate module which contains lua+js (based only on this new functionality) without cpp at all.

But in case with single listener it will be much harder to do in a nice way.
That's why I moved message_id ("listener_id") in my example as part of registration and sending data, not receiving.

Ah, yes, good point with the multiple callbacks to more easily support multiple modules. Ok, so. no html5.set_listener(). Instead we should have html5.add_listener() and html5.remove_listener().

Was this page helpful?
0 / 5 - 0 ratings