summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/streaming/html_stream.js
blob: 8182f69a60783dba7de0ff7d3f47ea211706bc7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { ChunkWriter } from '~/streaming/chunk_writer';

export class HtmlStream {
  constructor(element) {
    const streamDocument = document.implementation.createHTMLDocument('stream');

    streamDocument.open();
    streamDocument.write('<streaming-element>');

    const virtualStreamingElement = streamDocument.querySelector('streaming-element');
    element.appendChild(document.adoptNode(virtualStreamingElement));

    this.streamDocument = streamDocument;
  }

  withChunkWriter(config) {
    return new ChunkWriter(this, config);
  }

  write(chunk) {
    // eslint-disable-next-line no-unsanitized/method
    this.streamDocument.write(chunk);
  }

  close() {
    this.streamDocument.write('</streaming-element>');
    this.streamDocument.close();
  }

  abort() {
    this.streamDocument.close();
  }
}