Giter Site home page Giter Site logo

Support for events? about nbind HOT 3 CLOSED

charto avatar charto commented on May 15, 2024
Support for events?

from nbind.

Comments (3)

jjrv avatar jjrv commented on May 15, 2024 1

libui-node binds a C GUI library to JavaScript using nbind and has several examples of listening to events. Basically you need a C++ function that takes a JavaScript callback, which it then copies and stores for calling when an event happens.

See for example onClosing in UiWindow. The UiWindow::onClosing method is exported to JavaScript and takes a callback, copies it and tells libui to call UiWindow_onClosing with the JavaScript callback as custom data. That function then casts the data pointer back to a JavaScript callback handle and calls it.

from nbind.

parro-it avatar parro-it commented on May 15, 2024 1

Regarding your comment:

//could I not just as well drop the parameter and just call SensorEventCallback here?

No, you can't because your static method has no access to this. You have to pass either the callback as you are doing, or the current instance in data argument.

from nbind.

spaasis avatar spaasis commented on May 15, 2024

Oh yeah, I tried to get what the libui was doing earlier, but your explanation really explained it well. My working test code is below - any gotchas?

class EventTest{
private:
  nbind::cbFunction * SensorEventCallback;

public:

  void onSensorEvent(nbind::cbFunction & cb) {
    SensorEventCallback = new nbind::cbFunction(cb);
  }

  static void Adapter_SensorEvent(int n,void *data)
  {
    nbind::cbFunction *cb = (nbind::cbFunction *) data; 
//could I not just as well drop the parameter and just call SensorEventCallback here?
    printf("SensorEventC++:%d\n", n);
    (*cb)(n);
  }

  void open(){
    printf("Opening\n");
    fakeEvents(4);
    printf("Opened\n");
  }

  void close(){
    printf("Closed\n");
  }

  void fakeEvents(int n){
    for (int i=n; i>0; i--) {
      Adapter_SensorEvent(i, SensorEventCallback);
      std::this_thread::sleep_for (std::chrono::seconds(1));
    }
  }
};

from nbind.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.