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

Callbacks lifetime #15

Closed
parro-it opened this issue Jun 10, 2016 · 7 comments
Closed

Callbacks lifetime #15

parro-it opened this issue Jun 10, 2016 · 7 comments

Comments

@parro-it
Copy link
Contributor

Hi, I'm planning on using nbind to write libui bindings for node.
nbind will greatly simplify the job, but I will need to use callbacks for UI events...

Warning: currently callbacks have a very short lifetime! They can be called only until the first function that received them returns. That means it's possible to create a function like Array.map which calls a callback zero or more times and then returns, never using the callback again. It's currently not possible to create a function like setTimeout which calls the callback after it has returned.

Will this limitations of callbacks usage be removed in near future? There is some known workaround to make them work (as in setTimeout usage)?

@jjrv
Copy link
Member

jjrv commented Jun 10, 2016

I'll look into this ASAP, others have been requesting the same thing. It seems nan may have lifted some earlier restriction, since I don't see any serious difficulty in implementing this now.

@parro-it
Copy link
Contributor Author

Awesome! Thank you!

@jjrv
Copy link
Member

jjrv commented Jun 10, 2016

Wow, I'm impressed how your bindings are already using nbind quite extensively. I got them working on my development machine and will be testing with callbacks soon. It seems to me they may already work, the callback reference you receive as a parameter will become invalid after the call returns, but you can do:

nbind::cbFunction *persistentCallback = new nbind::cbFunction(callback);

It's never been tested though, so we'll see. Note that the callback must be called in the same thread where V8 runs or it will crash. Fixing that will require using Nan::AsyncWorker and is hopefully not yet necessary.

@parro-it
Copy link
Contributor Author

parro-it commented Jun 10, 2016

Wow, I'm impressed how your bindings are already using nbind quite extensively.

You did an awesome job with nbind 👍

will be testing with callbacks soon

I'll create a separate branch with a failing test that uses nbind callbacks. You can use it to test nbind if you want.

Note that the callback must be called in the same thread where V8 runs or it will crash.

libui run in the javascript thread by default, so this shouldn't be a problem.

@parro-it
Copy link
Contributor Author

There is a multithread libui example but I actually not support them to avoid the problem.
But it could be a useful improvement in future...

@parro-it
Copy link
Contributor Author

I successfully implemented WindowOnClosing before switching to nbind on this commit in libui-node if it could be useful for some reference...

@parro-it
Copy link
Contributor Author

It seems to me they may already work, the callback reference you receive as a parameter will become invalid after the call returns, but you can do:

nbind::cbFunction *persistentCallback = new nbind::cbFunction(callback);

It's never been tested though, so we'll see.

It's working well, after I save the callback as you suggested... you can see here how I implemented the change event for a widget.

Thank you!

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