Code: https://github.com/t348575/blockchain-api-testing/tree/master
Blocks are added using genesis(). A thread watches the worker queue, and sends blocks to be computed and added to the chain. Order needs to be preserved, which is why i have a queuing system. Does the Queue() in AsyncWorker take care of this? This example works if my queuing system is removed. BlockChainWrapper
holds all the functions, and inherits from ObjectWrap
. When an item from the queue is to be executed, AsyncBlockChainWrapper
is used, which implements PromiseWorker
, which does some block chain work and returns a string to block_as_json_string
, after this the promise needs to be resolved. Running BlockchChainAPI.js
throws a V8 error, while test.js
gives no output at all. What is going on? I am relativity new to node-addon-api. Any suggestions on how to proceed?
Store Napi::Env and the object for later use (blockchainWrapper.cpp)
Napi::Value BlockChainWrapper::genesis(const Napi::CallbackInfo& info) {
Napi::Object input_obj = info[0].As<Napi::Object>();
std::lock_guard<std::mutex> guard_ready_queue(ready_queue_mutex);
this->ready_queue_data.push_back(input_obj);
this->ready_queue_func.push_back(BlockChainWrapperTypes::_genesis_ready);
this->ready_queue_env.push_back(info.Env());
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env());
return deferred.Promise();
}
Vectors (blockchainWrapper.h):
std::vector<Napi::Env> ready_queue_env;
std::vector<Napi::Object> ready_queue_data;
Every 200 milliseconds, the work queue is scanned, and AsyncFunctions (the AsyncWorker class) is called.