https://github.com/re-js/provi
Minimalistic and cute Service Provider
https://github.com/re-js/provi
Last synced: 4 months ago
JSON representation
Minimalistic and cute Service Provider
- Host: GitHub
- URL: https://github.com/re-js/provi
- Owner: re-js
- License: mit
- Created: 2022-08-14T02:15:50.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-01T14:17:05.000Z (over 1 year ago)
- Last Synced: 2025-02-15T14:50:02.678Z (4 months ago)
- Language: JavaScript
- Homepage:
- Size: 198 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# provi
[](https://www.npmjs.com/package/provi)
[](https://bundlephobia.com/result?p=provi)
[](https://coveralls.io/github/re-js/provi)
[](index.d.ts)Minimalistic and cute Service Provider for server environment (node.js, bun) and for clients (browser, native).
- You can use it at any place of your application without rewriting your application's architecture or other preparations or initializations.
- Each dependency can be a class or function.
- Feel free with your shared logic.The **service provider** pattern - also called _service locator_ pattern.
Advantages over Dependency Injection:
- Zero configuration, easy to use everywhere whole your app.
- Start-time and run-time dependency resolving, solving the circular dependency problem.
- Smaller bundle size.node.js or bun usage
```javascript
import { provide } from "provi/server"class Db { /* ... */ }
// Define dependencies using "provide" function
export class App {
db = provide(Db)
// ...
start() {
this.db.init()
// ...
}
}
```browser or native usage
```javascript
import { provide } from "provi/client"class Auth {
// user = sharedUser()
// ...
logout() {
if (sharedUser().isAnonymous) return
// ...
}
}export const sharedAuth = () => provide(Auth)
```in both ways you can use plain javascript functions as dependency constructor
```javascript
import { provide } from "provi/client"const User = () => {
// ...
return {
get isAnonymous() {
return true;
}
}
}export const sharedUser = () => provide(User)
```**Isolation of async scopes** (only in node environment)
Run your app in isolated Service Provider scope. All instances cached for this will be isolated from all cached instances in other scopes. Useful for implementing SSR.
```javascript
import { isolate } from "provi/ssr"const html = await isolate(async () => {
const { run } = provide(Logic); // Isolated instance of app logic
await run();
// ...
return ReactDOMServer.renderToString();
});
```Each isolated instance will be destroyed at the end of the isolated asynchronous function.
**Installation**
```bash
npm install provi
```Enjoy your code!