Skip to content

Instantly share code, notes, and snippets.

@nfroidure
Last active January 26, 2019 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfroidure/d9fb9d2d40f0614ca9f31a75a3c5201d to your computer and use it in GitHub Desktop.
Save nfroidure/d9fb9d2d40f0614ca9f31a75a3c5201d to your computer and use it in GitHub Desktop.
Sample usage of Knifecycle
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