Vue——mergeOptions【四】

来源:https://www.cnblogs.com/wangyang0210/archive/2023/03/14/17216197.html
-Advertisement-
Play Games

前言 前面我們簡單的瞭解了 vue 初始化時的一些大概的流程,這裡我們詳細的瞭解下具體的內容; 這塊建議搭建可以根據 demo 進行 debugger 來觀察; 內容 這一塊主要圍繞init.ts中的mergeOptions進行剖析。 mergeOptions mergeOptions的方法位於sc ...


前言

前面我們簡單的瞭解了 vue 初始化時的一些大概的流程,這裡我們詳細的瞭解下具體的內容;
這塊建議搭建可以根據 demo 進行 debugger 來觀察;

內容

這一塊主要圍繞init.ts中的mergeOptions進行剖析。

mergeOptions

mergeOptions的方法位於scr/core/util/options.ts中;

/**
 * Option overwriting strategies are functions that handle
 * how to merge a parent option value and a child option
 * value into the final value.
 * 選項合併策略 處理合併parent選項值和child選項值並轉為最終的值
 */
const strats = config.optionMergeStrategies

/**
 * Options with restrictions
 *
 */
if (__DEV__) {
  strats.el = strats.propsData = function (
    parent: any,
    child: any,
    vm: any,
    key: any
  ) {
    if (!vm) {
      warn(
        `option "${key}" can only be used during instance ` +
          'creation with the `new` keyword.'
      )
    }
    return defaultStrat(parent, child)
  }
}

/**
 * Helper that recursively merges two data objects together.
 *
 * 合併data選項
 *
 */
function mergeData(
  to: Record<string | symbol, any>,
  from: Record<string | symbol, any> | null,
  recursive = true
): Record<PropertyKey, any> {
  if (!from) return to
  let key, toVal, fromVal

  const keys = hasSymbol
    ? (Reflect.ownKeys(from) as string[])
    : Object.keys(from)

  for (let i = 0; i < keys.length; i++) {
    key = keys[i]
    // in case the object is already observed...
    // 跳過已經存在響應式的對象
    if (key === '__ob__') continue
    toVal = to[key]
    fromVal = from[key]
    if (!recursive || !hasOwn(to, key)) {
      // 對數據進行響應式處理
      set(to, key, fromVal)
    } else if (
      toVal !== fromVal &&
      isPlainObject(toVal) &&
      isPlainObject(fromVal)
    ) {
      // 如果parent和child都有值卻不相等而且兩個都是對象的時候,繼續遞歸合併
      mergeData(toVal, fromVal)
    }
  }
  return to
}

/**
 * Data
 *
 * 合併作為函數的data
 */
export function mergeDataOrFn(
  parentVal: any,
  childVal: any,
  vm?: Component
): Function | null {
  // 判斷是否存在vue實例
  if (!vm) {
    // in a Vue.extend merge, both should be functions
    // 在Vue.extend的合併中,兩個參數都應該是函數
    if (!childVal) {
      return parentVal
    }
    if (!parentVal) {
      return childVal
    }
    // when parentVal & childVal are both present,
    // 當parentVal和childVal都存在的時候
    // we need to return a function that returns the
    // 我們需要返回一個函數
    // merged result of both functions... no need to
    // 該函數返回兩者合併的結果
    // check if parentVal is a function here because
    // 不需要去檢查parentVal是否是一個函數因為
    // it has to be a function to pass previous merges.
    // 他肯定是先前合併的函數
    return function mergedDataFn() {
      // 合併data數據
      return mergeData(
        isFunction(childVal) ? childVal.call(this, this) : childVal,
        isFunction(parentVal) ? parentVal.call(this, this) : parentVal
      )
    }
  } else {
    // 合併實例data函數
    return function mergedInstanceDataFn() {
      // instance merge
      // child 數據
      const instanceData = isFunction(childVal)
        ? childVal.call(vm, vm)
        : childVal
      // 預設數據
      const defaultData = isFunction(parentVal)
        ? parentVal.call(vm, vm)
        : parentVal
      // 如果child存在數據則進行合併否則直接返回預設數據
      if (instanceData) {
        return mergeData(instanceData, defaultData)
      } else {
        return defaultData
      }
    }
  }
}

strats.data = function (
  parentVal: any,
  childVal: any,
  vm?: Component
): Function | null {
  if (!vm) {
    // dev環境下會判斷child是否為函數,不是的話則發出警告並返回parentVal
    if (childVal && typeof childVal !== 'function') {
      __DEV__ &&
        warn(
          'The "data" option should be a function ' +
            'that returns a per-instance value in component ' +
            'definitions.',
          vm
        )
      return parentVal
    }
    return mergeDataOrFn(parentVal, childVal)
  }

  return mergeDataOrFn(parentVal, childVal, vm)
}

/**
 * Hooks and props are merged as arrays.
 *
 * 生命周期合併策略會將生命周期內的鉤子函數和props轉化為數組格式併合併到一個數組中
 */
export function mergeLifecycleHook(
  parentVal: Array<Function> | null,
  childVal: Function | Array<Function> | null
): Array<Function> | null {
  const res = childVal
    ? parentVal
      ? parentVal.concat(childVal)
      : isArray(childVal)
      ? childVal
      : [childVal]
    : parentVal
  return res ? dedupeHooks(res) : res
}

function dedupeHooks(hooks: any) {
  const res: Array<any> = []
  for (let i = 0; i < hooks.length; i++) {
    if (res.indexOf(hooks[i]) === -1) {
      res.push(hooks[i])
    }
  }
  return res
}

LIFECYCLE_HOOKS.forEach(hook => {
  strats[hook] = mergeLifecycleHook
})

/**
 * Assets
 *
 * When a vm is present (instance creation), we need to do
 * a three-way merge between constructor options, instance
 * options and parent options.
 * 當存在vm(實例創建)時,我們需要在構造函數選項、實例和父選之間進行三方合併
 *
 */
function mergeAssets(
  parentVal: Object | null,
  childVal: Object | null,
  vm: Component | null,
  key: string
): Object {
  const res = Object.create(parentVal || null)
  if (childVal) {
    __DEV__ && assertObjectType(key, childVal, vm)
    // 將child合併到parentVal中會進行覆蓋
    return extend(res, childVal)
  } else {
    return res
  }
}

ASSET_TYPES.forEach(function (type) {
  strats[type + 's'] = mergeAssets
})

/**
 * Watchers.
 *
 * Watchers hashes should not overwrite one
 * another, so we merge them as arrays.
 *監聽不應該被覆蓋,所以使用數組格式進行合併
 *
 * watch合併
 */
strats.watch = function (
  parentVal: Record<string, any> | null,
  childVal: Record<string, any> | null,
  vm: Component | null,
  key: string
): Object | null {
  // work around Firefox's Object.prototype.watch...
  // nativeWatch 相容火狐瀏覽器,因為火狐瀏覽器原型上存在watch
  //@ts-expect-error work around
  if (parentVal === nativeWatch) parentVal = undefined
  //@ts-expect-error work around
  if (childVal === nativeWatch) childVal = undefined
  /* istanbul ignore if */
  if (!childVal) return Object.create(parentVal || null)
  if (__DEV__) {
    assertObjectType(key, childVal, vm)
  }
  if (!parentVal) return childVal
  const ret: Record<string, any> = {}
  extend(ret, parentVal)
  for (const key in childVal) {
    let parent = ret[key]
    const child = childVal[key]
    if (parent && !isArray(parent)) {
      parent = [parent]
    }
    ret[key] = parent ? parent.concat(child) : isArray(child) ? child : [child]
  }
  return ret
}

/**
 * Other object hashes.
 *
 * 對象合併,存在childVal的話以childVal為準
 */
strats.props =
  strats.methods =
  strats.inject =
  strats.computed =
    function (
      parentVal: Object | null,
      childVal: Object | null,
      vm: Component | null,
      key: string
    ): Object | null {
      if (childVal && __DEV__) {
        assertObjectType(key, childVal, vm)
      }
      if (!parentVal) return childVal
      const ret = Object.create(null)
      extend(ret, parentVal)
      if (childVal) extend(ret, childVal)
      return ret
    }

/**
 * provide合併
 */
strats.provide = function (parentVal: Object | null, childVal: Object | null) {
  if (!parentVal) return childVal
  return function () {
    const ret = Object.create(null)
    mergeData(ret, isFunction(parentVal) ? parentVal.call(this) : parentVal)
    if (childVal) {
      // 不進行遞歸合併
      mergeData(
        ret,
        isFunction(childVal) ? childVal.call(this) : childVal,
        false // non-recursive
      )
    }
    return ret
  }
}

/**
 * Default strategy.
 * 預設值策略 | 避免parentVal被未定義的childVal覆蓋
 */
const defaultStrat = function (parentVal: any, childVal: any): any {
  return childVal === undefined ? parentVal : childVal
}

/**
 * Validate component names
 * 校驗組件名是否合法 | 避免組件名稱使用保留的關鍵字或者不符合html5規範
 */
function checkComponents(options: Record<string, any>) {
  for (const key in options.components) {
    validateComponentName(key)
  }
}

export function validateComponentName(name: string) {
  if (
    !new RegExp(`^[a-zA-Z][\\-\\.0-9_${unicodeRegExp.source}]*$`).test(name)
  ) {
    warn(
      'Invalid component name: "' +
        name +
        '". Component names ' +
        'should conform to valid custom element name in html5 specification.'
    )
  }
  if (isBuiltInTag(name) || config.isReservedTag(name)) {
    warn(
      'Do not use built-in or reserved HTML elements as component ' +
        'id: ' +
        name
    )
  }
}

/**
 * Ensure all props option syntax are normalized into the
 * Object-based format.
 *
 * 格式化object對象 | 確保所有的props選項的語法都符合對象格式
 */
function normalizeProps(options: Record<string, any>, vm?: Component | null) {
  const props = options.props
  // 不存在props則直接return
  if (!props) return
  const res: Record<string, any> = {}
  let i, val, name
  // 判斷是否是數組
  if (isArray(props)) {
    i = props.length
    while (i--) {
      val = props[i]
      if (typeof val === 'string') {
        // 使用駝峰來代替-連字元
        name = camelize(val)
        res[name] = { type: null }
      } else if (__DEV__) {
        // 如果是dev環境則發出警告,提示在數組語法下props必須為字元串
        warn('props must be strings when using array syntax.')
      }
    }
  } else if (isPlainObject(props)) {
    // 判斷是否為對象
    for (const key in props) {
      val = props[key]
      // 使用駝峰來代替-連字元
      name = camelize(key)
      res[name] = isPlainObject(val) ? val : { type: val }
    }
  } else if (__DEV__) {
    // 如果是dev環境則發出警告,提示props應該是一個數組或者對象
    warn(
      `Invalid value for option "props": expected an Array or an Object, ` +
        `but got ${toRawType(props)}.`,
      vm
    )
  }
  options.props = res
}

/**
 * Normalize all injections into Object-based format
 *
 * 將所有的inject格式化object對象
 */
function normalizeInject(options: Record<string, any>, vm?: Component | null) {
  const inject = options.inject
  if (!inject) return
  const normalized: Record<string, any> = (options.inject = {})
  if (isArray(inject)) {
    for (let i = 0; i < inject.length; i++) {
      normalized[inject[i]] = { from: inject[i] }
    }
  } else if (isPlainObject(inject)) {
    for (const key in inject) {
      const val = inject[key]
      normalized[key] = isPlainObject(val)
        ? extend({ from: key }, val)
        : { from: val }
    }
  } else if (__DEV__) {
    // 開發環境下如果inject格式不是數組或者對象則發出警告
    warn(
      `Invalid value for option "inject": expected an Array or an Object, ` +
        `but got ${toRawType(inject)}.`,
      vm
    )
  }
}

/**
 * Normalize raw function directives into object format.
 *
 *將原始的指令函數轉為object對象格式
 */
function normalizeDirectives(options: Record<string, any>) {
  const dirs = options.directives
  if (dirs) {
    for (const key in dirs) {
      const def = dirs[key]
      if (isFunction(def)) {
        dirs[key] = { bind: def, update: def }
      }
    }
  }
}

// 開發環境下,會進行檢測,如果不是對象的話發出警告
function assertObjectType(name: string, value: any, vm: Component | null) {
  if (!isPlainObject(value)) {
    warn(
      `Invalid value for option "${name}": expected an Object, ` +
        `but got ${toRawType(value)}.`,
      vm
    )
  }
}

/**
 * Merge two option objects into a new one.
 * Core utility used in both instantiation and inheritance.
 *
 * 將兩個option對象合併成一個新的對象
 * 用於實例化和繼承的核心工具
 */
export function mergeOptions(
  parent: Record<string, any>,
  child: Record<string, any>,
  vm?: Component | null
): ComponentOptions {
  // dev環境下會檢查組件名稱是否合法
  if (__DEV__) {
    checkComponents(child)
  }

  // 檢查option是否是函數,是的話直接將options賦值給child
  if (isFunction(child)) {
    // @ts-expect-error
    child = child.options
  }

  // 格式化props為object對象 | 檢測格式是否為數組和對象,並使用駝峰代替-連字元,開發環境下如果格式存在問題會發出警告
  normalizeProps(child, vm)
  // 格式化inject為object對象 | 檢測格式是否為數組和對象,開發環境下如果格式存在問題會發出警告
  normalizeInject(child, vm)
  // 格式化directive為object對象
  normalizeDirectives(child)

  // Apply extends and mixins on the child options,
  // 在子選項上去應用 extends和mixins
  // but only if it is a raw options object that isn't
  // the result of another mergeOptions call.
  // 前提是它是一個原始選項對象,而不是另一個mergeOptions的結果
  // Only merged options has the _base property.
  // 只合併具有_base屬性的合併選項
  if (!child._base) {
    // 合併extends到parent
    if (child.extends) {
      parent = mergeOptions(parent, child.extends, vm)
    }
    // 合併mixins到parent
    if (child.mixins) {
      for (let i = 0, l = child.mixins.length; i < l; i++) {
        parent = mergeOptions(parent, child.mixins[i], vm)
      }
    }
  }

  const options: ComponentOptions = {} as any
  let key
  for (key in parent) {
    mergeField(key)
  }
  for (key in child) {
    if (!hasOwn(parent, key)) {
      mergeField(key)
    }
  }
  // 合併parent和child選項
  function mergeField(key: any) {
    const strat = strats[key] || defaultStrat
    options[key] = strat(parent[key], child[key], vm, key)
  }
  return options
}

/**
 * Resolve an asset.
 * 解析資源
 * This function is used because child instances need access
 * to assets defined in its ancestor chain.
 * 使用這個函數是因為子實例中需要訪問其祖先中定義的資源
 */
export function resolveAsset(
  options: Record<string, any>,
  type: string,
  id: string,
  warnMissing?: boolean
): any {
  /* istanbul ignore if */
  if (typeof id !== 'string') {
    return
  }
  const assets = options[type]
  // check local registration variations first
  if (hasOwn(assets, id)) return assets[id]
  const camelizedId = camelize(id)
  if (hasOwn(assets, camelizedId)) return assets[camelizedId]
  const PascalCaseId = capitalize(camelizedId)
  if (hasOwn(assets, PascalCaseId)) return assets[PascalCaseId]
  // fallback to prototype chain
  const res = assets[id] || assets[camelizedId] || assets[PascalCaseId]
  if (__DEV__ && warnMissing && !res) {
    warn('Failed to resolve ' + type.slice(0, -1) + ': ' + id)
  }
  return res
}
學無止境,謙卑而行.
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 第一步就是安裝 為了節省資源,運行起來更快捷,首先是在電腦上安裝好vm虛擬機, 新建虛擬機,安裝xp,也就是把xp光碟文件導入, 接著在虛擬機中下載oracle,解壓的話會用到WinRAR,也一併導入虛擬機C盤 然後在主機上,安裝客戶端client,plsql, 打開虛擬機中的監聽,連接資料庫, 登 ...
  • 摘要:本文主要為大家帶來Mysql中的3種數據類型和3種運算符。 本文分享自華為雲社區《Mysql中的數據類型和運算符》,作者: 1+1=王。 Mysql的數據類型 Mysql支持數值型、文本型和日期時間型三大數據類型。 數值型數據 數值型是描述定量數據的數據類型,包括整數型數據類型和浮點型數據類型 ...
  • 摘要:本文介紹的DSC工具是針對資料庫切換時面臨的遷移任務而開發的免安裝命令行工具。目的是提供簡單、快速、可靠的SQL腳本遷移服務。 本文分享自華為雲社區《GaussDB(DWS)DSC工具系列:DSC工具初識【玩轉PB級數倉GaussDB(DWS)】》,作者:積少成多 。 DSC背景介紹與DSC介 ...
  • MySQL鎖的粒度分為:行級鎖、表級鎖、頁級鎖。 一、行級鎖(INNODB引擎) 行級鎖是Mysql中鎖定粒度最細的一種鎖,表示只針對當前操作的行進行加鎖。 行級鎖能大大減少資料庫操作的衝突。其加鎖粒度最小,但加鎖的開銷也最大。 行級鎖分為共用鎖 和 排他鎖。 特點:開銷大,加鎖慢;會出現死鎖;鎖定 ...
  • 轉載自作者zhang502219048的微信公眾號【SQL資料庫編程】:巧妙使用SQL Server的計算列實現項目唯一規則快速定製 軟體產品,相當於是一個通用模板。而軟體項目,則是基於軟體產品的項目個性化定製。不同軟體項目的定製多種多樣,如何能快速實現軟體項目的定製,則是軟體產品設計者所需要優先考 ...
  • 前言 前面我們簡單的瞭解了 vue 初始化時的一些大概的流程,這裡我們詳細的瞭解下具體的內容;這塊建議搭建可以根據 demo 進行 debugger 來觀察; 內容 這一塊主要圍繞init.ts中的initLifecycle進行剖析。 initLifecycle initLifecycle的方法位於 ...
  • 分享一個面試題: 聲明一個數組,代表股票的各個期值,求在這個階段最大的收益值為多少? 簡言之:其實就是求數組中兩個值的差值中,最大的值。 第一反應的思路就是,進行雙層迴圈進行差值計算,再從差值計算獲得的數組中選出最大的值。面試完想了這個方法一下有些麻煩,還不如直接就在迴圈中比較出來,選出最大值,直接 ...
  • OSI OSI是Open System Interconnect的縮寫,意為開放式系統互聯。其各個層次的劃分遵循下列原則: ​ (1)同一層中的各網路節點都有相同的層次結構,具有同樣的功能。 ​ (2)同一節點內相鄰層之間通過介面進行通信。 ​ (3)七層結構中的每一層使用下一層提供的服務,並且向其 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...