我正在尝试使用需要本机模块支持的 Node.Js 构建应用程序。我已经通过应用程序使用了 libuv 库,并且我能够使大多数异步方法工作,除了我必须实现进度事件回调的部分。我想异步实现进度事件回调并且不阻塞 Node.js 事件循环。
以下是代码片段:
本机.cc
#include <node.h>
#include <uv.h>
#include "nbind/nbind.h"
using v8::Isolate;
using v8::HandleScope;
int FileProgressCallback(uint64_t const sent, uint64_t const total, void const *const data) {
nbind::cbFunction cb = *((nbind::cbFunction *) data);
cb(sent, total);
return 0;
}
class WorkerFileTransfer {
public:
WorkerFileTransfer(std::string path, nbind::cbFunction cb) :
callback(cb), path(path) {};
uv_work_t worker;
nbind::cbFunction callback;
bool error;
std::string errorMsg;
std::string path;
};
void FileTransferDone(uv_work_t *order, int status) {
Isolate *isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
WorkerFileTransfer *work = static_cast< WorkerFileTransfer * >( order->data );
if (work->error) {
work->callback.call<void>(work->errorMsg.c_str(), work->output);
} else {
ThirdPartyLibraryFileCopy(work->path.c_str(), FileProgressCallback, (const void *) &work->callback);
}
// Memory cleanup
work->callback.reset();
delete work;
}
void FileTransferRunner(uv_work_t *order) {
WorkerFileTransfer *work = static_cast< WorkerFileTransfer * >( order->data );
try {
work->output = true;
}
catch (...) {
work->error = true;
work->errorMsg = "Error occured while executing the method...";
}
}
void FileTransfer(const std::string path, nbind::cbFunction &callback) {
WorkerFileTransfer *work = new WorkerFileTransfer(path, callback);
work->worker.data = work;
work->path = path;
work->error = false;
uv_queue_work(uv_default_loop(), &work->worker, FileTransferRunner, FileTransferDone);
}
function(FileTransfer);
测试.js
FileTransfer(
'/path/file.txt',
(sent, total) => {
console.log(`sent`, sent);
console.log('total', total);
}
);
由于以下几行,我能够完成文件传输,但 Node.Js 事件循环在这里被阻塞。
void FileTransferDone(uv_work_t *order, int status) {
...
ThirdPartyLibraryFileCopy(work->path.c_str(), FileProgressCallback, (const void *) &work->callback);
...
}
当我将线ThirdPartyLibraryFileCopy(work->path.c_str(), FileProgressCallback, (const void *) &work->callback);
移到FileTransferRunner(uv_work_t *order)
方法中时,我在 javascript 回调函数中没有得到任何输出。
如何在不阻塞 Node.Js 事件循环的情况下异步获取 javascript 回调函数中的进度值?