feat: make vue and basic server renderer compatible in pure js runtimes

This commit is contained in:
Evan You 2017-10-03 17:54:37 -04:00
parent 093673e308
commit c5d0fa0503
3 changed files with 24 additions and 5 deletions

View File

@ -44,7 +44,7 @@ Vue.prototype.$mount = function (
// devtools global hook
/* istanbul ignore next */
setTimeout(() => {
Vue.nextTick(() => {
if (config.devtools) {
if (devtools) {
devtools.emit('init', Vue)

View File

@ -58,9 +58,11 @@ if (hasTransition) {
}
// binding to window is necessary to make hot reload work in IE in strict mode
const raf = inBrowser && window.requestAnimationFrame
? window.requestAnimationFrame.bind(window)
: setTimeout
const raf = inBrowser
? window.requestAnimationFrame
? window.requestAnimationFrame.bind(window)
: setTimeout
: /* istanbul ignore next */ fn => fn()
export function nextFrame (fn: Function) {
raf(() => {

View File

@ -1,6 +1,23 @@
/* @flow */
const MAX_STACK_DEPTH = 1000
const noop = _ => _
const defer = typeof process !== 'undefined' && process.nextTick
? process.nextTick
: typeof Promise !== 'undefined'
? fn => Promise.resolve().then(fn)
: typeof setTimeout !== 'undefined'
? setTimeout
: noop
if (defer === noop) {
throw new Error(
'Your JavaScript runtime does not support any asynchronous primitives ' +
'that are required by vue-server-renderer. Please use a polyfill for ' +
'either Promise or setTimeout.'
)
}
export function createWriteFunction (
write: (text: string, next: Function) => boolean,
@ -14,7 +31,7 @@ export function createWriteFunction (
const waitForNext = write(text, next)
if (waitForNext !== true) {
if (stackDepth >= MAX_STACK_DEPTH) {
process.nextTick(() => {
defer(() => {
try { next() } catch (e) {
onError(e)
}