types(runtime-core): refactor defineComponent (#1883)

This commit is contained in:
Carlos Rodrigues
2020-09-15 16:46:11 +01:00
committed by GitHub
parent 848ccf56fb
commit 4fd468aced
8 changed files with 583 additions and 132 deletions

View File

@@ -32,17 +32,17 @@ const readonlyGet = /*#__PURE__*/ createGetter(true)
const shallowReadonlyGet = /*#__PURE__*/ createGetter(true, true)
const arrayInstrumentations: Record<string, Function> = {}
;['includes', 'indexOf', 'lastIndexOf'].forEach(key => {
arrayInstrumentations[key] = function(...args: any[]): any {
const arr = toRaw(this) as any
for (let i = 0, l = (this as any).length; i < l; i++) {
;(['includes', 'indexOf', 'lastIndexOf'] as const).forEach(key => {
arrayInstrumentations[key] = function(this: unknown[], ...args: unknown[]) {
const arr = toRaw(this)
for (let i = 0, l = this.length; i < l; i++) {
track(arr, TrackOpTypes.GET, i + '')
}
// we run the method using the original args first (which may be reactive)
const res = arr[key](...args)
const res = (arr[key] as any)(...args)
if (res === -1 || res === false) {
// if that didn't work, run it again using raw values.
return arr[key](...args.map(toRaw))
return (arr[key] as any)(...args.map(toRaw))
} else {
return res
}