A minimal implementation of a manageable object.
What?
A manageable object is any object that can initialize and destroy itself along with its dependencies.
It does so by responding to initialize
and destroy
messages.
Why?
I often find myself writing code like this:
const myService = initialized: false { if initialized // throw already initialized. return Promiseall thisdependencies ; } { if !initialized // throw not initialized. return Promiseall thisdependencies ; };
So I decided to abstract it into a factory use it like this:
const createManageable = ; const myServiceDependencies const myService = Object; // Alternativelyconst destroyMyService = {};const initializeMyService = {}; const myService = Object;
If it sounds like over-kill to you, this real world example might show how its useful:
const mongodb = ; { const connectionUri = options; let db; const client = Object; { return mongodbMongoClient; } { return db; }} const myMongoDbClient = ; // Usage with promisesmyMongoDbClient; // Usage with generators// assuming `const coroutine = require('co')`;;