1.0.1 • Published 5 years ago
provider-launcher v1.0.1
provider-launcher
Usage
Suppose we have three providers A, B, C. The dependency graph as follows:
A <- B <- C
It means we should launch them in order: A, B, C, and shutdown them after use in order: C, B, A.
Let's implement all of them in typescript:
class A implements Provider {
static readonly dependencies = [];
// Do asynchronous initialization after construction.
async initialize() {}
// Finalize resources.
async finalize() {}
}
class B implements Provider {
static readonly dependencies = [A];
// An initialized instance of A will be injected.
constructor(a: A) {}
async initialize() {}
async finalize() {}
}
class C implements Provider {
// Since A is an indirect dependency of C, it can be omitted.
static readonly dependencies = [B];
// An initialized instance of B will be injected.
constructor(b: B) {}
async initialize() {}
async finalize() {}
}
Now we can launch them and aquire an instance of C:
const launcher = new ProviderLauncher(A, B, C);
await launcher.initialize();
const c = launcher.get(C);
// Do whatever you want with c.
// ......
await launcher.finalize();
Practical Example
For practical example, please check example folder. To run example, just type npm run example
.