-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(async-worker): introduce a
KerberosWorker
using lambdas
This allows us to signifcantly reduce the amount of boilerplate required for async workers, by replacing the implementation with a single lambda. This should also make it much easier to split up implementation from definition in order to support both unix and windows implementations.
- Loading branch information
Showing
3 changed files
with
68 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef ASYNC_WORKER_H | ||
#define ASYNC_WORKER_H | ||
|
||
#include <functional> | ||
#include <nan.h> | ||
|
||
class KerberosWorker : public Nan::AsyncWorker { | ||
public: | ||
typedef std::function<void(KerberosWorker*)> OnFinishedHandler; | ||
typedef std::function<void(OnFinishedHandler)> SetOnFinishedHandler; | ||
typedef std::function<void(SetOnFinishedHandler)> ExecuteHandler; | ||
|
||
explicit KerberosWorker(Nan::Callback *callback, const char* resource_name, ExecuteHandler handler) | ||
: Nan::AsyncWorker(callback, resource_name), execute_handler(handler) {} | ||
|
||
template <class... T> | ||
void Call(T... t) { | ||
callback->Call(t..., async_resource); | ||
} | ||
|
||
virtual void Execute() { | ||
execute_handler([=] (OnFinishedHandler handler) { | ||
on_finished_handler = handler; | ||
}); | ||
} | ||
|
||
static void Run(Nan::Callback *callback, const char* resource_name, ExecuteHandler handler) { | ||
Nan::TryCatch tryCatch; | ||
if (tryCatch.HasCaught()) { | ||
tryCatch.ReThrow(); | ||
return; // don't proceed in case there were any previous errors | ||
} | ||
|
||
auto worker = new KerberosWorker(callback, resource_name, handler); | ||
Nan::AsyncQueueWorker(worker); | ||
} | ||
|
||
protected: | ||
void HandleOKCallback() { | ||
on_finished_handler(this); | ||
} | ||
|
||
private: | ||
ExecuteHandler execute_handler; | ||
OnFinishedHandler on_finished_handler; | ||
}; | ||
|
||
#endif // ASYNC_WORKER_H |