eilvelia / tdl

Node.js bindings to TDLib 🥒
MIT License
411 stars 53 forks source link

How do you re-create a client? #164

Closed norwend closed 2 months ago

norwend commented 3 months ago

I have a code

module.exports.logOut = async function() {
    try {
        await client.invoke({
            _: 'logOut'
        });
        client = tdl.createClient({
            apiId: API_ID,
            apiHash: API_HASH,
        });
        return true;
    } catch (err) {
        console.error(err);
        return false;
    }
};

that should (in theory) destroy the client and then re-create it, but in practice the library still tells me, that my client is closed.

openError: A closed client cannot be reused, create a new client

How do you properly re-create a client?

eilvelia commented 3 months ago

The error comes from other parts of the code. The execution probably yields to something else before await is finished. One possible solution is try to reorder the calls:

        const oldClient = client
        client = tdl.createClient({
            apiId: API_ID,
            apiHash: API_HASH,
        });
        await oldClient.invoke({
            _: 'logOut'
        });