|
| 1 | +#ifndef NEON_EVENTHANDLER_H_ |
| 2 | +#define NEON_EVENTHANDLER_H_ |
| 3 | + |
| 4 | +#include <uv.h> |
| 5 | +#include "neon.h" |
| 6 | +#include "v8.h" |
| 7 | +#include <mutex> |
| 8 | + |
| 9 | +namespace neon { |
| 10 | + |
| 11 | + // THe implementation of this class was adapted from |
| 12 | + // https://github.com/mika-fischer/napi-thread-safe-callback |
| 13 | + class EventHandler { |
| 14 | + public: |
| 15 | + EventHandler(v8::Isolate *isolate, |
| 16 | + v8::Local<v8::Value> self, |
| 17 | + v8::Local<v8::Function> callback): isolate_(isolate), close_(false) |
| 18 | + { |
| 19 | + async_.data = this; |
| 20 | + uv_async_init(uv_default_loop(), &async_, async_complete); |
| 21 | + // Save the this argument and the callback to be invoked. |
| 22 | + self_.Reset(isolate, self); |
| 23 | + callback_.Reset(isolate, callback); |
| 24 | + // Save the context (aka realm) to be used when invoking the callback. |
| 25 | + context_.Reset(isolate, isolate->GetCurrentContext()); |
| 26 | + } |
| 27 | + |
| 28 | + void schedule(void *rust_callback, Neon_EventHandler handler) { |
| 29 | + { |
| 30 | + std::lock_guard<std::mutex> lock(mutex_); |
| 31 | + handlers_.push_back({ rust_callback, handler }); |
| 32 | + } |
| 33 | + uv_async_send(&async_); |
| 34 | + } |
| 35 | + |
| 36 | + void close() { |
| 37 | + // close is called when the rust struct is dropped |
| 38 | + // this guarantees that it's called only once and |
| 39 | + // that no other method (call) will be called after it. |
| 40 | + close_ = true; |
| 41 | + uv_async_send(&async_); |
| 42 | + } |
| 43 | + |
| 44 | + void complete() { |
| 45 | + // Ensure that we have all the proper scopes installed on the C++ stack before |
| 46 | + // invoking the callback, and use the context (i.e. realm) we saved with the task. |
| 47 | + v8::Isolate::Scope isolate_scope(isolate_); |
| 48 | + v8::HandleScope handle_scope(isolate_); |
| 49 | + v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate_, context_); |
| 50 | + v8::Context::Scope context_scope(context); |
| 51 | + |
| 52 | + v8::Local<v8::Value> self = v8::Local<v8::Value>::New(isolate_, self_); |
| 53 | + v8::Local<v8::Function> callback = v8::Local<v8::Function>::New(isolate_, callback_); |
| 54 | + |
| 55 | + while (true) { |
| 56 | + std::vector<HandlerData> handlers; |
| 57 | + { |
| 58 | + std::lock_guard<std::mutex> lock(mutex_); |
| 59 | + if (handlers_.empty()) { |
| 60 | + break; |
| 61 | + } else { |
| 62 | + handlers.swap(handlers_); |
| 63 | + } |
| 64 | + } |
| 65 | + for (const HandlerData &data : handlers) { |
| 66 | + data.handler(self, callback, data.rust_callback); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (close_) { |
| 71 | + uv_close(reinterpret_cast<uv_handle_t*>(&async_), [](uv_handle_t* handle) { |
| 72 | + delete static_cast<EventHandler*>(handle->data); |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private: |
| 78 | + static void async_complete(uv_async_t* handle) { |
| 79 | + EventHandler* cb = static_cast<EventHandler*>(handle->data); |
| 80 | + cb->complete(); |
| 81 | + } |
| 82 | + |
| 83 | + uv_async_t async_; |
| 84 | + v8::Isolate *isolate_; |
| 85 | + v8::Persistent<v8::Value> self_; |
| 86 | + v8::Persistent<v8::Function> callback_; |
| 87 | + v8::Persistent<v8::Context> context_; |
| 88 | + |
| 89 | + struct HandlerData { |
| 90 | + void *rust_callback; |
| 91 | + Neon_EventHandler handler; |
| 92 | + }; |
| 93 | + |
| 94 | + std::mutex mutex_; |
| 95 | + std::vector<HandlerData> handlers_; |
| 96 | + |
| 97 | + bool close_; |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +#endif |
0 commit comments