Wrap getTopics as special case for JS

This commit is contained in:
Alex Hultman 2021-04-10 23:08:50 +02:00
parent 53db43250e
commit 703311649c

View File

@ -233,6 +233,26 @@ struct WebSocketWrapper {
}
}
/* This one is wrapped instead of iterateTopics as JS-people will put their hands in wood chipper for sure. */
template <bool SSL>
static void uWS_WebSocket_getTopics(const FunctionCallbackInfo<Value> &args) {
Isolate *isolate = args.GetIsolate();
auto *ws = getWebSocket<SSL>(args);
if (ws) {
Local<Array> topicsArray = Array::New(isolate, 0);
ws->iterateTopics([&topicsArray, isolate](std::string_view topic) {
Local<String> topicString = String::NewFromUtf8(isolate, topic.data(), NewStringType::kNormal, topic.length()).ToLocalChecked();
topicsArray->Set(isolate->GetCurrentContext(), topicsArray->Length(), topicString).IsNothing();
});
/* Todo: we need to pass a copy here */
args.GetReturnValue().Set(topicsArray);
}
}
template <bool SSL>
static Local<Object> init(Isolate *isolate) {
Local<FunctionTemplate> wsTemplateLocal = FunctionTemplate::New(isolate);
@ -257,6 +277,9 @@ struct WebSocketWrapper {
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getRemoteAddressAsText", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getRemoteAddressAsText<SSL>));
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "isSubscribed", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_isSubscribed<SSL>));
/* This one does not exist in C++ */
wsTemplateLocal->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "getTopics", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_WebSocket_getTopics<SSL>));
/* Create the template */
Local<Object> wsObjectLocal = wsTemplateLocal->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();