@Override
    public void data(Data data) throws IOException {
        if (data instanceof EventStream.ErrorEvent errorEvent) {
            renderError(errorEvent.asError());
        } else if (data instanceof ErrorHit errorHit) {
            for (var error: errorHit.errors()) {
                renderError(error);
            }
        } else if (data instanceof EventStream.Event event) {
            if (RENDER_EVENT_HEADER) {
                generator.writeRaw("event: " + event.type() + "\n");
            }
            generator.writeRaw("data: ");
            if (event.type().equals("hits")) {
                generator.writeRaw(event.toString());
            } else {
                generator.writeStartObject();
                generator.writeStringField(event.type(), event.toString());
                generator.writeEndObject();
            }
            generator.writeRaw("\n\n");
            generator.flush();
        }
    }
A Server-Sent Events (SSE) renderer for asynchronous events such as
tokens from a language model.

@author lesters
container-search/src/main/java/com/yahoo/search/rendering/EventRenderer.java