diff options
| author | ruki <waruqi@gmail.com> | 2018-11-08 00:38:48 +0800 |
|---|---|---|
| committer | ruki <waruqi@gmail.com> | 2018-11-07 21:53:09 +0800 |
| commit | 26105034da4fcce7ac883c899d781f016559310d (patch) | |
| tree | c459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/vue/src/server/render-stream.js | |
| parent | 2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff) | |
| download | xmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip | |
switch to vuepress
Diffstat (limited to 'node_modules/vue/src/server/render-stream.js')
| -rw-r--r-- | node_modules/vue/src/server/render-stream.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/node_modules/vue/src/server/render-stream.js b/node_modules/vue/src/server/render-stream.js new file mode 100644 index 00000000..d76012af --- /dev/null +++ b/node_modules/vue/src/server/render-stream.js @@ -0,0 +1,94 @@ +/* @flow */ + +/** + * Original RenderStream implementation by Sasha Aickin (@aickin) + * Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Modified by Evan You (@yyx990803) + */ + +const stream = require('stream') + +import { isTrue, isUndef } from 'shared/util' +import { createWriteFunction } from './write' + +export default class RenderStream extends stream.Readable { + buffer: string; + render: (write: Function, done: Function) => void; + expectedSize: number; + write: Function; + next: Function; + end: Function; + done: boolean; + + constructor (render: Function) { + super() + this.buffer = '' + 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 = () => { + // 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() + } + } +} |
