-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathSuggestWorker.cc
More file actions
45 lines (37 loc) · 1.17 KB
/
SuggestWorker.cc
File metadata and controls
45 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <napi.h>
#include <hunspell.hxx>
#include "Worker.cc"
class SuggestWorker : public Worker {
public:
SuggestWorker(
HunspellContext* context,
Napi::Promise::Deferred d,
std::string word)
: Worker(context, d), word(std::move(word)) {}
void Execute() {
// Worker thread; don't use N-API here
context->lockRead();
bool correct = context->instance->spell(word.c_str());
if (!correct) {
length = this->context->instance->suggest(&suggestions, word.c_str());
}
context->unlockRead();
}
void Resolve(Napi::Promise::Deferred const &deferred) {
Napi::Env env = deferred.Env();
if (length == -1) {
deferred.Resolve(env.Null());
return;
}
Napi::Array array = Napi::Array::New(env, length);
for (int i = 0; i < length; i++) {
array.Set(i, Napi::String::New(env, suggestions[i]));
}
context->instance->free_list(&suggestions, length);
deferred.Resolve(array);
}
private:
int length = -1;
std::string word;
char** suggestions = NULL;
};