Skip to content

Commit

Permalink
Control the OnError and OnOK callbacks in case we are in a terminatin…
Browse files Browse the repository at this point in the history
…g thread, #200
  • Loading branch information
kriszyp committed Oct 31, 2022
1 parent a945757 commit c26e71a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/dbi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,13 @@ class PrefetchWorker : public AsyncWorker {
}

void OnOK() {
Callback().Call({Env().Null()});
napi_value result; // we use direct napi call here because node-addon-api interface with throw a fatal error if a worker thread is terminating
napi_call_function(Env(), Env().Undefined(), Callback().Value(), 0, {}, &result);
}
void OnError(const Error& e) {
napi_value result; // we use direct napi call here because node-addon-api interface with throw a fatal error if a worker thread is terminating
napi_value arg = e.Value();
napi_call_function(Env(), Env().Undefined(), Callback().Value(), 1, &arg, &result);
}

private:
Expand Down
5 changes: 5 additions & 0 deletions src/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ class SyncWorker : public AsyncWorker {
napi_value result; // we use direct napi call here because node-addon-api interface with throw a fatal error if a worker thread is terminating
napi_call_function(Env(), Env().Undefined(), Callback().Value(), 0, {}, &result);
}
void OnError(const Error& e) {
napi_value result; // we use direct napi call here because node-addon-api interface with throw a fatal error if a worker thread is terminating
napi_value arg = e.Value();
napi_call_function(Env(), Env().Undefined(), Callback().Value(), 1, &arg, &result);
}

void Execute() {
#ifdef _WIN32
Expand Down
1 change: 1 addition & 0 deletions src/lmdb-js.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class AsyncWriteWorker : public WriteWorker, public AsyncProgressWorker<char> {
void Execute(const AsyncProgressWorker::ExecutionProgress& execution);
void OnProgress(const char* data, size_t count);
void OnOK();
void OnError(const Error& e);
void ReportError(const char* error);
void SendUpdate();
private:
Expand Down
6 changes: 6 additions & 0 deletions src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ void AsyncWriteWorker::OnOK() {
napi_create_int32(Env(), 0, &arg);
napi_call_function(Env(), Env().Undefined(), Callback().Value(), 1, &arg, &result);
}
void AsyncWriteWorker::OnError(const Error& e) {
finishedProgress = true;
napi_value result; // we use direct napi call here because node-addon-api interface with throw a fatal error if a worker thread is terminating
napi_value arg = e.Value();
napi_call_function(Env(), Env().Undefined(), Callback().Value(), 1, &arg, &result);
}

Value EnvWrap::resumeWriting(const Napi::CallbackInfo& info) {
// if we had async txns, now we resume
Expand Down

0 comments on commit c26e71a

Please sign in to comment.