Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for another bind() #924

Open
g40 opened this issue Mar 22, 2023 · 0 comments
Open

Suggestion for another bind() #924

g40 opened this issue Mar 22, 2023 · 0 comments

Comments

@g40
Copy link

g40 commented Mar 22, 2023

Persuant to #923. I added the following code to my fork of webview to check the C++ side of things is all OK.

  // another overload of bind() with user data
  // using statement means we can overload cleanly
  using sync_binding_arg_t = std::function<std::string(std::string, void*)>;
  // invoked with user data
  void bind(const std::string &name, sync_binding_arg_t fn, void* arg) {
    auto wrapper = [this, fn](const std::string &seq, const std::string &req,
                              void * arg) { resolve(seq, 0, fn(req,arg)); };
    bind(name, wrapper, arg);
  }

Example use therefor:

// invoked with instance data, so JS -> C++ -> JS
std::string callout2(const std::string& arg, void* data)
{
	webview::webview* pwv = reinterpret_cast<webview::webview*>(data);
	std::string ret = std::string("{ \"status\": true, \"data\": \"JSONpacket\" }");
	std::printf("%s (%p) => %s\n", arg.c_str(), pwv, ret.c_str());
	// JSON status message
	return ret;
}

// example usage
webview::webview w(true, nullptr);
w.bind("callout2", callout2, &w);

This works, with the promise completing as expected:

callit:  callout2 ƒ () {
        var seq = RPC.nextSeq++;
        var promise = new Promise(function(resolve, reject) {
          RPC[seq] = {
            resolve: resolve,
            reject: reject,
          };
     …
index.html:61 ret =>  Promise {<pending>}
index.html:36 success:  {status: true, data: 'JSONpacket'}

If interested I can submit a PR.

Thanks for listening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant