Skip to content

Instantly share code, notes, and snippets.

@NickNaso
Last active February 5, 2019 05:27
Show Gist options
  • Select an option

  • Save NickNaso/daccdb6d8642863816ed2fccdaa3c244 to your computer and use it in GitHub Desktop.

Select an option

Save NickNaso/daccdb6d8642863816ed2fccdaa3c244 to your computer and use it in GitHub Desktop.
#include<napi.h>
#include <chrono>
#include <thread>
#include <iostream>
Napi::Value CallEmit(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Function emit = info[0].As<Napi::Function>();
emit.Call({Napi::String::New(env, "start")});
// Here some long running task and return piece of data exectuing some task
for(int i = 0; i < 3; i++) {
std::this_thread::sleep_for(std::chrono::seconds(3));
emit.Call({Napi::String::New(env, "data"), Napi::String::New(env, "data ...")});
}
emit.Call({Napi::String::New(env, "end")});
return Napi::String::New(env, "OK");
}
// Init
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "callEmit"), Napi::Function::New(env, CallEmit));
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment