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

passing lambda as a callback #436

Open
hemml opened this issue Apr 1, 2022 · 1 comment
Open

passing lambda as a callback #436

hemml opened this issue Apr 1, 2022 · 1 comment

Comments

@hemml
Copy link
Contributor

hemml commented Apr 1, 2022

I'm trying to use MutationObserver object to catch a DOM change using the following code (can be reproduced in JSCL console):

(defparameter obs (jscl::make-new (jscl::oget (jscl::%js-vref "window") "MutationObserver") (lambda (&rest args) (format t "~A~%" args))))
(defparameter cfg (jscl::new))
(setf (jscl::oget cfg "childList") t)
((jscl::oget obs "observe") ((jscl::oget (jscl::%js-vref "document") "getElementById") "console") cfg)

When I'm adding a node (using developer tools) to console div, getting the following:

(#<JS-OBJECT [object MutationObserver]>)

So, the lambda is called with just one argument, but MutationObserver callback should receive two. The first one is not passed to the lambda.
In JS console the code works fine, both args are passed:

obs=new MutationObserver((x,y)=>{console.log(x,y)})
MutationObserver {  }
obs.observe(document.getElementById("console"),{childList:true})
undefined
Array [ MutationRecord ] MutationObserver {  }
@vlad-km
Copy link
Member

vlad-km commented Apr 1, 2022

For MutationObserver (as well as for Promise and some others) you need to use such a constructor instead of make-new:

var make_Instance  =  function () {
                     var args = [].concat(null,Array.prototype.slice.call(arguments,2));
                     var fn = arguments[0][arguments[1]];
                     return new (Function.prototype.bind.apply(fn,args))();
                    };

https://github.com/vlad-km/web-api/blob/master/mutation-observer/package.lisp#:~:text=(eval%2Dwhen%20(%3Acompile,make%2Dinstance%2Dproto))))

see its use:

(defun new-observer (fun)
    "Constructor for instantiating new DOM mutation observers"
    (#j:make_Instance #j:window "MutationObserver"
                      (lambda (mutations observer)
                          ;; mutations - array of MutationRecord
                          ;; observer  - some mutant observer
                          (funcall fun mutations))))

https://github.com/vlad-km/web-api/blob/master/mutation-observer/observer.lisp#:~:text=(defun%20new%2Dobserver,funcall%20fun%20mutations))))

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