Skip to content

Commit 3802858

Browse files
committed
refactoring some code and improve performance
1 parent 20b4b95 commit 3802858

File tree

15 files changed

+146
-163
lines changed

15 files changed

+146
-163
lines changed

src/component.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,10 @@ function ComponentOption(cons: Cons, extend?: any) {
3030
optionInject(cons, optionBuilder)
3131
optionEmit(cons, optionBuilder)
3232
optionRef(cons, optionBuilder)//after Computed
33-
optionMethodsAndHooks(cons, optionBuilder)//after Ref Computed
3433
optionAccessor(cons, optionBuilder)
35-
36-
37-
const setupFunction: OptionSetupFunction | undefined = optionBuilder.setup ? function (props, ctx) {
38-
return optionBuilder.setup!(props, ctx)
39-
} : undefined
40-
34+
optionMethodsAndHooks(cons, optionBuilder)//the last one
4135
const raw = {
42-
setup: setupFunction,
36+
setup: optionBuilder.setup,
4337
data() {
4438
delete optionBuilder.data
4539
optionData(cons, optionBuilder, this)
@@ -71,7 +65,9 @@ type ComponentOption = {
7165
mixins?: any[]
7266
setup?: ComponentSetupFunction
7367
}
68+
7469
type ComponentConsOption = Cons | ComponentOption
70+
7571
function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
7672
const option = ComponentOption(cons, extend)
7773
const slot = obtainSlot(cons.prototype)
@@ -82,21 +78,21 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
8278
option[name] = arg[name as keyof ComponentOption]
8379
return option
8480
}, option)
81+
82+
//apply event emits
8583
let emits = Array.from(slot.obtainMap('emits').keys())
8684
if (Array.isArray(arg.emits)) {
8785
emits = Array.from(new Set([...emits, ...arg.emits]))
8886
}
8987
option.emits = emits
9088

91-
arg.setup ??= function () { return {} }
92-
89+
//merge setup function
9390
if (!option.setup) {
94-
9591
option.setup = arg.setup
9692
} else {
9793

9894
const oldSetup: OptionSetupFunction = option.setup
99-
const newSetup: ComponentSetupFunction = arg.setup
95+
const newSetup: ComponentSetupFunction = arg.setup ?? function () { return {} }
10096

10197
const setup: ComponentSetupFunction = function (props, ctx) {
10298
const newRet = newSetup(props, ctx)
@@ -116,16 +112,24 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
116112
option.setup = setup
117113
}
118114

119-
slot.obtainMap('customDecorator').forEach((v) => {
120-
v.creator.apply({}, [option, v.key])
121-
})
115+
//custom decorator
116+
const map = slot.getMap('customDecorator')
117+
if (map && map.size > 0) {
118+
map.forEach((v) => {
119+
v.creator.apply({}, [option, v.key])
120+
})
121+
}
122122

123+
//shallow merge options
123124
if (arg.options) {
124125
Object.assign(option, arg.options)
125126
}
127+
128+
//apply modifier
126129
if (arg.modifier) {
127130
arg.modifier(option)
128131
}
132+
129133
return defineComponent(option)
130134
}
131135
function build(cons: Cons, option: ComponentOption) {

src/index-return-cons.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// This feature is deprecated and is preserved for compatibility
12
export * from './index'

src/option/accessor.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import type { Cons } from '../component'
2-
import { type OptionBuilder, applyAccessors} from '../optionBuilder'
2+
import { type OptionBuilder, applyAccessors } from '../optionBuilder'
33
import { toComponentReverse, obtainSlot } from '../utils'
44

55
export function build(cons: Cons, optionBuilder: OptionBuilder) {
66
const slot = obtainSlot(cons.prototype)
7-
const vanillaMap = slot.obtainMap('vanilla')
7+
const vanillaMap = slot.getMap('vanilla')
8+
if (!vanillaMap || vanillaMap.size === 0) {
9+
return
10+
}
811
const protoArr = toComponentReverse(cons.prototype)
9-
const map: Map<string, { get: (() => any) | undefined, set: ((v: any) => any) | undefined }> = new Map
12+
const map: Map<string, { get: (() => any) | undefined, set: ((v: any) => any) | undefined }> | undefined = new Map
1013

11-
applyAccessors(optionBuilder,(ctx:any)=>{
14+
applyAccessors(optionBuilder, (ctx: any) => {
1215
protoArr.forEach(proto => {
1316
const deses = Object.getOwnPropertyDescriptors(proto)
1417
for (const name in deses) {

src/option/data.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@ import type { Cons } from '../component'
22
import type { OptionBuilder } from '../optionBuilder'
33
import { makeObject, obtainSlot, excludeNames, getValidNames } from '../utils'
44

5-
export function build(cons: Cons, optionBuilder: OptionBuilder, vueInstance: any, _propNames?: string[]) {
5+
export function build(cons: Cons, optionBuilder: OptionBuilder, vueInstance: any) {
66
optionBuilder.data ??= {}
7-
8-
9-
10-
117
const sample = new cons(optionBuilder,vueInstance)
12-
138
let names = getValidNames(sample, (des) => {
149
return !!des.enumerable
1510
})
16-
17-
1811
const slot = obtainSlot(cons.prototype)
1912
names = excludeNames(names, slot)
2013
Object.assign(optionBuilder.data, makeObject(names, sample))

src/option/emit.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Cons } from '../component'
22
import type { OptionBuilder } from '../optionBuilder'
33
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
4-
import { compatibleMemberDecorator } from '../deco3/utils'
54
export type EmitConfig = null | string
65

76
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, key?: string) {
@@ -14,25 +13,25 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
1413
optionBuilder.methods ??= {}
1514
const proto = cons.prototype
1615
const slot = obtainSlot(proto)
17-
const names = slot.obtainMap('emit')
16+
const names = slot.getMap('emit')
17+
if (!names || names.size === 0) {
18+
return
19+
}
1820
const emits = slot.obtainMap('emits')
21+
names.forEach((value, key) => {
22+
const eventName = value === null ? key : value
23+
emits.set(eventName, true)
24+
optionBuilder.methods![key] = async function (this: any) {
1925

20-
if (names) {
21-
names.forEach((value, key) => {
22-
const eventName = value === null ? key : value
23-
emits.set(eventName, true)
24-
optionBuilder.methods![key] = async function (this: any) {
25-
26-
const ret = proto[key].apply(this, arguments)
27-
if (ret instanceof Promise) {
28-
const proRet = await ret
29-
this.$emit(eventName, proRet)
30-
}
31-
else {
32-
this.$emit(eventName, ret)
33-
}
26+
const ret = proto[key].apply(this, arguments)
27+
if (ret instanceof Promise) {
28+
const proRet = await ret
29+
this.$emit(eventName, proRet)
3430
}
35-
})
36-
}
31+
else {
32+
this.$emit(eventName, ret)
33+
}
34+
}
35+
})
3736

3837
}

src/option/inject.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ export const decorator = optoinNullableMemberDecorator(function (proto: any, nam
1919
export function build(cons: Cons, optionBuilder: OptionBuilder) {
2020
optionBuilder.inject ??= {}
2121
const slot = obtainSlot(cons.prototype)
22-
const names = slot.obtainMap('inject')
23-
if (names) {
24-
names.forEach((value, name) => {
25-
optionBuilder.inject![name] = value
26-
})
22+
const names = slot.getMap('inject')
23+
if (!names || names.size === 0) {
24+
return
2725
}
2826

27+
names.forEach((value, name) => {
28+
optionBuilder.inject![name] = value
29+
})
30+
2931
}
3032

3133

src/option/methodsAndHooks.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
5151
}
5252
return false
5353
})
54-
names = excludeNames(names, slot);
54+
names = excludeNames(names, slot, (mapName) => {
55+
//include these names:
56+
//watch, user may call watch method directly
57+
//hooks, user may call hook method directly
58+
//emits, user may have a method name which is same as one of event names
59+
return !['watch', 'hooks', 'emits'].includes(mapName)
60+
});
5561
names.forEach(name => {
5662
if (HookNames.includes(name as any) || map.has(name)) {
5763

@@ -66,13 +72,13 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
6672
})
6773

6874
Object.assign(optionBuilder.methods, MethodFunctions)
69-
const bccbs = optionBuilder.beforeCreateCallbacks
70-
if (bccbs && bccbs.length > 0) {
71-
const oldBc = HookFunctions['beforeCreate']
75+
const beforeCreateCallbacks = [...optionBuilder.beforeCreateCallbacks??[]]
76+
if (beforeCreateCallbacks && beforeCreateCallbacks.length > 0) {
77+
const oldBeforeCreateCallback = HookFunctions['beforeCreate']
7278
HookFunctions['beforeCreate'] = function () {
73-
bccbs.forEach(bccb => bccb.apply(this, arguments))
74-
if (oldBc) {
75-
oldBc.apply(this, arguments)
79+
beforeCreateCallbacks.forEach(callback => callback.apply(this, arguments))
80+
if (oldBeforeCreateCallback) {
81+
oldBeforeCreateCallback.apply(this, arguments)
7682
}
7783
}
7884
}

src/option/props.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ export const decorator = optoinNullableMemberDecorator(function (proto: any, nam
1919
export function build(cons: Cons, optionBuilder: OptionBuilder) {
2020
optionBuilder.props ??= {}
2121
const slot = obtainSlot(cons.prototype)
22-
const names = slot.obtainMap('props')
23-
24-
if (names) {
25-
names.forEach((value, name) => {
26-
optionBuilder.props![name] = value
27-
})
22+
const names = slot.getMap('props')
23+
if (!names || names.size === 0) {
24+
return
2825
}
2926

27+
names.forEach((value, name) => {
28+
optionBuilder.props![name] = value
29+
})
30+
31+
3032
}
3133

3234

src/option/ref.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ export const decorator = optoinNullableMemberDecorator(function (proto: any, nam
1111

1212
export function build(cons: Cons, optionBuilder: OptionBuilder) {
1313
const slot = obtainSlot(cons.prototype)
14-
const names = slot.obtainMap('ref')!
15-
if (names) {
16-
applyAccessors(optionBuilder, (ctx: any) => {
17-
const data: Map<string, { get: () => any, set: undefined }> = new Map
18-
names.forEach((value, name) => {
19-
data.set(name, {
20-
get: function (this: any) {
21-
return ctx.$refs[name]
22-
},
23-
set: undefined
24-
})
14+
const names = slot.getMap('ref')
15+
if (!names || names.size === 0) {
16+
return
17+
}
18+
19+
applyAccessors(optionBuilder, (ctx: any) => {
20+
const data: Map<string, { get: () => any, set: undefined }> = new Map
21+
names.forEach((value, name) => {
22+
data.set(name, {
23+
get: function (this: any) {
24+
return ctx.$refs[name]
25+
},
26+
set: undefined
2527
})
26-
return data
2728
})
28-
}
29+
return data
30+
})
31+
2932
}

src/option/setup.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { compatibleMemberDecorator} from '../deco3/utils'
1+
import { compatibleMemberDecorator } from '../deco3/utils'
22
import type { OptionSetupFunction } from '../component'
33
import type { Cons } from '../component'
44
import type { OptionBuilder } from '../optionBuilder'
@@ -19,16 +19,15 @@ export function decorator(setupFunction: OptionSetupFunction) {
1919
}
2020

2121
const isPromise = (v: any): v is Promise<any> => v instanceof Promise
22-
//(v: any) => typeof v === 'object' && typeof v.then === 'function'
2322

2423
export function build(cons: Cons, optionBuilder: OptionBuilder) {
2524
const slot = obtainSlot(cons.prototype)
26-
const map = slot.obtainMap('setup')
27-
if (map.size === 0) {
25+
const map = slot.getMap('setup')
26+
if (!map || map.size === 0) {
2827
return
2928
}
3029
const setup: OptionSetupFunction = function (props, ctx) {
31-
30+
3231
const setupData: Record<string, any> = {};
3332
let promises: Promise<any>[] | null = null;
3433
for (const name of map.keys()) {

0 commit comments

Comments
 (0)