Skip to the content.

jFactory > Reference > Trait > TraitService > Service Phases

Service Phases

Changing the Service State creates an awaitable internal process called a “Phase”.

Phase changes of the same group (Init or Remove) are queued, but starting an opposite Phase aborts the current one.

Phases are controlled by the Service State Switchers.

await myComponent.$enable(); // wait until PHASE_ENABLE is completed

Remove Phase

All subscriptions to the $ registry (listeners, promises, timers, fetch, dom…) are automatically stopped and/or removed during the Remove Phase, according to this pattern:

There is no other case because you are not allowed to call most of the methods of jFactory if the component is not enabled.

Phase process and resolving

When a new Phase starts, it runs this pattern:

1) It synchronously stops and removes the Tasks registered in myComponent.$.tasks, only if their Remove Phase match the new Phase. 1) If declared, it runs and awaits the corresponding Service State Handler. 1) It awaits any registered Tasks. 1) The Phase is resolved.

Consequence: A Phase only ends after all the Tasks and their subpromises created by the current Service State Handler are resolved:

let component = jFactory("myComponent", {
    onInstall() { // service state handler called at PHASE_INSTALL 
        this.$cssFetch("css", "stylesheet.css")
            .then(() => 
                this.$timeout("wait", 10000)
                    .then(()=>this.$log("ok"))
            )
    }
});

await component.$install();
// => await until the whole promise chain is resolved, including the timer 

See also: