vue 2.6.13 → 2.7.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (262) hide show
  1. package/README.md +17 -295
  2. package/compiler-sfc/index.d.ts +1 -0
  3. package/compiler-sfc/index.js +1 -0
  4. package/compiler-sfc/index.mjs +1 -0
  5. package/compiler-sfc/package.json +5 -0
  6. package/dist/vue.common.dev.js +11481 -11946
  7. package/dist/vue.common.prod.js +8 -3
  8. package/dist/vue.esm.browser.js +11321 -12011
  9. package/dist/vue.esm.browser.min.js +8 -3
  10. package/dist/vue.esm.js +11476 -11984
  11. package/dist/vue.js +11484 -11949
  12. package/dist/vue.min.js +8 -3
  13. package/dist/vue.runtime.common.dev.js +8427 -8421
  14. package/dist/vue.runtime.common.prod.js +8 -3
  15. package/dist/vue.runtime.esm.js +8436 -8454
  16. package/dist/vue.runtime.js +8430 -8424
  17. package/dist/vue.runtime.min.js +8 -3
  18. package/package.json +88 -111
  19. package/packages/compiler-sfc/dist/compiler-sfc.js +7 -0
  20. package/packages/compiler-sfc/node_modules/.bin/parser +17 -0
  21. package/packages/compiler-sfc/node_modules/.bin/sass +17 -0
  22. package/packages/compiler-sfc/package.json +30 -0
  23. package/src/compiler/{codeframe.js → codeframe.ts} +9 -7
  24. package/src/compiler/codegen/{events.js → events.ts} +21 -41
  25. package/src/compiler/codegen/{index.js → index.ts} +146 -136
  26. package/src/compiler/{create-compiler.js → create-compiler.ts} +26 -18
  27. package/src/compiler/directives/bind.ts +9 -0
  28. package/src/compiler/directives/{index.js → index.ts} +0 -2
  29. package/src/compiler/directives/{model.js → model.ts} +15 -18
  30. package/src/compiler/directives/{on.js → on.ts} +3 -4
  31. package/src/compiler/{error-detector.js → error-detector.ts} +61 -31
  32. package/src/compiler/{helpers.js → helpers.ts} +73 -61
  33. package/src/compiler/{index.js → index.ts} +2 -3
  34. package/src/compiler/{optimizer.js → optimizer.ts} +30 -23
  35. package/src/compiler/parser/{entity-decoder.js → entity-decoder.ts} +1 -3
  36. package/src/compiler/parser/{filter-parser.js → filter-parser.ts} +43 -24
  37. package/src/compiler/parser/{html-parser.js → html-parser.ts} +50 -29
  38. package/src/compiler/parser/{index.js → index.ts} +190 -172
  39. package/src/compiler/parser/{text-parser.js → text-parser.ts} +8 -9
  40. package/src/compiler/{to-function.js → to-function.ts} +27 -22
  41. package/src/core/components/{index.js → index.ts} +0 -0
  42. package/src/core/components/{keep-alive.js → keep-alive.ts} +42 -30
  43. package/src/core/{config.js → config.ts} +28 -30
  44. package/src/core/global-api/{assets.js → assets.ts} +8 -7
  45. package/src/core/global-api/{extend.js → extend.ts} +10 -13
  46. package/src/core/global-api/{index.js → index.ts} +4 -5
  47. package/src/core/global-api/{mixin.js → mixin.ts} +2 -3
  48. package/src/core/global-api/use.ts +23 -0
  49. package/src/core/{index.js → index.ts} +1 -1
  50. package/src/core/instance/{events.js → events.ts} +41 -24
  51. package/src/core/instance/{index.js → index.ts} +9 -5
  52. package/src/core/instance/{init.js → init.ts} +26 -15
  53. package/src/core/instance/inject.ts +72 -0
  54. package/src/core/instance/{lifecycle.js → lifecycle.ts} +86 -44
  55. package/src/core/instance/{proxy.js → proxy.ts} +29 -24
  56. package/src/core/instance/render-helpers/{bind-dynamic-keys.js → bind-dynamic-keys.ts} +6 -5
  57. package/src/core/instance/render-helpers/{bind-object-listeners.js → bind-object-listeners.ts} +4 -8
  58. package/src/core/instance/render-helpers/{bind-object-props.js → bind-object-props.ts} +12 -17
  59. package/src/core/instance/render-helpers/{check-keycodes.js → check-keycodes.ts} +5 -7
  60. package/src/core/instance/render-helpers/{index.js → index.ts} +1 -3
  61. package/src/core/instance/render-helpers/{render-list.js → render-list.ts} +12 -13
  62. package/src/core/instance/render-helpers/{render-slot.js → render-slot.ts} +10 -11
  63. package/src/core/instance/render-helpers/{render-static.js → render-static.ts} +7 -10
  64. package/src/core/instance/render-helpers/{resolve-filter.js → resolve-filter.ts} +1 -3
  65. package/src/core/instance/render-helpers/{resolve-scoped-slots.js → resolve-scoped-slots.ts} +11 -8
  66. package/src/core/instance/render-helpers/{resolve-slots.js → resolve-slots.ts} +12 -11
  67. package/src/core/instance/{render.js → render.ts} +66 -29
  68. package/src/core/instance/{state.js → state.ts} +100 -83
  69. package/src/core/observer/{array.js → array.ts} +11 -2
  70. package/src/core/observer/{dep.js → dep.ts} +37 -15
  71. package/src/core/observer/{index.js → index.ts} +137 -71
  72. package/src/core/observer/{scheduler.js → scheduler.ts} +42 -43
  73. package/src/core/observer/{traverse.js → traverse.ts} +10 -7
  74. package/src/core/observer/{watcher.js → watcher.ts} +88 -60
  75. package/src/core/util/{debug.js → debug.ts} +38 -34
  76. package/src/core/util/{env.js → env.ts} +27 -30
  77. package/src/core/util/{error.js → error.ts} +12 -14
  78. package/src/core/util/{index.js → index.ts} +0 -2
  79. package/src/core/util/{lang.js → lang.ts} +6 -7
  80. package/src/core/util/{next-tick.js → next-tick.ts} +16 -10
  81. package/src/core/util/{options.js → options.ts} +119 -114
  82. package/src/core/util/{perf.js → perf.ts} +5 -1
  83. package/src/core/util/{props.js → props.ts} +54 -50
  84. package/src/core/vdom/{create-component.js → create-component.ts} +51 -50
  85. package/src/core/vdom/{create-element.js → create-element.ts} +49 -43
  86. package/src/core/vdom/{create-functional-component.js → create-functional-component.ts} +52 -27
  87. package/src/core/vdom/helpers/{extract-props.js → extract-props.ts} +18 -18
  88. package/src/core/vdom/helpers/{get-first-component-child.js → get-first-component-child.ts} +6 -5
  89. package/src/core/vdom/helpers/{index.js → index.ts} +0 -2
  90. package/src/core/vdom/helpers/is-async-placeholder.ts +6 -0
  91. package/src/core/vdom/helpers/{merge-hook.js → merge-hook.ts} +7 -5
  92. package/src/core/vdom/helpers/{normalize-children.js → normalize-children.ts} +26 -16
  93. package/src/core/vdom/helpers/{normalize-scoped-slots.js → normalize-scoped-slots.ts} +23 -17
  94. package/src/core/vdom/helpers/{resolve-async-component.js → resolve-async-component.ts} +29 -37
  95. package/src/core/vdom/helpers/update-listeners.ts +101 -0
  96. package/src/core/vdom/modules/{directives.js → directives.ts} +20 -14
  97. package/src/core/vdom/modules/index.ts +4 -0
  98. package/src/core/vdom/modules/template-ref.ts +96 -0
  99. package/src/core/vdom/{patch.js → patch.ts} +239 -138
  100. package/src/core/vdom/{vnode.js → vnode.ts} +36 -31
  101. package/src/global.d.ts +15 -0
  102. package/src/platforms/web/compiler/directives/html.ts +8 -0
  103. package/src/platforms/web/compiler/directives/{index.js → index.ts} +0 -0
  104. package/src/platforms/web/compiler/directives/{model.js → model.ts} +47 -41
  105. package/src/platforms/web/compiler/directives/{text.js → text.ts} +2 -3
  106. package/src/platforms/web/compiler/{index.js → index.ts} +0 -2
  107. package/src/platforms/web/compiler/modules/{class.js → class.ts} +10 -15
  108. package/src/platforms/web/compiler/modules/{index.js → index.ts} +1 -5
  109. package/src/platforms/web/compiler/modules/{model.js → model.ts} +6 -11
  110. package/src/platforms/web/compiler/modules/{style.js → style.ts} +9 -14
  111. package/src/platforms/web/compiler/{options.js → options.ts} +1 -2
  112. package/src/platforms/web/compiler/{util.js → util.ts} +5 -7
  113. package/src/platforms/web/entry-compiler-sfc.ts +1 -0
  114. package/src/platforms/web/{entry-compiler.js → entry-compiler.ts} +1 -3
  115. package/src/platforms/web/{entry-runtime.js → entry-runtime-esm.ts} +2 -2
  116. package/src/platforms/web/entry-runtime-with-compiler-esm.ts +5 -0
  117. package/src/platforms/web/entry-runtime-with-compiler.ts +10 -0
  118. package/src/platforms/web/entry-runtime.ts +7 -0
  119. package/src/platforms/web/runtime/{class-util.js → class-util.ts} +2 -4
  120. package/src/platforms/web/runtime/components/{index.js → index.ts} +0 -0
  121. package/src/platforms/web/runtime/components/{transition-group.js → transition-group.ts} +59 -46
  122. package/src/platforms/web/runtime/components/{transition.js → transition.ts} +43 -36
  123. package/src/platforms/web/runtime/directives/{index.js → index.ts} +0 -0
  124. package/src/platforms/web/runtime/directives/{model.js → model.ts} +22 -21
  125. package/src/platforms/web/runtime/directives/{show.js → show.ts} +11 -10
  126. package/src/platforms/web/runtime/{index.js → index.ts} +9 -10
  127. package/src/platforms/web/runtime/modules/{attrs.js → attrs.ts} +12 -16
  128. package/src/platforms/web/runtime/modules/{class.js → class.ts} +7 -18
  129. package/src/platforms/web/runtime/modules/{dom-props.js → dom-props.ts} +24 -17
  130. package/src/platforms/web/runtime/modules/{events.js → events.ts} +22 -15
  131. package/src/platforms/web/runtime/modules/{index.js → index.ts} +1 -8
  132. package/src/platforms/web/runtime/modules/{style.js → style.ts} +24 -13
  133. package/src/platforms/web/runtime/modules/{transition.js → transition.ts} +61 -63
  134. package/src/platforms/web/runtime/node-ops.ts +66 -0
  135. package/src/platforms/web/runtime/{patch.js → patch.ts} +0 -2
  136. package/src/platforms/web/runtime/{transition-util.js → transition-util.ts} +63 -38
  137. package/src/platforms/web/{entry-runtime-with-compiler.js → runtime-with-compiler.ts} +28 -19
  138. package/src/platforms/web/util/{attrs.js → attrs.ts} +18 -14
  139. package/src/platforms/web/util/{class.js → class.ts} +23 -21
  140. package/src/platforms/web/util/{compat.js → compat.ts} +4 -4
  141. package/src/platforms/web/util/{element.js → element.ts} +21 -22
  142. package/src/platforms/web/util/{index.js → index.ts} +2 -6
  143. package/src/platforms/web/util/{style.js → style.ts} +12 -12
  144. package/src/sfc/{parser.js → parser.ts} +18 -15
  145. package/src/shared/{constants.js → constants.ts} +2 -6
  146. package/src/shared/{util.js → util.ts} +78 -55
  147. package/src/types/compiler.ts +224 -0
  148. package/src/types/component.ts +209 -0
  149. package/src/types/global-api.ts +37 -0
  150. package/src/types/modules.d.ts +15 -0
  151. package/src/types/options.ts +120 -0
  152. package/src/types/ssr.ts +27 -0
  153. package/src/types/utils.ts +3 -0
  154. package/src/types/vnode.ts +127 -0
  155. package/src/v3/apiInject.ts +66 -0
  156. package/src/v3/apiLifecycle.ts +53 -0
  157. package/src/v3/apiSetup.ts +161 -0
  158. package/src/v3/apiWatch.ts +355 -0
  159. package/src/v3/currentInstance.ts +23 -0
  160. package/src/v3/debug.ts +21 -0
  161. package/src/v3/h.ts +18 -0
  162. package/src/v3/index.ts +77 -0
  163. package/src/v3/reactivity/computed.ts +100 -0
  164. package/src/v3/reactivity/effect.ts +20 -0
  165. package/src/v3/reactivity/effectScope.ts +134 -0
  166. package/src/v3/reactivity/operations.ts +14 -0
  167. package/src/v3/reactivity/reactive.ts +123 -0
  168. package/src/v3/reactivity/readonly.ts +121 -0
  169. package/src/v3/reactivity/ref.ts +249 -0
  170. package/types/index.d.ts +10 -13
  171. package/types/options.d.ts +265 -132
  172. package/types/plugin.d.ts +4 -4
  173. package/types/umd.d.ts +47 -27
  174. package/types/v3-generated.d.ts +389 -0
  175. package/types/v3.d.ts +16 -0
  176. package/types/vnode.d.ts +70 -57
  177. package/types/vue.d.ts +292 -88
  178. package/dist/README.md +0 -122
  179. package/src/compiler/directives/bind.js +0 -11
  180. package/src/core/global-api/use.js +0 -23
  181. package/src/core/instance/inject.js +0 -73
  182. package/src/core/vdom/helpers/is-async-placeholder.js +0 -5
  183. package/src/core/vdom/helpers/update-listeners.js +0 -95
  184. package/src/core/vdom/modules/index.js +0 -7
  185. package/src/core/vdom/modules/ref.js +0 -45
  186. package/src/platforms/web/compiler/directives/html.js +0 -9
  187. package/src/platforms/web/entry-server-basic-renderer.js +0 -13
  188. package/src/platforms/web/entry-server-renderer.js +0 -27
  189. package/src/platforms/web/runtime/node-ops.js +0 -59
  190. package/src/platforms/web/server/compiler.js +0 -11
  191. package/src/platforms/web/server/directives/index.js +0 -7
  192. package/src/platforms/web/server/directives/model.js +0 -44
  193. package/src/platforms/web/server/directives/show.js +0 -12
  194. package/src/platforms/web/server/modules/attrs.js +0 -67
  195. package/src/platforms/web/server/modules/class.js +0 -11
  196. package/src/platforms/web/server/modules/dom-props.js +0 -50
  197. package/src/platforms/web/server/modules/index.js +0 -11
  198. package/src/platforms/web/server/modules/style.js +0 -41
  199. package/src/platforms/web/server/util.js +0 -101
  200. package/src/platforms/weex/compiler/directives/index.js +0 -5
  201. package/src/platforms/weex/compiler/directives/model.js +0 -33
  202. package/src/platforms/weex/compiler/index.js +0 -52
  203. package/src/platforms/weex/compiler/modules/append.js +0 -27
  204. package/src/platforms/weex/compiler/modules/class.js +0 -74
  205. package/src/platforms/weex/compiler/modules/index.js +0 -13
  206. package/src/platforms/weex/compiler/modules/props.js +0 -37
  207. package/src/platforms/weex/compiler/modules/recycle-list/component-root.js +0 -13
  208. package/src/platforms/weex/compiler/modules/recycle-list/component.js +0 -16
  209. package/src/platforms/weex/compiler/modules/recycle-list/index.js +0 -60
  210. package/src/platforms/weex/compiler/modules/recycle-list/recycle-list.js +0 -50
  211. package/src/platforms/weex/compiler/modules/recycle-list/text.js +0 -23
  212. package/src/platforms/weex/compiler/modules/recycle-list/v-bind.js +0 -21
  213. package/src/platforms/weex/compiler/modules/recycle-list/v-for.js +0 -33
  214. package/src/platforms/weex/compiler/modules/recycle-list/v-if.js +0 -63
  215. package/src/platforms/weex/compiler/modules/recycle-list/v-on.js +0 -25
  216. package/src/platforms/weex/compiler/modules/recycle-list/v-once.js +0 -19
  217. package/src/platforms/weex/compiler/modules/style.js +0 -87
  218. package/src/platforms/weex/entry-compiler.js +0 -2
  219. package/src/platforms/weex/entry-framework.js +0 -187
  220. package/src/platforms/weex/entry-runtime-factory.js +0 -6
  221. package/src/platforms/weex/runtime/components/index.js +0 -9
  222. package/src/platforms/weex/runtime/components/richtext.js +0 -82
  223. package/src/platforms/weex/runtime/components/transition-group.js +0 -148
  224. package/src/platforms/weex/runtime/components/transition.js +0 -9
  225. package/src/platforms/weex/runtime/directives/index.js +0 -2
  226. package/src/platforms/weex/runtime/index.js +0 -42
  227. package/src/platforms/weex/runtime/modules/attrs.js +0 -44
  228. package/src/platforms/weex/runtime/modules/class.js +0 -76
  229. package/src/platforms/weex/runtime/modules/events.js +0 -54
  230. package/src/platforms/weex/runtime/modules/index.js +0 -13
  231. package/src/platforms/weex/runtime/modules/style.js +0 -84
  232. package/src/platforms/weex/runtime/modules/transition.js +0 -270
  233. package/src/platforms/weex/runtime/node-ops.js +0 -91
  234. package/src/platforms/weex/runtime/patch.js +0 -16
  235. package/src/platforms/weex/runtime/recycle-list/render-component-template.js +0 -34
  236. package/src/platforms/weex/runtime/recycle-list/virtual-component.js +0 -136
  237. package/src/platforms/weex/runtime/text-node.js +0 -9
  238. package/src/platforms/weex/util/element.js +0 -52
  239. package/src/platforms/weex/util/index.js +0 -40
  240. package/src/platforms/weex/util/parser.js +0 -60
  241. package/src/server/bundle-renderer/create-bundle-renderer.js +0 -151
  242. package/src/server/bundle-renderer/create-bundle-runner.js +0 -150
  243. package/src/server/bundle-renderer/source-map-support.js +0 -45
  244. package/src/server/create-basic-renderer.js +0 -37
  245. package/src/server/create-renderer.js +0 -152
  246. package/src/server/optimizing-compiler/codegen.js +0 -264
  247. package/src/server/optimizing-compiler/index.js +0 -20
  248. package/src/server/optimizing-compiler/modules.js +0 -124
  249. package/src/server/optimizing-compiler/optimizer.js +0 -141
  250. package/src/server/optimizing-compiler/runtime-helpers.js +0 -150
  251. package/src/server/render-context.js +0 -130
  252. package/src/server/render-stream.js +0 -95
  253. package/src/server/render.js +0 -437
  254. package/src/server/template-renderer/create-async-file-mapper.js +0 -57
  255. package/src/server/template-renderer/index.js +0 -277
  256. package/src/server/template-renderer/parse-template.js +0 -42
  257. package/src/server/template-renderer/template-stream.js +0 -82
  258. package/src/server/util.js +0 -18
  259. package/src/server/webpack-plugin/client.js +0 -65
  260. package/src/server/webpack-plugin/server.js +0 -66
  261. package/src/server/webpack-plugin/util.js +0 -34
  262. package/src/server/write.js +0 -50
@@ -0,0 +1,161 @@
1
+ import { Component } from 'types/component'
2
+ import type { SetupContext } from 'types/options'
3
+ import { def, invokeWithErrorHandling, isReserved, warn } from '../core/util'
4
+ import VNode from '../core/vdom/vnode'
5
+ import { bind, emptyObject, isFunction, isObject } from '../shared/util'
6
+ import { currentInstance, setCurrentInstance } from './currentInstance'
7
+ import { isRef } from './reactivity/ref'
8
+
9
+ export function initSetup(vm: Component) {
10
+ const options = vm.$options
11
+ const setup = options.setup
12
+ if (setup) {
13
+ const ctx = (vm._setupContext = createSetupContext(vm))
14
+
15
+ setCurrentInstance(vm)
16
+ const setupResult = invokeWithErrorHandling(
17
+ setup,
18
+ null,
19
+ [vm._props, ctx],
20
+ vm,
21
+ `setup`
22
+ )
23
+ setCurrentInstance()
24
+
25
+ if (isFunction(setupResult)) {
26
+ // render function
27
+ // @ts-ignore
28
+ options.render = setupResult
29
+ } else if (isObject(setupResult)) {
30
+ // bindings
31
+ if (__DEV__ && setupResult instanceof VNode) {
32
+ warn(
33
+ `setup() should not return VNodes directly - ` +
34
+ `return a render function instead.`
35
+ )
36
+ }
37
+ vm._setupState = setupResult
38
+ for (const key in setupResult) {
39
+ if (!isReserved(key)) {
40
+ proxySetupProperty(vm, setupResult, key)
41
+ }
42
+ }
43
+ } else if (__DEV__ && setupResult !== undefined) {
44
+ warn(
45
+ `setup() should return an object. Received: ${
46
+ setupResult === null ? 'null' : typeof setupResult
47
+ }`
48
+ )
49
+ }
50
+ }
51
+ }
52
+
53
+ function proxySetupProperty(
54
+ vm: Component,
55
+ setupResult: Record<string, any>,
56
+ key: string
57
+ ) {
58
+ const raw = setupResult[key]
59
+ const unwrap = isRef(raw)
60
+ Object.defineProperty(vm, key, {
61
+ enumerable: true,
62
+ configurable: true,
63
+ get: unwrap ? () => raw.value : () => setupResult[key],
64
+ set: unwrap ? v => (raw.value = v) : v => (setupResult[key] = v)
65
+ })
66
+ }
67
+
68
+ function createSetupContext(vm: Component) {
69
+ return {
70
+ get attrs() {
71
+ return initAttrsProxy(vm)
72
+ },
73
+ get slots() {
74
+ return initSlotsProxy(vm)
75
+ },
76
+ emit: bind(vm.$emit, vm) as any
77
+ }
78
+ }
79
+
80
+ function initAttrsProxy(vm: Component) {
81
+ if (!vm._attrsProxy) {
82
+ const proxy = (vm._attrsProxy = {})
83
+ def(proxy, '_v_attr_proxy', true)
84
+ syncSetupAttrs(proxy, vm.$attrs, emptyObject, vm)
85
+ }
86
+ return vm._attrsProxy
87
+ }
88
+
89
+ export function syncSetupAttrs(
90
+ to: any,
91
+ from: any,
92
+ prev: any,
93
+ instance: Component
94
+ ) {
95
+ let changed = false
96
+ for (const key in from) {
97
+ if (!(key in to)) {
98
+ changed = true
99
+ defineProxyAttr(to, key, instance)
100
+ } else if (from[key] !== prev[key]) {
101
+ changed = true
102
+ }
103
+ }
104
+ for (const key in to) {
105
+ if (!(key in from)) {
106
+ changed = true
107
+ delete to[key]
108
+ }
109
+ }
110
+ return changed
111
+ }
112
+
113
+ function defineProxyAttr(proxy: any, key: string, instance: Component) {
114
+ Object.defineProperty(proxy, key, {
115
+ enumerable: true,
116
+ configurable: true,
117
+ get() {
118
+ return instance.$attrs[key]
119
+ }
120
+ })
121
+ }
122
+
123
+ function initSlotsProxy(vm: Component) {
124
+ if (!vm._slotsProxy) {
125
+ syncSetupSlots((vm._slotsProxy = {}), vm.$scopedSlots)
126
+ }
127
+ return vm._slotsProxy
128
+ }
129
+
130
+ export function syncSetupSlots(to: any, from: any) {
131
+ for (const key in from) {
132
+ to[key] = from[key]
133
+ }
134
+ for (const key in to) {
135
+ if (!(key in from)) {
136
+ delete to[key]
137
+ }
138
+ }
139
+ }
140
+
141
+ /**
142
+ * @internal use manual type def
143
+ */
144
+ export function useSlots(): SetupContext['slots'] {
145
+ return getContext().slots
146
+ }
147
+
148
+ /**
149
+ * @internal use manual type def
150
+ */
151
+ export function useAttrs(): SetupContext['attrs'] {
152
+ return getContext().attrs
153
+ }
154
+
155
+ function getContext(): SetupContext {
156
+ if (__DEV__ && !currentInstance) {
157
+ warn(`useContext() called without active instance.`)
158
+ }
159
+ const vm = currentInstance!
160
+ return vm._setupContext || (vm._setupContext = createSetupContext(vm))
161
+ }
@@ -0,0 +1,355 @@
1
+ import { isRef, Ref } from './reactivity/ref'
2
+ import { ComputedRef } from './reactivity/computed'
3
+ import { isReactive, isShallow } from './reactivity/reactive'
4
+ import {
5
+ warn,
6
+ noop,
7
+ isArray,
8
+ isFunction,
9
+ emptyObject,
10
+ hasChanged,
11
+ isServerRendering,
12
+ invokeWithErrorHandling
13
+ } from 'core/util'
14
+ import { currentInstance } from './currentInstance'
15
+ import { traverse } from 'core/observer/traverse'
16
+ import Watcher from '../core/observer/watcher'
17
+ import { queueWatcher } from '../core/observer/scheduler'
18
+ import { DebuggerOptions } from './debug'
19
+
20
+ const WATCHER = `watcher`
21
+ const WATCHER_CB = `${WATCHER} callback`
22
+ const WATCHER_GETTER = `${WATCHER} getter`
23
+ const WATCHER_CLEANUP = `${WATCHER} cleanup`
24
+
25
+ export type WatchEffect = (onCleanup: OnCleanup) => void
26
+
27
+ export type WatchSource<T = any> = Ref<T> | ComputedRef<T> | (() => T)
28
+
29
+ export type WatchCallback<V = any, OV = any> = (
30
+ value: V,
31
+ oldValue: OV,
32
+ onCleanup: OnCleanup
33
+ ) => any
34
+
35
+ type MapSources<T, Immediate> = {
36
+ [K in keyof T]: T[K] extends WatchSource<infer V>
37
+ ? Immediate extends true
38
+ ? V | undefined
39
+ : V
40
+ : T[K] extends object
41
+ ? Immediate extends true
42
+ ? T[K] | undefined
43
+ : T[K]
44
+ : never
45
+ }
46
+
47
+ type OnCleanup = (cleanupFn: () => void) => void
48
+
49
+ export interface WatchOptionsBase extends DebuggerOptions {
50
+ flush?: 'pre' | 'post' | 'sync'
51
+ }
52
+
53
+ export interface WatchOptions<Immediate = boolean> extends WatchOptionsBase {
54
+ immediate?: Immediate
55
+ deep?: boolean
56
+ }
57
+
58
+ export type WatchStopHandle = () => void
59
+
60
+ // Simple effect.
61
+ export function watchEffect(
62
+ effect: WatchEffect,
63
+ options?: WatchOptionsBase
64
+ ): WatchStopHandle {
65
+ return doWatch(effect, null, options)
66
+ }
67
+
68
+ export function watchPostEffect(
69
+ effect: WatchEffect,
70
+ options?: DebuggerOptions
71
+ ) {
72
+ return doWatch(
73
+ effect,
74
+ null,
75
+ (__DEV__
76
+ ? { ...options, flush: 'post' }
77
+ : { flush: 'post' }) as WatchOptionsBase
78
+ )
79
+ }
80
+
81
+ export function watchSyncEffect(
82
+ effect: WatchEffect,
83
+ options?: DebuggerOptions
84
+ ) {
85
+ return doWatch(
86
+ effect,
87
+ null,
88
+ (__DEV__
89
+ ? { ...options, flush: 'sync' }
90
+ : { flush: 'sync' }) as WatchOptionsBase
91
+ )
92
+ }
93
+
94
+ // initial value for watchers to trigger on undefined initial values
95
+ const INITIAL_WATCHER_VALUE = {}
96
+
97
+ type MultiWatchSources = (WatchSource<unknown> | object)[]
98
+
99
+ // overload: array of multiple sources + cb
100
+ export function watch<
101
+ T extends MultiWatchSources,
102
+ Immediate extends Readonly<boolean> = false
103
+ >(
104
+ sources: [...T],
105
+ cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
106
+ options?: WatchOptions<Immediate>
107
+ ): WatchStopHandle
108
+
109
+ // overload: multiple sources w/ `as const`
110
+ // watch([foo, bar] as const, () => {})
111
+ // somehow [...T] breaks when the type is readonly
112
+ export function watch<
113
+ T extends Readonly<MultiWatchSources>,
114
+ Immediate extends Readonly<boolean> = false
115
+ >(
116
+ source: T,
117
+ cb: WatchCallback<MapSources<T, false>, MapSources<T, Immediate>>,
118
+ options?: WatchOptions<Immediate>
119
+ ): WatchStopHandle
120
+
121
+ // overload: single source + cb
122
+ export function watch<T, Immediate extends Readonly<boolean> = false>(
123
+ source: WatchSource<T>,
124
+ cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
125
+ options?: WatchOptions<Immediate>
126
+ ): WatchStopHandle
127
+
128
+ // overload: watching reactive object w/ cb
129
+ export function watch<
130
+ T extends object,
131
+ Immediate extends Readonly<boolean> = false
132
+ >(
133
+ source: T,
134
+ cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
135
+ options?: WatchOptions<Immediate>
136
+ ): WatchStopHandle
137
+
138
+ // implementation
139
+ export function watch<T = any, Immediate extends Readonly<boolean> = false>(
140
+ source: T | WatchSource<T>,
141
+ cb: any,
142
+ options?: WatchOptions<Immediate>
143
+ ): WatchStopHandle {
144
+ if (__DEV__ && typeof cb !== 'function') {
145
+ warn(
146
+ `\`watch(fn, options?)\` signature has been moved to a separate API. ` +
147
+ `Use \`watchEffect(fn, options?)\` instead. \`watch\` now only ` +
148
+ `supports \`watch(source, cb, options?) signature.`
149
+ )
150
+ }
151
+ return doWatch(source as any, cb, options)
152
+ }
153
+
154
+ function doWatch(
155
+ source: WatchSource | WatchSource[] | WatchEffect | object,
156
+ cb: WatchCallback | null,
157
+ {
158
+ immediate,
159
+ deep,
160
+ flush = 'pre',
161
+ onTrack,
162
+ onTrigger
163
+ }: WatchOptions = emptyObject
164
+ ): WatchStopHandle {
165
+ if (__DEV__ && !cb) {
166
+ if (immediate !== undefined) {
167
+ warn(
168
+ `watch() "immediate" option is only respected when using the ` +
169
+ `watch(source, callback, options?) signature.`
170
+ )
171
+ }
172
+ if (deep !== undefined) {
173
+ warn(
174
+ `watch() "deep" option is only respected when using the ` +
175
+ `watch(source, callback, options?) signature.`
176
+ )
177
+ }
178
+ }
179
+
180
+ const warnInvalidSource = (s: unknown) => {
181
+ warn(
182
+ `Invalid watch source: `,
183
+ s,
184
+ `A watch source can only be a getter/effect function, a ref, ` +
185
+ `a reactive object, or an array of these types.`
186
+ )
187
+ }
188
+
189
+ const instance = currentInstance
190
+ const call = (fn: Function, type: string, args: any[] | null = null) =>
191
+ invokeWithErrorHandling(fn, null, args, instance, type)
192
+
193
+ let getter: () => any
194
+ let forceTrigger = false
195
+ let isMultiSource = false
196
+
197
+ if (isRef(source)) {
198
+ getter = () => source.value
199
+ forceTrigger = isShallow(source)
200
+ } else if (isReactive(source)) {
201
+ getter = isArray(source)
202
+ ? () => {
203
+ ;(source as any).__ob__.dep.depend()
204
+ return source
205
+ }
206
+ : () => source
207
+ deep = true
208
+ } else if (isArray(source)) {
209
+ isMultiSource = true
210
+ forceTrigger = source.some(s => isReactive(s) || isShallow(s))
211
+ getter = () =>
212
+ source.map(s => {
213
+ if (isRef(s)) {
214
+ return s.value
215
+ } else if (isReactive(s)) {
216
+ return traverse(s)
217
+ } else if (isFunction(s)) {
218
+ return call(s, WATCHER_GETTER)
219
+ } else {
220
+ __DEV__ && warnInvalidSource(s)
221
+ }
222
+ })
223
+ } else if (isFunction(source)) {
224
+ if (cb) {
225
+ // getter with cb
226
+ getter = () => call(source as Function, WATCHER_GETTER)
227
+ } else {
228
+ // no cb -> simple effect
229
+ getter = () => {
230
+ if (instance && instance._isDestroyed) {
231
+ return
232
+ }
233
+ if (cleanup) {
234
+ cleanup()
235
+ }
236
+ return call(source as Function, WATCHER, [onCleanup])
237
+ }
238
+ }
239
+ } else {
240
+ getter = noop
241
+ __DEV__ && warnInvalidSource(source)
242
+ }
243
+
244
+ if (cb && deep) {
245
+ const baseGetter = getter
246
+ getter = () => traverse(baseGetter())
247
+ }
248
+
249
+ let cleanup: () => void
250
+ let onCleanup: OnCleanup = (fn: () => void) => {
251
+ cleanup = watcher.onStop = () => {
252
+ call(fn, WATCHER_CLEANUP)
253
+ }
254
+ }
255
+
256
+ // in SSR there is no need to setup an actual effect, and it should be noop
257
+ // unless it's eager
258
+ if (isServerRendering()) {
259
+ // we will also not call the invalidate callback (+ runner is not set up)
260
+ onCleanup = noop
261
+ if (!cb) {
262
+ getter()
263
+ } else if (immediate) {
264
+ call(cb, WATCHER_CB, [
265
+ getter(),
266
+ isMultiSource ? [] : undefined,
267
+ onCleanup
268
+ ])
269
+ }
270
+ return noop
271
+ }
272
+
273
+ const watcher = new Watcher(currentInstance, getter, noop, {
274
+ lazy: true
275
+ })
276
+ watcher.noRecurse = !cb
277
+
278
+ let oldValue = isMultiSource ? [] : INITIAL_WATCHER_VALUE
279
+ // overwrite default run
280
+ watcher.run = () => {
281
+ if (
282
+ !watcher.active &&
283
+ !(flush === 'pre' && instance && instance._isBeingDestroyed)
284
+ ) {
285
+ return
286
+ }
287
+ if (cb) {
288
+ // watch(source, cb)
289
+ const newValue = watcher.get()
290
+ if (
291
+ deep ||
292
+ forceTrigger ||
293
+ (isMultiSource
294
+ ? (newValue as any[]).some((v, i) =>
295
+ hasChanged(v, (oldValue as any[])[i])
296
+ )
297
+ : hasChanged(newValue, oldValue))
298
+ ) {
299
+ // cleanup before running cb again
300
+ if (cleanup) {
301
+ cleanup()
302
+ }
303
+ call(cb, WATCHER_CB, [
304
+ newValue,
305
+ // pass undefined as the old value when it's changed for the first time
306
+ oldValue === INITIAL_WATCHER_VALUE ? undefined : oldValue,
307
+ onCleanup
308
+ ])
309
+ oldValue = newValue
310
+ }
311
+ } else {
312
+ // watchEffect
313
+ watcher.get()
314
+ }
315
+ }
316
+
317
+ if (flush === 'sync') {
318
+ watcher.update = watcher.run
319
+ } else if (flush === 'post') {
320
+ watcher.id = Infinity
321
+ watcher.update = () => queueWatcher(watcher)
322
+ } else {
323
+ // pre
324
+ watcher.update = () => {
325
+ if (!instance || instance._isMounted) {
326
+ queueWatcher(watcher)
327
+ } else {
328
+ const buffer = instance._preWatchers || (instance._preWatchers = [])
329
+ if (buffer.indexOf(watcher) < 0) buffer.push(watcher)
330
+ }
331
+ }
332
+ }
333
+
334
+ if (__DEV__) {
335
+ watcher.onTrack = onTrack
336
+ watcher.onTrigger = onTrigger
337
+ }
338
+
339
+ // initial run
340
+ if (cb) {
341
+ if (immediate) {
342
+ watcher.run()
343
+ } else {
344
+ oldValue = watcher.get()
345
+ }
346
+ } else if (flush === 'post' && instance) {
347
+ instance.$once('hook:mounted', () => watcher.get())
348
+ } else {
349
+ watcher.get()
350
+ }
351
+
352
+ return () => {
353
+ watcher.teardown()
354
+ }
355
+ }
@@ -0,0 +1,23 @@
1
+ import { Component } from 'types/component'
2
+
3
+ export let currentInstance: Component | null = null
4
+
5
+ /**
6
+ * This is exposed for compatibility with v3 (e.g. some functions in VueUse
7
+ * relies on it). Do not use this internally, just use `currentInstance`.
8
+ *
9
+ * @internal this function needs manual type declaration because it relies
10
+ * on previously manually authored types from Vue 2
11
+ */
12
+ export function getCurrentInstance(): { proxy: Component } | null {
13
+ return currentInstance && { proxy: currentInstance }
14
+ }
15
+
16
+ /**
17
+ * @internal
18
+ */
19
+ export function setCurrentInstance(vm: Component | null = null) {
20
+ if (!vm) currentInstance && currentInstance._scope.off()
21
+ currentInstance = vm
22
+ vm && vm._scope.on()
23
+ }
@@ -0,0 +1,21 @@
1
+ import { TrackOpTypes, TriggerOpTypes } from './reactivity/operations'
2
+
3
+ export interface DebuggerOptions {
4
+ onTrack?: (event: DebuggerEvent) => void
5
+ onTrigger?: (event: DebuggerEvent) => void
6
+ }
7
+
8
+ export type DebuggerEvent = {
9
+ /**
10
+ * @internal
11
+ */
12
+ effect: any
13
+ } & DebuggerEventExtraInfo
14
+
15
+ export type DebuggerEventExtraInfo = {
16
+ target: object
17
+ type: TrackOpTypes | TriggerOpTypes
18
+ key?: any
19
+ newValue?: any
20
+ oldValue?: any
21
+ }
package/src/v3/h.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { createElement } from '../core/vdom/create-element'
2
+ import { currentInstance } from './currentInstance'
3
+ import { warn } from 'core/util'
4
+
5
+ /**
6
+ * @internal this function needs manual public type declaration because it relies
7
+ * on previously manually authored types from Vue 2
8
+ */
9
+ export function h(type: any, props?: any, children?: any) {
10
+ if (!currentInstance) {
11
+ __DEV__ &&
12
+ warn(
13
+ `globally imported h() can only be invoked when there is an active ` +
14
+ `component instance, e.g. synchronously in a component's render or setup function.`
15
+ )
16
+ }
17
+ return createElement(currentInstance!, type, props, children, 2, true)
18
+ }
@@ -0,0 +1,77 @@
1
+ export {
2
+ ref,
3
+ shallowRef,
4
+ isRef,
5
+ toRef,
6
+ toRefs,
7
+ unref,
8
+ customRef,
9
+ triggerRef,
10
+ Ref,
11
+ ToRef,
12
+ ToRefs,
13
+ UnwrapRef,
14
+ ShallowRef,
15
+ ShallowUnwrapRef,
16
+ RefUnwrapBailTypes,
17
+ CustomRefFactory
18
+ } from './reactivity/ref'
19
+
20
+ export {
21
+ reactive,
22
+ isReactive,
23
+ isReadonly,
24
+ isShallow,
25
+ isProxy,
26
+ shallowReactive,
27
+ markRaw,
28
+ toRaw,
29
+ ReactiveFlags,
30
+ ShallowReactive,
31
+ UnwrapNestedRefs
32
+ } from './reactivity/reactive'
33
+
34
+ export { readonly, shallowReadonly, DeepReadonly } from './reactivity/readonly'
35
+
36
+ export {
37
+ computed,
38
+ ComputedRef,
39
+ WritableComputedRef,
40
+ WritableComputedOptions,
41
+ ComputedGetter,
42
+ ComputedSetter
43
+ } from './reactivity/computed'
44
+
45
+ export {
46
+ watch,
47
+ watchEffect,
48
+ watchPostEffect,
49
+ watchSyncEffect,
50
+ WatchEffect,
51
+ WatchOptions,
52
+ WatchOptionsBase,
53
+ WatchCallback,
54
+ WatchSource,
55
+ WatchStopHandle
56
+ } from './apiWatch'
57
+
58
+ export {
59
+ EffectScope,
60
+ effectScope,
61
+ onScopeDispose,
62
+ getCurrentScope
63
+ } from './reactivity/effectScope'
64
+
65
+ export { DebuggerOptions, DebuggerEvent, DebuggerEventExtraInfo } from './debug'
66
+
67
+ export { TrackOpTypes, TriggerOpTypes } from './reactivity/operations'
68
+
69
+ export { provide, inject, InjectionKey } from './apiInject'
70
+
71
+ export { h } from './h'
72
+ export { getCurrentInstance } from './currentInstance'
73
+ export { useSlots, useAttrs } from './apiSetup'
74
+ export { nextTick } from 'core/util/next-tick'
75
+ export { set, del } from 'core/observer'
76
+
77
+ export * from './apiLifecycle'