https://github.com/mojotech/entwine
Immutable dependency injection in Node.js
https://github.com/mojotech/entwine
Last synced: 12 months ago
JSON representation
Immutable dependency injection in Node.js
- Host: GitHub
- URL: https://github.com/mojotech/entwine
- Owner: mojotech
- License: other
- Created: 2015-12-09T16:32:32.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-15T15:27:00.000Z (over 10 years ago)
- Last Synced: 2025-08-09T05:38:33.735Z (12 months ago)
- Language: JavaScript
- Homepage:
- Size: 13.7 KB
- Stars: 4
- Watchers: 43
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Entwine
Immutable dependency injection in Node.js inspired (very) heavily by [Component](https://github.com/stuartsierra/component).
## Installation ##
```shell
npm install --save entwine
```
## Usage ##
```javascript
let entwine = require('entwine');
//
// Database component
//
class Database extends entwine.component({config: {}, conn: null}) {
start() {
let self = this;
return connectToDatabase(this.config).then(conn => self.set('conn', conn));
}
stop() {
let self = this;
return disconnectFromDatabase(this.conn).then(() => self.remove('conn'));
}
}
function database(config) {
return new Database({config: config});
}
//
// Server component
//
class Server extends entwine.component({config: {}, socket: null, database: null}) {
start() {
let self = this;
return listenOnPort(this.config).then(socket => self.set('socket', socket));
}
stop() {
let self = this;
return unlistenOnPort(this.config).then(() => self.remove('socket'));
}
}
function server(config) {
return new Server({config: config});
}
//
// System
//
entwine.system({
server: server(serverConfig),
database: database(databaseConfig)
},{
server: ['database']
}).start().then(s => {
console.log('system started');
return s.stop();
}).then(() => {
console.log('system stopped');
});
```
## Testing ##
```shell
npm test
```
## License ##
Copyright (c) 2015 [MojoTech](https://mojotech.com)
Entwine is free software, and may be redistributed under the terms specified in the [LICENSE](LICENSE.txt) file.