Skip to content

Instantly share code, notes, and snippets.

@radzserg
Created July 10, 2019 11:54
Show Gist options
  • Select an option

  • Save radzserg/6023eb8a74b80a064400ee11ed988339 to your computer and use it in GitHub Desktop.

Select an option

Save radzserg/6023eb8a74b80a064400ee11ed988339 to your computer and use it in GitHub Desktop.
import DIContainer, { object, get, value, factory, IDIContainer } from "rsdi";
const config = {
"ENV": value("PRODUCTION"), // define raw value
"Storage": object(CookieStorage), // will instantiate an object of class CookieStorage without arguments
"AuthStorage": object(AuthStorage).construct(
get("Storage") // refer to dependency described above
),
"BrowserHistory": factory(configureHistory), // factory callback
};
const container = new DIContainer();
container.addDefinitions(config);
const env = container.get<string>("ENV"); // PRODUCTION
const authStorage = container.get<AuthStorage>("AuthStorage"); // object of AuthStorage
const history = container.get<History>("BrowserHistory"); // object of History
function configureHistory(container: IDIContainer): History {
const history = createBrowserHistory();
const env = container.get("ENV"); // we have container and can get what we need.
if (env === "production") {
// do what you need
}
return history;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment