Creating A Service

What is a service provider

A service provides capabilities mostly for plugins to use. Services are instantiated once by the framework and that same instance shared across all plugins. Services are accessed via interfaces to allow them to be switched out for different implementations.

Example service providers

Service structure

WARNING: Services are capable of using other services, however, other services should NOT be used during initialisation else it will create a dependency on the order in which the services are initialised. They can however safely be stored within the object for later use.

Here is a template for a new service. Replace {SERVICENAME} with the name of your service. Then save the file within /src/libs/services/ as all lowercase.

import {iConfigService, iService, Services} from "../libs/services"

/**
* Allows you to provide multiple implementations of your service. This should be used when getting an instance of this service.
*/
export interface i{SERVICENAME} {
    
}

/**
* You don't have to export this, you can make it abstract but make sure you export one or more of it sub classes
*/
export class {SERVICENAME} implements iService, i{SERVICENAME} {

    /**
     * Initialise using provided config
     * @param services 
     * @param config
     */
    initialise(services:Services, config:i{SERVICENAME}Config) {
        
    }

    /**
     * Called when all plugins have initialised
     */
    pluginsInitialised() {

    }

    /**
     * Called when all plugins have generated their respective pages
     */
    pluginsGenerated() {

    }

}

/**
* Defines the configuration (if any) that can be configured within `/config.ts`
*/
export interface i{SERVICENAME}Config extends iConfigService {

}

Registering & configuring the service

Open /config.ts.

Locate the services sections. Within you will see two more sections beforePluginsInitialised and afterPluginsInitialised. You need to register your plugin and it’s configuration in the relevant section.

A service config looks like this.

"../services/globalmodel": { 
    
    // The name used to get an instance of the service. DO NOT change this else any 3rd party dependencies will break. 
    name: "{SERVICENAME}", 
    
    // The class to create an instance of (you can switch this out since the service should be developed against the interface defined within the file)
    class: {CLASSNAME}.name,
    
    // The config interface used to help define the correct format of the config followed by the actual configuration of the service
    config: <i{CLASSNAME}Config>{}

},

An example of a completed service registered within the beforePluginsInitialised section.

services: {
    beforePluginsInitialised: {
        "../services/globalmodel": { 
            name: "globalmodel", 
            class: GlobalModel.name,
            config: <iGlobalModelConfig>{}
        },
    },
    afterPluginsInitialised: {
        
    },
}

If installed (and classes/interfaces imported) correctly, you should be able to call the service within plugin methods and service methods like: services.get("{NAME}").{METHOD}()