# All files / src/instance init.ts

84.93% Statements 62/73
68.96% Branches 20/29
50% Functions 8/16
84.93% Lines 62/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    1x
    3x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    20x
    20x
    20x

    20x

    20x

    22x
    22x
    22x
    22x
    22x

    20x

    1x
    1x
    1x
    1x

    1x

    1x

    2x
    2x
    2x
    2x

    2x
    22x

    2x
    2x
    2x
    2x
    2x
    2x

    2x
    1x

    2x
    1x

    2x
    2x
    2x

    2x
    2x

    1x
    1x
    2x

    1x
    1x
    2x

    1x
    1x
    1x

     import shallowEqual from 'shallowequal'
    import classNames from 'classnames'
    import styleToCssString from 'style-object-to-css-string'
    import { warn } from '../util/warn'
    import { ComponentOptions } from '../types/options'
    import { nextTick } from '../util/nextTick'
    import { eventsMixin, renderMixin, stateMixin } from './mixin'
    import { proxy } from './proxy'
    import { config } from './config'
    import { isDev } from '../util/env'

    let uid: number = 0
    let cid: number = 1

    class Doraemon {
      _isDoraemon: boolean = false
      _isMounted: boolean = false
      _isDestroyed: boolean = false
      _renderProxy: ComponentRenderProxy<Doraemon>
      _uid: number
      _self: this
      $options: ComponentOptions<Doraemon>
      get $root (): Doraemon {
        return undefined
      }
      get $parent (): Doraemon {
        return undefined
      }
      get $children (): Doraemon[] {
        return undefined
      }
      get $refs (): { [key: string]: Doraemon | Doraemon[] | undefined } {
        return undefined
      }
      get $data (): Record<string, any> {
        return undefined
      }
      get $props (): Record<string, any> {
        return undefined
      }
      $emit: (event: string, ...args: any[]) => this
      $nextTick: (callback: (this: this) => void) => void

      /**
       * Creates an instance of Doraemon.
       *
       * @param {ComponentOptions<Doraemon>} [options]
       * @memberof Doraemon
       */
      constructor (options?: ComponentOptions<Doraemon>) {
        if (isDev && !(this instanceof Doraemon)) {
          warn('Doraemon is a constructor and should be called with the `new` keyword')
        }I
        this._init(options)
      }

      /**
       * init.
       *
       * @param {ComponentOptions<Doraemon>} [options]
       * @memberof Doraemon
       */
      _init (options?: ComponentOptions<Doraemon>) {
        const vm = this
        vm._uid = uid++
        vm._isDoraemon = true
        vm.$options = Object.assign({},
          (vm.constructor as DoraemonClass<Doraemon>).options,
          options || {}
        )
        vm._self = vm
      }

      /**
       * proxy miniprogram instance.
       *
       * @param {ComponentRenderProxy<Doraemon>} vm
       * @memberof Doraemon
       */
      _render (vm: ComponentRenderProxy<Doraemon>) {
        this._renderProxy = vm
      }

      static cid: number = 0
      static options: ComponentOptions<Doraemon> = {}
      static nextTick = nextTick
      static extend = extend
      static get config () {
        return config
      }
      static set config (_) {
        if (isDev) {
          warn(
            'Do not replace the Doraemon.config object, set individual fields instead.'
          )
        }
      }

      /**
       * exposed util methods.
       *
       * @static
       * @memberof Doraemon
       */
      static util = {
        warn,
        shallowEqual,
        classNames,
        styleToCssString,
      }
    }

    /**
     * Class inheritance
     *
     * @Iparam {ComponentOptions<Doraemon>} [extendOptions={}]
     * @returns
     */
    function extend (extendOptions: ComponentOptions<Doraemon> = {}) {
      const Super = this as DoraemonClass<Doraemon>
      const SuperId = Super.cid
      const cachedCtors = (extendOptions as any)._Ctor || ((extendOptions as any)._Ctor = {})
      if (cachedCtors[SuperId]) {
        return cachedCtors[SuperId]
      }
      const Sub = function DoraemonComponent(options: ComponentOptions<Doraemon> = {}) {
        this._init(options)
      }
      Sub.prototype = Object.create(Super.prototype)
      Sub.prototype.constructor = Sub
      Sub.cid = cid++
      Sub.options = Object.assign({}, Super.options, extendOptions)
      Sub.extend = Super.extend
      Sub['super'] = Super

      // 在这里 props & computed 定义在原型上
      // 避免为每个创建的实例调用 Object.defineProperty
      if (Sub.options.props) {
        initProps(Sub)
      }
      if (Sub.options.computed) {
        initComputed(Sub)
      }

      // keep a reference to the super options at extension time
      Sub.superOptions = Super.options
      Sub.extendOptions = extendOptions
      Sub.sealedOptions = Object.assign({}, Sub.options)

      // cache constructor
      cachedCtors[SuperId] = Sub

      return Sub
    }

    function initProps (Component) {
      const props = Component.options.props || {}
      for (const key in props) {
        proxy(Component.prototype, 'data', key)
      }
    }

    function initComputed (Component) {
      const computed = Component.options.computed || {}
      for (const key in computed) {
        proxy(Component.prototype, 'data', key)
      }
    }

    stateMixin(Doraemon)
    eventsMixin(Doraemon)
    renderMixin(Doraemon)

    export {
      Doraemon,
    }

    export type DoraemonClass<D> = { new (...args: any[]): D & Doraemon } & typeof Doraemon

    export type ComponentRenderProxy<
      D extends Doraemon
    > = WechatMiniprogram.Component.Instance<
      WechatMiniprogram.Component.DataOption,
      WechatMiniprogram.Component.PropertyOption,
      Partial<WechatMiniprogram.Component.MethodOption>,
      {
        $component: D
      }
    >