-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
utils.ts
46 lines (43 loc) · 1.27 KB
/
utils.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
import { h } from 'vue'
import type { Component } from 'vue'
// eslint-disable-next-line
import { isString, isPromise, isArray } from '@vue/shared'
/**
* Internal utility
*
* @private
*/
export const _wrapIf = (component: Component, props: any, slots: any) => {
props = props === true ? {} : props
return { default: () => props ? h(component, props, slots) : slots.default?.() }
}
// eslint-disable-next-line no-use-before-define
export type SSRBuffer = SSRBufferItem[] & { hasAsync?: boolean }
export type SSRBufferItem = string | SSRBuffer | Promise<SSRBuffer>
/**
* create buffer retrieved from @vue/server-renderer
*
* @see https://github.com/vuejs/core/blob/9617dd4b2abc07a5dc40de6e5b759e851b4d0da1/packages/server-renderer/src/render.ts#L57
* @private
*/
export function createBuffer () {
let appendable = false
const buffer: SSRBuffer = []
return {
getBuffer (): SSRBuffer {
return buffer
},
push (item: SSRBufferItem) {
const isStringItem = isString(item)
if (appendable && isStringItem) {
buffer[buffer.length - 1] += item as string
} else {
buffer.push(item)
}
appendable = isStringItem
if (isPromise(item) || (isArray(item) && item.hasAsync)) {
buffer.hasAsync = true
}
}
}
}