|
| 1 | +/********************************************************************* |
| 2 | + * NAN - Native Abstractions for Node.js |
| 3 | + * |
| 4 | + * Copyright (c) 2017 NAN contributors |
| 5 | + * |
| 6 | + * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md> |
| 7 | + ********************************************************************/ |
| 8 | + |
| 9 | +#ifndef _WIN32 |
| 10 | +#include <unistd.h> |
| 11 | +#define Sleep(x) usleep((x)*1000) |
| 12 | +#endif |
| 13 | +#include <nan.h> |
| 14 | + |
| 15 | +using namespace Nan; // NOLINT(build/namespaces) |
| 16 | + |
| 17 | +class ProgressQueueWorker : public AsyncProgressQueueWorker<char> { |
| 18 | + public: |
| 19 | + ProgressQueueWorker( |
| 20 | + Callback *callback |
| 21 | + , Callback *progress |
| 22 | + , int milliseconds |
| 23 | + , int iters) |
| 24 | + : AsyncProgressQueueWorker(callback), progress(progress) |
| 25 | + , milliseconds(milliseconds), iters(iters) {} |
| 26 | + |
| 27 | + ~ProgressQueueWorker() { |
| 28 | + delete progress; |
| 29 | + } |
| 30 | + |
| 31 | + void Execute (const AsyncProgressQueueWorker::ExecutionProgress& progress) { |
| 32 | + for (int i = 0; i < iters; ++i) { |
| 33 | + progress.Send(reinterpret_cast<const char*>(&i), sizeof(int)); |
| 34 | + Sleep(milliseconds); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + void HandleProgressCallback(const char *data, size_t count) { |
| 39 | + HandleScope scope; |
| 40 | + |
| 41 | + v8::Local<v8::Value> argv[] = { |
| 42 | + New<v8::Integer>(*reinterpret_cast<int*>(const_cast<char*>(data))) |
| 43 | + }; |
| 44 | + progress->Call(1, argv); |
| 45 | + } |
| 46 | + |
| 47 | + private: |
| 48 | + Callback *progress; |
| 49 | + int milliseconds; |
| 50 | + int iters; |
| 51 | +}; |
| 52 | + |
| 53 | +NAN_METHOD(DoProgress) { |
| 54 | + Callback *progress = new Callback(info[2].As<v8::Function>()); |
| 55 | + Callback *callback = new Callback(info[3].As<v8::Function>()); |
| 56 | + AsyncQueueWorker(new ProgressQueueWorker( |
| 57 | + callback |
| 58 | + , progress |
| 59 | + , To<uint32_t>(info[0]).FromJust() |
| 60 | + , To<uint32_t>(info[1]).FromJust())); |
| 61 | +} |
| 62 | + |
| 63 | +NAN_MODULE_INIT(Init) { |
| 64 | + Set(target |
| 65 | + , New<v8::String>("a").ToLocalChecked() |
| 66 | + , New<v8::FunctionTemplate>(DoProgress)->GetFunction()); |
| 67 | +} |
| 68 | + |
| 69 | +NODE_MODULE(asyncprogressqueueworker, Init) |
0 commit comments