Skip to content

Wait for Ink to finish any pending renders before process exit #796

@kevinschaich

Description

@kevinschaich

I have a custom logger class which uses Ink.

If I hit an exception anywhere in my application, the parent process immediately exits and stops Ink from going through any final render cycles that would result in logs being printed.

If I put a timeout for 10ms directly before throwing an error, Ink finishes as expected. But I don't always know where errors are going to occur.

This is the best I've come up with in my logger class:

    private setupExitHandler() {
        const flush = async () => {
            // give Ink a bit of time to get one or two additional renders in
            await new Promise(resolve => setTimeout(resolve, 100))
        }
        process.on('uncaughtException', async error => {
            await flush()
            console.error(`${error.stack} ${error.message}`)
        })

        process.on('unhandledRejection', async reason => {
            await flush()
            const error = reason instanceof Error ? reason : new Error(String(reason))
            console.error(`${error.stack} ${error.message}`)
        })
    }

Is there a canonical/better way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions