Created
January 4, 2017 01:10
-
-
Save akerouanton/d1994055fc4a443ec00b1926608d932e to your computer and use it in GitHub Desktop.
pure js IOC container
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import amqp from 'amqplib'; | |
module.exports = { | |
boot: async function () { | |
return await amqp.connect('amqp://guest:guest@rabbitmq:5672'); | |
}, | |
close: (conn) => conn.close() | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ResourceManager from './ResourceManager'; | |
import _ from 'lodash'; | |
function createContainer (defs) { | |
return new Proxy(new ResourceManager(_.toPairs(defs)), { | |
get (target, name, receiver) { | |
if (name === 'close') { | |
return target.close.bind(target); | |
} | |
return target.get(name); | |
} | |
}); | |
} | |
export const container = createContainer({ | |
amqp: require('./amqp'), | |
mongo: require('./mongo') | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {MongoClient} from 'mongodb'; | |
module.exports = { | |
boot: async function () { | |
return await MongoClient.connect('mongodb://mongodb/issue-api'); | |
}, | |
close: (conn) => conn.close() | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class ResourceManager { | |
constructor (defs) { | |
this._defs = new Map(defs); | |
this._booted = new Map(); | |
} | |
async get (name) { | |
if (!this._defs.has(name)) { | |
throw new Error(`There is no resource named "${name}".`); | |
} else if (!this._booted.has(name)) { | |
const def = this._defs.get(name); | |
this._booted.set(name, await def.boot()); | |
} | |
return this._booted.get(name); | |
} | |
close () { | |
this._booted.forEach((resource, name) => { | |
const def = this._defs.get(name); | |
if (def.close) { | |
def.close(resource); | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {container} from './'; | |
function createProgram () { | |
const program = require('commander'); | |
program.command('test').action(handler); | |
return program; | |
} | |
async function handler () { | |
const amqp = await container.amqp; | |
const mongo = await container.mongo; | |
console.log('yolo'); | |
container.close(); | |
return; | |
} | |
const program = createProgram(); | |
program.parse(process.argv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment