Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 926 Bytes

on-activation.md

File metadata and controls

37 lines (27 loc) · 926 Bytes

Calling function on service activation

Simple feature that allows you to get some extra code ran when service gets created. You need to register a hook that is a function (async or sync) and returns service instance to use.

Hook are called one-by-one and. If a hook is asynchronous then alpha-dic awaits for promise to be resolved then continues calling other hooks.

// via decorator

import {OnActivation, Service} from 'alpha-dic';

@OnActivation(function(service: Foo) {
    console.log('I got called!');
    return service; // this is super important!
})
@Service()
class Foo {
    
}
// manually via annotation
import {onActivation, activationMiddleware} from 'alpha-dic';

// add annotation to service
container.definitionWithConstructor('service', Foo)
.annotate(
    onActivation((service) => {
        assert.ok(service instanceof Foo);
        return service;
    })
);