-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#197: Implement progress to NanAsyncWorker.
- Loading branch information
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/********************************************************************************** | ||
* NAN - Native Abstractions for Node.js | ||
* | ||
* Copyright (c) 2014 NAN contributors | ||
* | ||
* MIT +no-false-attribs License <https://github.com/rvagg/nan/blob/master/LICENSE> | ||
**********************************************************************************/ | ||
|
||
#ifndef _WIN32 | ||
#include <unistd.h> | ||
#define Sleep(x) usleep((x)*1000) | ||
#endif | ||
#include <nan.h> | ||
|
||
class ProgressWorker : public NanAsyncProgressWorker { | ||
public: | ||
ProgressWorker( | ||
NanCallback *callback | ||
, NanCallback *progress | ||
, int milliseconds | ||
, int iters) | ||
: NanAsyncProgressWorker(callback), progress(progress) | ||
, milliseconds(milliseconds), iters(iters) {} | ||
~ProgressWorker() {} | ||
|
||
void Execute (const NanAsyncProgressWorker::ExecutionProgress& progress) { | ||
for (int i = 0; i < iters; ++i) { | ||
progress.Send(reinterpret_cast<const char*>(&i), sizeof(int)); | ||
Sleep(milliseconds); | ||
} | ||
} | ||
|
||
void HandleProgressCallback(const char *data, size_t size) { | ||
NanScope(); | ||
|
||
v8::Local<v8::Value> argv[] = { | ||
NanNew<v8::Integer>(*reinterpret_cast<int*>(const_cast<char*>(data))) | ||
}; | ||
progress->Call(1, argv); | ||
} | ||
|
||
private: | ||
NanCallback *progress; | ||
int milliseconds; | ||
int iters; | ||
}; | ||
|
||
NAN_METHOD(DoProgress) { | ||
NanScope(); | ||
NanCallback *progress = new NanCallback(args[2].As<v8::Function>()); | ||
NanCallback *callback = new NanCallback(args[3].As<v8::Function>()); | ||
NanAsyncQueueWorker(new ProgressWorker( | ||
callback | ||
, progress | ||
, args[0]->Uint32Value() | ||
, args[1]->Uint32Value())); | ||
NanReturnUndefined(); | ||
} | ||
|
||
void Init(v8::Handle<v8::Object> exports) { | ||
exports->Set( | ||
NanNew<v8::String>("a") | ||
, NanNew<v8::FunctionTemplate>(DoProgress)->GetFunction()); | ||
} | ||
|
||
NODE_MODULE(asyncprogressworker, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const test = require('tap').test | ||
, testRoot = require('path').resolve(__dirname, '..') | ||
, bindings = require('bindings')({ module_root: testRoot, bindings: 'asyncprogressworker' }); | ||
|
||
test('asyncprogressworker', function (t) { | ||
var worker = bindings.a | ||
, progressed = 0 | ||
worker(100, 5, function(i) { | ||
t.ok(i === progressed, 'got the progress updates #' + i); | ||
progressed++; | ||
}, function () { | ||
t.ok(progressed === 5, 'got all progress updates') | ||
t.end() | ||
}) | ||
}) |