https://github.com/vanifatovvlad/lifetimed
Lifetimed is a JavaScript library for simplifying the management of disposable resources
https://github.com/vanifatovvlad/lifetimed
javascript library programming-paradigm react rxjs typescript
Last synced: 11 months ago
JSON representation
Lifetimed is a JavaScript library for simplifying the management of disposable resources
- Host: GitHub
- URL: https://github.com/vanifatovvlad/lifetimed
- Owner: vanifatovvlad
- License: mit
- Created: 2025-07-18T09:07:47.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-26T09:27:38.000Z (12 months ago)
- Last Synced: 2025-07-26T15:33:30.248Z (12 months ago)
- Topics: javascript, library, programming-paradigm, react, rxjs, typescript
- Language: TypeScript
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Lifetimed
Lifetimed is a JavaScript/TypeScript library for simplifying the management of disposable resources
### Lifetimed :heart: Promises
```ts
async function loadUsers(parentLifetime: Lifetime): Promise {
// Сreate a temporary child lifetime for function
const { lifetime, abort } = parentLifetime.scope()
try {
// Add listener to the cancel button.
// No need to manually unsubscribe, it will done automatically
lifetimedListenEvents(lifetime, cancelButton, 'click', () => {
// Early cancel the lifetime with all attached resources like web-request or setTimeouts
abort()
})
// Make a request.
// Request will be aborted if if takes more than a second.
// Request will be aborted also if parent lifetime will be aborted.
const response = await lifetimedFetch(lifetime.timeout(1000), '/users')
const data = await response.json()
return data
}
finally {
// Clear resources attached to the lifetime
abort()
}
}
```
### Lifetimed :heart: React
```ts
// useLifetimedEffect will recreate the lifetime when deps are changes
// and abort it when the component is unmounted.
// Automatically.
useLifetimedEffect(async lifetime => {
const response = await lifetimedFetch(lifetime, `avatars/${user}`)
const data = await response.text()
setUserAvatarUrl(data)
}, [user])
```
### Lifetimed :heart: Rx
```ts
const observable = Rx.Observable.interval(1000);
// with lifetimes you will never forget to unsubscribe from observable
observable.lifetimedSubscribe(lifetime, x => console.log(x));
```