Last active
January 26, 2019 11:13
-
-
Save nfroidure/d9fb9d2d40f0614ca9f31a75a3c5201d to your computer and use it in GitHub Desktop.
Sample usage of Knifecycle
This file contains 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 { Knifecycle, constant, autoService, autoProvider } from 'knifecycle'; | |
// Initialize the injector | |
const $ = new Knifecycle(); | |
// Declare constants | |
$.register(constant('FTP_CONFIG', { user: 'test', password: 'test' })); | |
$.register(constant('DB_CONFIG', { user: 'test', password: 'test' })); | |
// Declare services | |
$.register(autoService(async function initFileServer({ FTP_CONFIG }) { | |
let fileServer; | |
// Actual initialization code | |
return fileServer; | |
})); | |
// Declare providers | |
$.register(autoProvider(async function initDatabase({ DB_CONFIG }) { | |
let database; | |
// Returning a structure describing the provider service and disposal | |
return { | |
service: database, | |
dispose: async () => { | |
// Do whatever needed to shut the db down properly here | |
}, | |
}; | |
})); | |
// Initialize root dependencies | |
$.run(['fileServer', 'database', '$shutdown']) | |
.then(({ fileServer, database, $shutdown }) => { | |
// Do the actual program work here | |
// Shutdown properly the process (will call dispose on every providers in order) | |
// Useful for graceful stop | |
return $shutdown(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment