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

addEventListener as |this| #216

Open
paulushub opened this issue Apr 4, 2020 · 2 comments
Open

addEventListener as |this| #216

paulushub opened this issue Apr 4, 2020 · 2 comments

Comments

@paulushub
Copy link

This sample is found on the Mozilla site: addEventListener

const Something = function(element) {
  // |this| is a newly created object
  this.name = 'Something Good';
  this.handleEvent = function(event) {
    console.log(this.name); // 'Something Good', as this is bound to newly created object
    switch(event.type) {
      case 'click':
        // some code here...
        break;
      case 'dblclick':
        // some code here...
        break;
    }
  };

  // Note that the listeners in this case are |this|, not this.handleEvent
  element.addEventListener('click', this, false);
  element.addEventListener('dblclick', this, false);

  // You can properly remove the listeners
  element.removeEventListener('click', this, false);
  element.removeEventListener('dblclick', this, false);
}
const s = new Something(window); // changed document.body to window for the demo

Defining a window object in C# as follows, I received the call to the addEventListener method.

public class JsSvgWindowNiL {
    public void addEventListener(string type, object listener, bool useCapture) {
        Trace.WriteLine("addEventListener(string type, object listener, bool useCapture)");

        Function listenerFunc = null;
        var listenerValue = listener as JSValue;
        if (listenerValue != null) {
            listenerFunc = listenerValue.Value as Function;
        }
    }
}

However, for the above script, the listenerValue.Value is JSObject (not Function as in all other cases). How do I invoke/call the JS handler in this case?

@nilproject
Copy link
Owner

nilproject commented Apr 5, 2020

I did not immediately understand the details of the question. As I can see, you want to invoke method handleEvent for Something. For this you should to get value of property handleEvent from object via GetProperty("handleEvent"), then cast this value to a Function and call it via Function.Call(...) with passing listener as this

@paulushub
Copy link
Author

@nilproject Thanks for the reply. This is about integrating a scripting into an SVG rendering library. So that will be a user written script and the handleEvent method is not known to the rendering library.
When you run this script in ClearScript.V8 engine, the listener is a ScriptObject as with other listeners and calling the invoke method defined on this object instance will automatically invoke the handleEvent method, since in this case as stated in the comment,

// Note that the listeners in this case are |this|, not this.handleEvent

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

No branches or pull requests

2 participants