@zhaoworks/context
v1.0.1
Published
simple resource management using contexts.
Downloads
6
Readme
— @zhaoworks/context
Isolated contexts to manage resources across a codebase and nothing more.
Installation
λ bun add @zhaoworks/contextUsage
import { Context } from '@zhaoworks/context';
const database = new Database();
const ApplicationContext = Context.create({
database: database,
users: new UserService(database)
});
// use: wraps a function so you have typed context from an callback function
const FetchUser = ApplicationContext.use(({ users }) => {
const user = await users.fetch('123');
console.log('User 123 is', user.name);
});
// apply: the `use` function can be applied later
ApplicationContext.apply(FetchUser, {
users: new FakeUserService() // overrides: you can override resources in apply method.
});
// fetch: gets a resource from an context
ApplicationContext.fetch('database').connect();