Browse Source

refactor: remove constants from config

dev
Evan You 8 years ago
parent
commit
69e62f0a74
  1. 48
      src/core/config.js
  2. 3
      src/core/global-api/assets.js
  3. 4
      src/core/global-api/extend.js
  4. 3
      src/core/global-api/index.js
  5. 5
      src/core/observer/scheduler.js
  6. 14
      src/core/util/options.js
  7. 2
      src/server/render.js
  8. 20
      src/shared/constants.js
  9. 2
      src/shared/util.js
  10. 8
      test/unit/modules/observer/scheduler.spec.js

48
src/core/config.js

@ -1,6 +1,10 @@
/* @flow */
import { no, noop, identity } from 'shared/util'
import {
no,
noop,
identity
} from 'shared/util'
export type Config = {
// user
@ -18,13 +22,9 @@ export type Config = {
isUnknownElement: (x?: string) => boolean;
getTagNamespace: (x?: string) => string | void;
mustUseProp: (tag: string, type: ?string, name: string) => boolean;
// internal
_assetTypes: Array<string>;
_lifecycleHooks: Array<string>;
_maxUpdateCount: number;
};
const config: Config = {
export default ({
/**
* Option merge strategies (used in core/util/options)
*/
@ -91,37 +91,5 @@ const config: Config = {
* Check if an attribute must be bound using property, e.g. value
* Platform-dependent.
*/
mustUseProp: no,
/**
* List of asset types that a component can own.
*/
_assetTypes: [
'component',
'directive',
'filter'
],
/**
* List of lifecycle hooks.
*/
_lifecycleHooks: [
'beforeCreate',
'created',
'beforeMount',
'mounted',
'beforeUpdate',
'updated',
'beforeDestroy',
'destroyed',
'activated',
'deactivated'
],
/**
* Max circular updates allowed in a scheduler flush cycle.
*/
_maxUpdateCount: 100
}
export default config
mustUseProp: no
}: Config)

3
src/core/global-api/assets.js

@ -1,13 +1,14 @@
/* @flow */
import config from '../config'
import { ASSET_TYPES } from 'shared/constants'
import { warn, isPlainObject } from '../util/index'
export function initAssetRegisters (Vue: GlobalAPI) {
/**
* Create asset registration methods.
*/
config._assetTypes.forEach(type => {
ASSET_TYPES.forEach(type => {
Vue[type] = function (
id: string,
definition: Function | Object

4
src/core/global-api/extend.js

@ -1,6 +1,6 @@
/* @flow */
import config from '../config'
import { ASSET_TYPES } from 'shared/constants'
import { warn, extend, mergeOptions } from '../util/index'
import { defineComputed, proxy } from '../instance/state'
@ -65,7 +65,7 @@ export function initExtend (Vue: GlobalAPI) {
// create asset registers, so extended classes
// can have their private assets too.
config._assetTypes.forEach(function (type) {
ASSET_TYPES.forEach(function (type) {
Sub[type] = Super[type]
})
// enable recursive self-lookup

3
src/core/global-api/index.js

@ -6,6 +6,7 @@ import { initMixin } from './mixin'
import { initExtend } from './extend'
import { initAssetRegisters } from './assets'
import { set, del } from '../observer/index'
import { ASSET_TYPES } from 'shared/constants'
import builtInComponents from '../components/index'
import {
@ -44,7 +45,7 @@ export function initGlobalAPI (Vue: GlobalAPI) {
Vue.nextTick = nextTick
Vue.options = Object.create(null)
config._assetTypes.forEach(type => {
ASSET_TYPES.forEach(type => {
Vue.options[type + 's'] = Object.create(null)
})

5
src/core/observer/scheduler.js

@ -3,12 +3,15 @@
import type Watcher from './watcher'
import config from '../config'
import { callHook } from '../instance/lifecycle'
import {
warn,
nextTick,
devtools
} from '../util/index'
export const MAX_UPDATE_COUNT = 100
const queue: Array<Watcher> = []
let has: { [key: number]: ?true } = {}
let circular: { [key: number]: number } = {}
@ -55,7 +58,7 @@ function flushSchedulerQueue () {
// in dev build, check and stop circular updates.
if (process.env.NODE_ENV !== 'production' && has[id] != null) {
circular[id] = (circular[id] || 0) + 1
if (circular[id] > config._maxUpdateCount) {
if (circular[id] > MAX_UPDATE_COUNT) {
warn(
'You may have an infinite update loop ' + (
watcher.user

14
src/core/util/options.js

@ -4,13 +4,19 @@ import Vue from '../instance/index'
import config from '../config'
import { warn } from './debug'
import { set } from '../observer/index'
import {
ASSET_TYPES,
LIFECYCLE_HOOKS
} from 'shared/constants'
import {
extend,
isPlainObject,
hasOwn,
camelize,
capitalize,
isBuiltInTag
isBuiltInTag,
isPlainObject
} from 'shared/util'
/**
@ -125,7 +131,7 @@ function mergeHook (
: parentVal
}
config._lifecycleHooks.forEach(hook => {
LIFECYCLE_HOOKS.forEach(hook => {
strats[hook] = mergeHook
})
@ -143,7 +149,7 @@ function mergeAssets (parentVal: ?Object, childVal: ?Object): Object {
: res
}
config._assetTypes.forEach(function (type) {
ASSET_TYPES.forEach(function (type) {
strats[type + 's'] = mergeAssets
})

2
src/server/render.js

@ -1,7 +1,7 @@
/* @flow */
import { escape } from 'he'
import { SSR_ATTR } from 'shared/util'
import { SSR_ATTR } from 'shared/constants'
import { RenderContext } from './render-context'
import { compileToFunctions } from 'web/compiler/index'
import { createComponentInstanceForVnode } from 'core/vdom/create-component'

20
src/shared/constants.js

@ -0,0 +1,20 @@
export const SSR_ATTR = 'data-server-rendered'
export const ASSET_TYPES = [
'component',
'directive',
'filter'
]
export const LIFECYCLE_HOOKS = [
'beforeCreate',
'created',
'beforeMount',
'mounted',
'beforeUpdate',
'updated',
'beforeDestroy',
'destroyed',
'activated',
'deactivated'
]

2
src/shared/util.js

@ -1,7 +1,5 @@
/* @flow */
export const SSR_ATTR = 'data-server-rendered'
// these helpers produces better vm code in JS engines due to their
// explicitness and function inlining
export function isUndef (v: any): boolean {

8
test/unit/modules/observer/scheduler.spec.js

@ -1,6 +1,8 @@
import Vue from 'vue'
import config from 'core/config'
import { queueWatcher as _queueWatcher } from 'core/observer/scheduler'
import {
MAX_UPDATE_COUNT,
queueWatcher as _queueWatcher
} from 'core/observer/scheduler'
function queueWatcher (watcher) {
watcher.vm = {} // mock vm
@ -119,7 +121,7 @@ describe('Scheduler', () => {
}
queueWatcher(job)
waitForUpdate(() => {
expect(count).toBe(config._maxUpdateCount + 1)
expect(count).toBe(MAX_UPDATE_COUNT + 1)
expect('infinite update loop').toHaveBeenWarned()
}).then(done)
})

Loading…
Cancel
Save