Passing Blocks (or Procs) as JS Functions #32
Closed
richardboehme
started this conversation in
General
Replies: 1 comment
-
@richardboehme This feature looks reasonable to me. PR will be welcomed :) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey it's me again with another question:
In JS it's common to pass functions as arguments into other functions to provide some type of callback. In Ruby we have a pretty similar approach, but instead of using methods directly we pass in blocks (or procs converted to blocks). Currently it's not possible to pass a block (or proc) to a JS::Object#call function. Something like the following is not possible:
In my opinion it would be worth to add this feature. I thought about how it could be implemented and came across an idea of doing it. My idea is to provide a
Proc#to_js
method that converts the proc into a JS::Object. The JS::Object would be a JS function that holds a reference to the Proc's RbValue object. The function body calls thecall
function of the Proc to execute it. I implemented this and it seems to be working. However, I do not know yet if it would behave exactly like a normal block would do. Also we would have to figure out what we want to do with block arguments (I guess they could be JS::Objects, maybe primatives could be converted to their Ruby counterparts).What do you think about this approach? Is there a better way of solving the problem? If you want to I can create a draft PR with my changes to discuss them.
Beta Was this translation helpful? Give feedback.
All reactions