Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useChannel hook can throw exception in the effect if connection is closed #1938

Open
ttypic opened this issue Dec 23, 2024 · 0 comments
Open
Labels
bug Something isn't working. It's clear that this does need to be fixed.

Comments

@ttypic
Copy link
Collaborator

ttypic commented Dec 23, 2024

useChannel hook implicitly attaches when we provide channel listener. If connection is in the terminal state we throw unhandled exception in that case.

To reproduce problem we can use this code snippet:

function App() {
    const [client, setClient] = useState(
        () => new Ably.Realtime({
            key: <KEY>,
        })
    );
    const [count, setCount] = useState(0);

    useEffect(() => {
        setClient((prevClient) => {
            prevClient.close();
            return  new Ably.Realtime({key: <KEY>});
        });
    }, [count]);

    return (
        <AblyProvider client={client}>
            <ChannelProvider channelName="get-started">
                <div>
                    <button onClick={() => setCount(count + 1)}>
                        force client reload {count}
                    </button>
                </div>
                <br />
                <AblyPubSub />
            </ChannelProvider>
        </AblyProvider>
    );
}

function AblyPubSub() {
    const [messages, setMessages] = useState([]);
    const { channel } = useChannel("get-started", "first", (message) => {
        setMessages((previousMessages) => [...previousMessages, message]);
    });
    return (
        <div>
            <button
                onClick={() => {
                    channel.publish("first", "Here is my first message!");
                }}
            >
                Publish
            </button>
            {messages.map((message) => {
                return <p key={message.id}>{message.data}</p>;
            })}
        </div>
    );
}

┆Issue is synchronized with this Jira Task by Unito

@ttypic ttypic added the bug Something isn't working. It's clear that this does need to be fixed. label Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. It's clear that this does need to be fixed.
Development

No branches or pull requests

1 participant