I've recently started with development in Dart. I need to run Dart on the node vm. Currently, I am required to create an interop to an external node module.
This module only exposes constructor functions. I am unable to find a way to create a Dart class that maps to a package.
Assuming the following node module export:
{
Client: [Function: Client],
Server: [Function: Server]
}
In this scenario I am trying to create a new instance of Client in my Dart workspace. I've created the following anonymous classes:
@JS()
@anonymous
abstract class ClientImpl {
// ClientImpl(String, num);
external String get host;
external num get port;
}
@JS()
@anonymous
abstract class Module {
ClientImpl Client(String, num);
}
Now I want to map the Module class to the node module
final Module _module = require('...');
void main() {
final client = _module.Client('192.168...', 1234);
}
No typing errors are shown by the analyzer and Dart compiles correctly to Javascript using dart2js. Now when I run my compiled js file in the node VM I get an exception on creating a new instance of Client. It requires the new
keyword. When I manually add the new keyword in the compiled js file I can properly instantiate the interop class.
I've also tried to use a typedef
instead, but that didnt book any success at all.
typedef ClientFunc = ClientImpl Function(String, num);
I can't find any examples online which describe my specific scenario. Is there anyone here who has had the same problem or know what I am doing wrong here?
Thanks in advance
This isn't exactly an answer my the question, but I am using a workaround for now..
const { Client, Server } = require('...')
module.exports = {
Client: (hostAddr, port) => new Client(hostAddr, port),
Server: (...args) => new Server(...args)
}
Now a normal function is exposed instead of a constructor function. However, there must be a way to solve the above without having to write a proxy for it as this one.