wasm: allow non-global scope callbacks #72
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the JS callbacks for
onlocalprivcreate,onremotekeyreceive, andonauthdatamust be on the JS global scope for the WASM to call them. This adds a potential for race conditions when connecting to multiple nodes simultaneously because each connection will share the same callback functions that are defined on the global scope. Depending on the order in which the connections are established and callbacks are called, the local/remote keys returned may be associated with the wrong connection.This PR allows the app to specify callbacks nested under a JS object, ex:
'--onauthdata=myapp.onAuthData'. The app would have needed to create the nested object like this, for example:The app can also leverage the support for namespaces by placing the callbacks on a JS object using the same name as the namespace, ex
'--onauthdata=' + namespace + '.onAuthData'. This would eliminate the chances of a race condition because each connection would have its own callback function defined under a namespace scoped JS object.I implemented this in a way that maintains backward compatibility, so apps can continue to use
'--onauthdata=onAuthData'with the callback defined on the global scope if they choose to. It just now supports the option of using non-global scoped callbacks to handle the scenarios where multiple connections are established concurrently.