Skip to content

Commit

Permalink
remove_async
Browse files Browse the repository at this point in the history
  • Loading branch information
davedoesdev committed Nov 15, 2019
1 parent 21154dd commit b7bbf25
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions src/qlobber_js_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class QlobberJSCommon :
return info.This();
}

void RemoveAsync(const Napi::CallbackInfo& info) {
(new RemoveAsyncWorker(this, info))->Queue();
}

Napi::Value Match(const Napi::CallbackInfo& info) {
const auto env = info.Env();
const auto topic = info[0].As<Napi::String>();
Expand Down Expand Up @@ -203,6 +207,7 @@ class QlobberJSCommon :
return r;
}

// For testing
Napi::Value GetShortcuts(const Napi::CallbackInfo& info) {
const auto env = info.Env();
const auto Map = env.Global().Get("Map").As<Napi::Function>();
Expand Down Expand Up @@ -260,26 +265,50 @@ class QlobberJSCommon :
push_t sink;
};

class AddAsyncWorker : public Napi::AsyncWorker {
class TopicAsyncWorker : public Napi::AsyncWorker {
public:
AddAsyncWorker(QlobberJSCommon* qlobber,
const Napi::CallbackInfo& info) :
TopicAsyncWorker(QlobberJSCommon* qlobber,
const Napi::CallbackInfo& info) :
Napi::AsyncWorker(GetCallback(info)),
qlobber(qlobber),
qlobber_ref(Napi::Persistent(qlobber->get_object())),
topic(info[0].As<Napi::String>()),
topic(info[0].As<Napi::String>()) {}

protected:
QlobberJSCommon* qlobber;
Napi::ObjectReference qlobber_ref;
std::string topic;
};

class AddAsyncWorker : public TopicAsyncWorker {
public:
AddAsyncWorker(QlobberJSCommon* qlobber,
const Napi::CallbackInfo& info) :
TopicAsyncWorker(qlobber, info),
value(qlobber->get_add_value(info)) {}

void Execute() override {
qlobber->add(topic, value);
this->qlobber->add(this->topic, value);
}

private:
QlobberJSCommon* qlobber;
Napi::ObjectReference qlobber_ref;
std::string topic;
Value value;
};

class RemoveAsyncWorker : public TopicAsyncWorker {
public:
RemoveAsyncWorker(QlobberJSCommon* qlobber,
const Napi::CallbackInfo& info) :
TopicAsyncWorker(qlobber, info),
value(qlobber->get_remove_value(info)) {}

void Execute() override {
this->qlobber->remove(this->topic, value);
}

private:
std::optional<const RemoveValue> value;
};
};

template<typename Value,
Expand Down Expand Up @@ -326,6 +355,7 @@ void Initialize(Napi::Env env, const char* name, Napi::Object exports) {
T::InstanceMethod("add", &T::Add),
T::InstanceMethod("add_async", &T::AddAsync),
T::InstanceMethod("remove", &T::Remove),
T::InstanceMethod("remove_async", &T::RemoveAsync),
T::InstanceMethod("match", &T::Match),
T::InstanceMethod("test", &T::Test),
T::InstanceMethod("clear", &T::Clear),
Expand Down

0 comments on commit b7bbf25

Please sign in to comment.