Lifetimed is a JavaScript/TypeScript library for simplifying the management of disposable resources
async function loadUsers(parentLifetime: Lifetime): Promise<string> {
// С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()
}
}// 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])const observable = Rx.Observable.interval(1000);
// with lifetimes you will never forget to unsubscribe from observable
observable.lifetimedSubscribe(lifetime, x => console.log(x));