From 03fd4da21d07952c209e4ca3f809e9f08b84f01a Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 4 Oct 2018 16:44:23 -0400 Subject: [PATCH] refactor: revert render fn signature --- packages/core/src/component.ts | 13 ++++++++----- packages/core/src/componentOptions.ts | 14 +------------- packages/core/src/componentUtils.ts | 7 ++++++- packages/core/src/createRenderer.ts | 15 ++++----------- packages/core/src/optional/asyncComponent.ts | 2 +- packages/core/src/optional/context.ts | 4 ++-- packages/core/src/optional/keepAlive.ts | 2 +- 7 files changed, 23 insertions(+), 34 deletions(-) diff --git a/packages/core/src/component.ts b/packages/core/src/component.ts index b010e836..b6500bf7 100644 --- a/packages/core/src/component.ts +++ b/packages/core/src/component.ts @@ -1,13 +1,10 @@ import { EMPTY_OBJ } from './utils' -import { createElement } from './h' import { VNode, Slots, RenderNode, MountedVNode } from './vdom' import { Data, - RenderFunction, ComponentOptions, ComponentPropsOptions, - WatchOptions, - RenderContext + WatchOptions } from './componentOptions' import { setupWatcher } from './componentWatch' import { Autorun, DebuggerEvent, ComputedGetter } from '@vue/observer' @@ -16,6 +13,12 @@ import { ErrorTypes } from './errorHandling' type Flatten = { [K in keyof T]: T[K] } +export type RenderFunction

= ( + props: P, + slots: Slots, + attrs: Data +) => any + export interface ComponentClass extends Flatten { new (): D & P & MountedComponent } @@ -42,7 +45,7 @@ export interface MountedComponent $children: MountedComponent[] $options: ComponentOptions - render(h: createElement, ctx: RenderContext

): any + render(props: P, slots: Slots, attrs: Data): any renderError?(e: Error): any renderTracked?(e: DebuggerEvent): void renderTriggered?(e: DebuggerEvent): void diff --git a/packages/core/src/componentOptions.ts b/packages/core/src/componentOptions.ts index 800ce63b..5723d10e 100644 --- a/packages/core/src/componentOptions.ts +++ b/packages/core/src/componentOptions.ts @@ -1,19 +1,7 @@ -import { createElement } from './h' -import { Slots } from './vdom' -import { MountedComponent } from './component' +import { MountedComponent, RenderFunction } from './component' export type Data = Record -export interface RenderContext

{ - props: P - slots: Slots - attrs: Data -} - -export interface RenderFunction

{ - (h: createElement, ctx: RenderContext

): any -} - export interface ComponentOptions { data?: () => Partial props?: ComponentPropsOptions

diff --git a/packages/core/src/componentUtils.ts b/packages/core/src/componentUtils.ts index 2c747a7f..44346970 100644 --- a/packages/core/src/componentUtils.ts +++ b/packages/core/src/componentUtils.ts @@ -179,7 +179,12 @@ export function createComponentClassFromOptions( for (const key in options) { const value = options[key] if (typeof value === 'function') { - ;(ObjectComponent.prototype as any)[key] = value + ;(ObjectComponent.prototype as any)[key] = + key === 'render' + ? function() { + return value.call(this, h) + } + : value } if (key === 'computed') { const isGet = typeof value === 'function' diff --git a/packages/core/src/createRenderer.ts b/packages/core/src/createRenderer.ts index c42a217a..ee3dd6ee 100644 --- a/packages/core/src/createRenderer.ts +++ b/packages/core/src/createRenderer.ts @@ -1,4 +1,3 @@ -import { h } from './h' import { autorun, stop } from '@vue/observer' import { queueJob } from '@vue/scheduler' import { VNodeFlags, ChildrenFlags } from './flags' @@ -238,11 +237,7 @@ export function createRenderer(options: RendererOptions) { const render = tag as FunctionalComponent const { props, attrs } = resolveProps(data, render.props) const subTree = (vnode.children = normalizeComponentRoot( - render(h, { - props, - slots: slots || EMPTY_OBJ, - attrs: attrs || EMPTY_OBJ - }), + render(props, slots || EMPTY_OBJ, attrs || EMPTY_OBJ), vnode, attrs, render.inheritAttrs @@ -530,11 +525,7 @@ export function createRenderer(options: RendererOptions) { if (shouldUpdate) { const { props, attrs } = resolveProps(nextData, render.props) const nextTree = (nextVNode.children = normalizeComponentRoot( - render(h, { - props, - slots: nextSlots || EMPTY_OBJ, - attrs: attrs || EMPTY_OBJ - }), + render(props, nextSlots || EMPTY_OBJ, attrs || EMPTY_OBJ), nextVNode, attrs, render.inheritAttrs @@ -1200,10 +1191,12 @@ export function createRenderer(options: RendererOptions) { instance.$vnode = renderInstanceRoot(instance) as MountedVNode mount(instance.$vnode, container, instance, isSVG, endNode) parentVNode.el = instance.$vnode.el + if (__DEV__) { // expose __vue__ for devtools ;(parentVNode.el as any).__vue__ = instance } + instance._mounted = true mountComponentInstanceCallbacks(instance, parentVNode.ref) } diff --git a/packages/core/src/optional/asyncComponent.ts b/packages/core/src/optional/asyncComponent.ts index 1267fa3f..752035a3 100644 --- a/packages/core/src/optional/asyncComponent.ts +++ b/packages/core/src/optional/asyncComponent.ts @@ -77,7 +77,7 @@ export function createAsyncComponent( } } - render(_: any, { props, slots }: { props: any; slots: Slots }) { + render(props: any, slots: Slots) { if (this.err || (this.timedOut && !this.comp)) { const error = this.err || new Error(`Async component timed out after ${timeout}ms.`) diff --git a/packages/core/src/optional/context.ts b/packages/core/src/optional/context.ts index 59793040..0906d9d3 100644 --- a/packages/core/src/optional/context.ts +++ b/packages/core/src/optional/context.ts @@ -31,7 +31,7 @@ export class Provide extends Component { beforeUpdate() { this.updateValue() } - render(_: any, { slots }: { slots: Slots }) { + render(props: any, slots: Slots) { return slots.default && slots.default() } } @@ -49,7 +49,7 @@ Provide.options = { } export class Inject extends Component { - render(_: any, { props, slots }: { props: any; slots: Slots }) { + render(props: any, slots: Slots) { return slots.default && slots.default(contextStore[props.id]) } } diff --git a/packages/core/src/optional/keepAlive.ts b/packages/core/src/optional/keepAlive.ts index 10eef597..00d1aa18 100644 --- a/packages/core/src/optional/keepAlive.ts +++ b/packages/core/src/optional/keepAlive.ts @@ -49,7 +49,7 @@ export class KeepAlive extends Component<{}, KeepAliveProps> { this.keys.delete(key) } - render(_: any, { props, slots }: { props: KeepAliveProps; slots: Slots }) { + render(props: KeepAliveProps, slots: Slots) { if (!slots.default) { return }