Skip to content

Commit 415311b

Browse files
committed
enhance custom decorators
1 parent 7bb7dfe commit 415311b

File tree

5 files changed

+34
-18
lines changed

5 files changed

+34
-18
lines changed

src/component.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { build as optionInject } from './option/inject'
1111
import { build as optionEmit } from './option/emit'
1212
import { build as optionVModel } from './option/vmodel'
1313
import { build as optionAccessor } from './option/accessor'
14-
import { CustomRecords } from './custom/custom'
1514
import type { SetupContext } from 'vue';
1615
import type { OptionBuilder } from './optionBuilder'
1716
import type { VueCons } from './index'
@@ -89,13 +88,6 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
8988
}
9089
option.emits = emits
9190

92-
93-
94-
CustomRecords.forEach(rec => {
95-
rec.creator.apply({}, [option, rec.key])
96-
})
97-
98-
9991
arg.setup ??= function () { return {} }
10092

10193
if (!option.setup) {
@@ -124,6 +116,10 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
124116
option.setup = setup
125117
}
126118

119+
slot.obtainMap('customDecorator').forEach((v) => {
120+
v.creator.apply({}, [option, v.key])
121+
})
122+
127123
if (arg.options) {
128124
Object.assign(option, arg.options)
129125
}

src/custom/custom.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import {compatibleMemberDecorator} from '../deco3/utils'
1+
import { obtainSlot } from '../utils'
2+
import { compatibleMemberDecorator } from '../deco3/utils'
23
type Creator = { (options: any, key: string): void }
3-
interface Record {
4+
export interface Record {
45
key: string
56
creator: Creator
7+
preserve: boolean
68
}
79

8-
// const CustomDecorators: CustomDecorator[] = []
9-
export const CustomRecords: Record[] = []
10-
11-
export function createDecorator(creator: Creator) {
10+
export function createDecorator(creator: Creator, opt?: {
11+
preserve?: boolean
12+
}) {
1213
return compatibleMemberDecorator(function (proto: any, key: string) {
13-
CustomRecords.push({
14+
const slot = obtainSlot(proto)
15+
const map = slot.obtainMap('customDecorator')
16+
map.set(key, {
1417
key,
15-
creator
18+
creator,
19+
preserve: !!opt?.preserve
1620
})
1721
})
1822
}

src/option/methodsAndHooks.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
4040
const HookFunctions: Record<string, Function> = {}
4141
const MethodFunctions: Record<string, Function> = {}
4242
protoArr.forEach(proto => {
43-
excludeNames(getValidNames(proto, (des, name) => {
43+
let names = getValidNames(proto, (des, name) => {
4444

4545
if (name === 'constructor') {
4646
return false
@@ -50,7 +50,9 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
5050
return true
5151
}
5252
return false
53-
}), slot).forEach(name => {
53+
})
54+
names = excludeNames(names, slot);
55+
names.forEach(name => {
5456
if (HookNames.includes(name as any) || map.has(name)) {
5557

5658
HookFunctions[name] = proto[name]

src/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { HookConfig } from "./option/methodsAndHooks";
77
import type { VModelConfig } from "./option/vmodel";
88
import type { WatchConfig } from "./option/watch";
99
import type { SetupConfig } from './option/setup'
10+
import type { Record as CustomDecoratorRecord } from './custom/custom'
1011
import { compatibleMemberDecorator } from './deco3/utils';
1112
const SlotSymbol = Symbol('vue-facing-decorator-slot')
1213

@@ -22,6 +23,7 @@ export type SlotMapTypes = {
2223
watch: Map<string, WatchConfig | WatchConfig[]>
2324
ref: Map<string, boolean>
2425
setup: Map<string, SetupConfig>
26+
customDecorator: Map<string, CustomDecoratorRecord>
2527
}
2628

2729
class Slot {
@@ -156,6 +158,16 @@ export function excludeNames(names: string[], slot: Slot) {
156158
if (['watch', 'hooks', 'setup', 'emits'].includes(mapName)) {
157159
continue
158160
}
161+
if (mapName === 'customDecorator') {
162+
const map = currSlot.obtainMap('customDecorator')
163+
if (map.has(name)) {
164+
if (!map.get(name)!.preserve) {
165+
return false
166+
}else{
167+
continue
168+
}
169+
}
170+
}
159171
const map = currSlot.names.get(mapName)!
160172
if (map.has(name)) {
161173
return false

test/custom/custom.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function CustomDeco(param?: String) {
1313
options.methods[key] = function (...args: any[]) {
1414
return `${old.apply(this, args)} ${param}`
1515
}
16+
},{
17+
preserve:true
1618
})
1719
}
1820

0 commit comments

Comments
 (0)