Skip to content

Commit

Permalink
annotate Vue class
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 12, 2016
1 parent 182a45e commit 354ea61
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 106 deletions.
1 change: 0 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
[include]

[libs]
flow

[options]
module.name_mapper='^compiler/\(.*\)$' -> '<PROJECT_ROOT>/src/compiler/\1'
Expand Down
10 changes: 0 additions & 10 deletions flow/declarations.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/compiler/directives/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export function ref (el, dir) {
}
// __registerRef__(name, ref, vFor?, remove?)
const code = `__registerRef__("${dir.arg}", n1.child || n1.elm, ${isFor ? 'true' : 'false'}`
addHook(el, 'insert', `${code})`)
addHook(el, 'insert', `${code}), false`)
addHook(el, 'destroy', `${code}, true)`)
}
26 changes: 14 additions & 12 deletions src/core/config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/* @flow */

const config: {
preserveWhitespace: boolean,
silent: boolean,
isReservedTag: (x: string) => boolean,
isUnknownElement: (x: string) => boolean,
_assetTypes: Array<string>,
_lifecycleHooks: Array<string>,
_maxUpdateCount: number,
_isServer: boolean
} = {
export type Config = {
preserveWhitespace: boolean,
silent: boolean,
isReservedTag: (x: string) => boolean,
isUnknownElement: (x: string) => boolean,
_assetTypes: Array<string>,
_lifecycleHooks: Array<string>,
_maxUpdateCount: number,
_isServer: boolean
}

const config: Config = {

/**
* Preserve whitespaces between elements.
Expand All @@ -25,13 +27,13 @@ const config: {
* Check if a tag is reserved so that it cannot be registered as a
* component. This is platform-dependent and may be overwritten.
*/
isReservedTag: () => false,
isReservedTag: _ => false,

/**
* Check if a tag is an unknown element.
* Platform-dependent.
*/
isUnknownElement: () => false,
isUnknownElement: _ => false,

/**
* List of asset types that a component can own.
Expand Down
7 changes: 7 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import Vue from './instance/index'
import config from './config'
import { initGlobalAPI } from './global-api/index'

initGlobalAPI(Vue)

// defining $isServer flag here because flow cannot handle
// Object.defineProperty getters
Object.defineProperty(Vue.prototype, '$isServer', {
get: () => config._isServer
})

Vue.version = '2.0.0-alpha.0'

export default Vue
38 changes: 11 additions & 27 deletions src/core/instance/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* @flow */

import type Vue from './index'
import { toArray } from '../util/index'
import { updateListeners } from '../vdom/helpers'

export function initEvents (vm) {
export function initEvents (vm: Vue) {
vm._events = Object.create(null)
// init parent attached events
const listeners = vm.$options._parentListeners
Expand All @@ -12,21 +15,13 @@ export function initEvents (vm) {
}
}

export function eventsMixin (Vue) {
Vue.prototype.$on = function (event, fn) {
(this._events[event] || (this._events[event] = []))
.push(fn)
export function eventsMixin (Vue: Class<Vue>) {
Vue.prototype.$on = function (event: string, fn: Function): Vue {
(this._events[event] || (this._events[event] = [])).push(fn)
return this
}

/**
* Adds an `event` listener that will be invoked a single
* time then automatically removed.
*
* @param {String} event
* @param {Function} fn
*/
Vue.prototype.$once = function (event, fn) {
Vue.prototype.$once = function (event: string, fn: Function): Vue {
const self = this
function on () {
self.$off(event, on)
Expand All @@ -37,14 +32,7 @@ export function eventsMixin (Vue) {
return this
}

/**
* Remove the given callback for `event` or all
* registered callbacks.
*
* @param {String} event
* @param {Function} fn
*/
Vue.prototype.$off = function (event, fn) {
Vue.prototype.$off = function (event?: string, fn?: Function): Vue {
// all
if (!arguments.length) {
this._events = Object.create(null)
Expand Down Expand Up @@ -72,12 +60,7 @@ export function eventsMixin (Vue) {
return this
}

/**
* Trigger an event on self.
*
* @param {String} event
*/
Vue.prototype.$emit = function (event) {
Vue.prototype.$emit = function (event: string): Vue {
let cbs = this._events[event]
if (cbs) {
cbs = cbs.length > 1 ? toArray(cbs) : cbs
Expand All @@ -86,5 +69,6 @@ export function eventsMixin (Vue) {
cbs[i].apply(this, args)
}
}
return this
}
}
98 changes: 96 additions & 2 deletions src/core/instance/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/* @flow */

import type { Config } from '../config'
import type VNode from '../vdom/vnode'
import type Watcher from '../observer/watcher'

import { initProxy } from './proxy'
import { initState, stateMixin } from './state'
import { initRender, renderMixin } from './render'
Expand All @@ -8,11 +14,99 @@ import { mergeOptions } from '../util/index'
let uid = 0

export default class Vue {
constructor (options) {
// static properties
static cid: number;
static options: Object;
static config: Config;
static util: Object;

// static methods
static set: (obj: Object, key: string, value: any) => void;
static delete: (obj: Object, key: string) => void;
static nextTick: (fn: Function, context?: Object) => void;
static use: (plugin: Function | Object) => void;
static mixin: (mixin: Object) => void;
static extend: (options: Object) => Class<Vue>;
static compile: (template: string) => { render: Function, staticRenderFns: Array<Function> };

// assets
static directive: (id: string, def?: Function | Object) => Function | Object | void;
static component: (id: string, def?: Class<Vue> | Object) => Class<Vue>;
static transition: (id: string, def?: Object) => Object | void;
static filter: (id: string, def?: Function) => Function | void;

// public properties
$el: Element | void;
$data: Object;
$options: Object;
$parent: Vue | void;
$root: Vue;
$children: Array<Vue>;
$refs: { [key: string]: Vue | Element };
$slots: { [key: string]: Array<VNode> };
$isServer: boolean;

// public methods
$mount: (el?: Element | string) => Vue;
$forceUpdate: () => void;
$destroy: () => void;
$watch: (expOrFn: string | Function, cb: Function, options?: Object) => Function;
$on: (event: string, fn: Function) => Vue;
$once: (event: string, fn: Function) => Vue;
$off: (event?: string, fn?: Function) => Vue;
$emit: (event: string, ...args: Array<any>) => Vue;
$nextTick: (fn: Function) => void;
$createElement: (
tag?: string | Vue,
data?: Object,
children?: Array<?VNode> | string,
namespace?: string
) => VNode;

// private properties
_uid: number;
_isVue: true;
_renderProxy: Vue;
_watchers: Array<Watcher>;
_data: Object;
_events: { [key: string]: Array<Function> };
_isMounted: boolean;
_isDestroyed: boolean;
_isBeingDestroyed: boolean;
_vnode: ?VNode;
_staticTrees: ?Array<VNode>;

// private methods
// lifecycle
_mount: () => Vue;
_update: (vnode: VNode) => void;
_updateFromParent: (propsData?: Object, listeners?: Object, parentVnode: VNode, renderChildren: Array<VNode> | () => Array<VNode>) => void;
// rendering
_render: () => VNode;
__h__: (
tag?: string | Vue,
data?: Object,
children?: Array<?VNode> | string,
namespace?: string
) => VNode;
__toString__: (value: any) => string;
__resolveFilter__: (id: string) => Function;
__renderList__: (
val: any,
render: Function
) => ?Array<VNode>;
__registerRef__: (
key: string,
ref: Vue | Element,
vFor: boolean,
isRemoval: boolean
) => void;

constructor (options?: Object) {
this._init(options)
}

_init (options) {
_init (options?: Object) {
// a uid
this._uid = uid++
// a flag to avoid this being observed
Expand Down
29 changes: 19 additions & 10 deletions src/core/instance/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/* @flow */

import type Vue from './index'
import type VNode from '../vdom/vnode'
import Watcher from '../observer/watcher'
import { warn, validateProp, remove } from '../util/index'
import { observerState } from '../observer/index'
import { updateListeners } from '../vdom/helpers'

export function initLifecycle (vm) {
export function initLifecycle (vm: Vue) {
const options = vm.$options

vm.$parent = options.parent
Expand All @@ -15,13 +19,13 @@ export function initLifecycle (vm) {
vm.$children = []
vm.$refs = {}

vm._mounted = false
vm._isMounted = false
vm._isDestroyed = false
vm._isBeingDestroyed = false
}

export function lifecycleMixin (Vue) {
Vue.prototype._mount = function () {
export function lifecycleMixin (Vue: Class<Vue>) {
Vue.prototype._mount = function (): Vue {
if (!this.$options.render) {
this.$options.render = () => this.$createElement('div')
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -43,16 +47,16 @@ export function lifecycleMixin (Vue) {
callHook(this, 'beforeMount')
this._watcher = new Watcher(this, this._render, this._update)
this._update(this._watcher.value)
this._mounted = true
this._isMounted = true
// root instance, call mounted on self
if (this.$root === this) {
callHook(this, 'mounted')
}
return this
}

Vue.prototype._update = function (vnode) {
if (this._mounted) {
Vue.prototype._update = function (vnode: VNode) {
if (this._isMounted) {
callHook(this, 'beforeUpdate')
}
if (!this._vnode) {
Expand All @@ -68,12 +72,17 @@ export function lifecycleMixin (Vue) {
if (parentNode) {
parentNode.elm = this.$el
}
if (this._mounted) {
if (this._isMounted) {
callHook(this, 'updated')
}
}

Vue.prototype._updateFromParent = function (propsData, listeners, parentVnode, renderChildren) {
Vue.prototype._updateFromParent = function (
propsData?: Object,
listeners?: Object,
parentVnode: VNode,
renderChildren: Array<VNode> | () => Array<VNode>
) {
this.$options._parentVnode = parentVnode
this.$options._renderChildren = renderChildren
// update props
Expand Down Expand Up @@ -133,7 +142,7 @@ export function lifecycleMixin (Vue) {
}
}

export function callHook (vm, hook) {
export function callHook (vm: Vue, hook: string) {
vm.$emit('pre-hook:' + hook)
const handlers = vm.$options[hook]
if (handlers) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/instance/proxy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* not type checking this file because flow doesn't play well with Proxy */

import { warn, makeMap } from '../util/index'

let hasProxy, proxyHandlers, initProxy
Expand Down
Loading

0 comments on commit 354ea61

Please sign in to comment.