Skip to content

vanifatovvlad/lifetimed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Lifetimed

Lifetimed is a JavaScript/TypeScript library for simplifying the management of disposable resources

downloads npm version MIT license

Lifetimed ❤️ Promises

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()
    }
}

Lifetimed ❤️ React

// 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 ❤️ Rx

const observable = Rx.Observable.interval(1000);
// with lifetimes you will never forget to unsubscribe from observable
observable.lifetimedSubscribe(lifetime, x => console.log(x));

About

Lifetimed is a JavaScript library for simplifying the management of disposable resources

Topics

Resources

License

Stars

Watchers

Forks

Contributors