Search code examples
dartdart-js-interopdart-dev-compiler

How can I interop with existing JS objects?


I'm trying to write Dart code that will generate a rough equivalent of this:

var disposable = vscode['commands'].registerCommand(
    'extension.sayHello',
    function () {
        vscode['window'].showInformationMessage('Hello World!');
    }
);
context.subscriptions.push(disposable);

The variables vscode and context are both globals available in the JavaScript which is executing my Dart (which is being compiled to JS with the Dart Dev Compiler).

I'm happy for context/vscode to be completely untyped for now, but I'm struggling to bend the JS package to output code like this (eg. if I put @JS() on a stub vscode, I get dart.global.vscode).


Solution

  • You can use the base dart:js features

    Note that context happens to be dart's name for the javascript context.

    var disposable = context['vscode']['commands'].callMethod('registerCommand', [
        'extension.sayHello',
        () {
            context['vscode']['window'].callMethod('showInformationMessage', ['Hello World!']);
        }]);
    );
    context['context']['subscriptions'].callMethod('push', [disposable]);