Home / Class/ RenderStream Class — vue Architecture

RenderStream Class — vue Architecture

Architecture documentation for the RenderStream class in render-stream.ts from the vue codebase.

Entity Profile

Relationship Graph

Source Code

packages/server-renderer/src/render-stream.ts lines 15–100

export default class RenderStream extends Readable {
  buffer: string
  render: (write: Function, done: Function) => void
  expectedSize: number
  write: Function
  //@ts-expect-error
  next: Function
  end: Function
  //@ts-expect-error
  done: boolean

  constructor(render: Function) {
    super()
    this.buffer = ''
    //@ts-expect-error
    this.render = render
    this.expectedSize = 0

    this.write = createWriteFunction(
      (text, next) => {
        const n = this.expectedSize
        this.buffer += text
        if (this.buffer.length >= n) {
          this.next = next
          this.pushBySize(n)
          return true // we will decide when to call next
        }
        return false
      },
      err => {
        this.emit('error', err)
      }
    )

    this.end = () => {
      this.emit('beforeEnd')
      // the rendering is finished; we should push out the last of the buffer.
      this.done = true
      this.push(this.buffer)
    }
  }

  pushBySize(n: number) {
    const bufferToPush = this.buffer.substring(0, n)
    this.buffer = this.buffer.substring(n)
    this.push(bufferToPush)
  }

  tryRender() {
    try {
      this.render(this.write, this.end)
    } catch (e) {
      this.emit('error', e)
    }
  }

  tryNext() {
    try {
      this.next()
    } catch (e) {
      this.emit('error', e)
    }
  }

  _read(n: number) {
    this.expectedSize = n
    // it's possible that the last chunk added bumped the buffer up to > 2 * n,
    // which means we will need to go through multiple read calls to drain it
    // down to < n.
    if (isTrue(this.done)) {
      this.push(null)
      return
    }
    if (this.buffer.length >= n) {
      this.pushBySize(n)
      return
    }
    if (isUndef(this.next)) {
      // start the rendering chain.
      this.tryRender()
    } else {
      // continue with the rendering.
      this.tryNext()
    }
  }
}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free