Create a Web Serial class with various events to process received data, trigger when connection established, etc. And then refactor sample code that uses web serial to use that class.