Register a custom service

You can use the services extension to register new JavaScript services for the Stingray editor.

These services are instantiated once, and are available as "web workers" throughout the whole Stingray session. You can then invoke the APIs of your custom JavaScript services from elsewhere in your plug-in -- or even from other plug-ins! -- exactly the same way you call functions in the JavaScript services that are built in to the Stingray editor. (See also Use built-in editor services for more on how to use services.)

For example, you could call your custom service's API functions from a custom menu action, or from a JavaScript module associated with a custom panel. This can be useful if you have multiple panels that need to share some state information, since a single instance of the service is shared by all panels that require it.

Configuration

Each service extension requires the following configuration parameters.

extensions = {
    services = [
        {
            name = "my-custom-service"
            module = "my-module-file.js"
            api = [
                "get_greeting"
                "print_message"
            ]
        }
    ]
}

name

The name of your service module, as it should appear in require and define calls.

module

A JavaScript module that contains the API functions listed by the api key. The path is relative to the location of your .stingray_plugin file.

api

This array should list the names of all functions that are exposed by your service's module file. When the editor creates and registers the new service, it exposes each of these functions through an asynchronous API on the new service.

Example

The following JavaScript block, if saved into a file named my-module-file.js, implements the API described in the configuration above.

Note that the names of the functions that are exposed on the module below match the names defined in the api key in the definition of the service extension above.

define([], function () {
    "use strict";

    function MyService() {

        this.get_greeting = function () {
            return "Hello world!";
        };

        this.alert_message = function (message) {
            alert(message);
        };

    };

    return new MyService();
});

Once your service is registered and its API functions declared, you can access those functions by requiring your new service inside another JavaScript module or action. For example, the following snippet configures a new menu item, whose triggered action calls out to the API of the custom service.

extensions = {
    menus = [
        {
            path = "Window/Print info"
            action = {
                type = "js"
                script = """
                    require(['services/my-custom-service'], function (myCustomService) {
                        myCustomService.get_greeting().then(function(greeting) { console.warn(greeting); }).
                            then( function() { myCustomService.print_message("it's a fine day.") } )
                    });
                """
            }
        }
    ]
}

Things to note: