Skip to content

Commit ec111ae

Browse files
committed
reedsy-method-from-data-this
2 parents d360030 + 5897660 commit ec111ae

File tree

19 files changed

+216
-56
lines changed

19 files changed

+216
-56
lines changed

changelog/v3.0.1.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Feature
2+
13
1. Use class constructor name as default component name.
24

3-
2. `Ref` decorator accepts `key` paramater. (doc updated)
5+
2. `Ref` decorator accepts `key` paramater.
6+
7+
# Document Update
8+
9+
'@Ref', which accepts `key` paramater.

src/component.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defineComponent, type ComponentCustomOptions } from 'vue';
2-
import { obtainSlot, getSuperSlot } from './utils'
2+
import { obtainSlot, getSuperSlot, getProviderFunction } from './utils'
33
import { build as optionSetup } from './option/setup'
44
import { build as optionComputed } from './option/computed'
55
import { build as optionData } from './option/data'
@@ -8,6 +8,7 @@ import { build as optionRef } from './option/ref'
88
import { build as optionWatch } from './option/watch'
99
import { build as optionProps } from './option/props'
1010
import { build as optionInject } from './option/inject'
11+
import { build as optionProvide } from './option/provide'
1112
import { build as optionEmit } from './option/emit'
1213
import { build as optionVModel } from './option/vmodel'
1314
import { build as optionAccessor } from './option/accessor'
@@ -45,6 +46,10 @@ function ComponentOption(cons: Cons, extend?: any) {
4546
watch: optionBuilder.watch,
4647
props: optionBuilder.props,
4748
inject: optionBuilder.inject,
49+
provide() {
50+
optionProvide(cons, optionBuilder, this)
51+
return optionBuilder.provide ?? {}
52+
},
4853
...optionBuilder.hooks,
4954
extends: extend
5055
}
@@ -73,7 +78,7 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
7378
const option = ComponentOption(cons, extend)
7479
const slot = obtainSlot(cons.prototype)
7580
Object.keys(arg).reduce<Record<string, any>>((option, name: string) => {
76-
if (['options', 'modifier', 'emits', 'setup'].includes(name)) {
81+
if (['options', 'modifier', 'emits', 'setup', 'provide'].includes(name)) {
7782
return option
7883
}
7984
option[name] = arg[name as keyof ComponentOption]
@@ -113,6 +118,13 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
113118
option.setup = setup
114119
}
115120

121+
//merge provide function
122+
const oldProvider = getProviderFunction(option.provide)
123+
const newProvider = getProviderFunction(arg.provide)
124+
option.provide = function() {
125+
return Object.assign({}, oldProvider.call(this), newProvider.call(this))
126+
}
127+
116128
//custom decorator
117129
const map = slot.getMap('customDecorator')
118130
if (map && map.size > 0) {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export { decorator as Setup } from './option/setup'
33
export { decorator as Ref } from './option/ref'
44
export { decorator as Watch } from './option/watch'
55
export { decorator as Prop } from './option/props'
6+
export { decorator as Provide } from './option/provide'
67
export { decorator as Inject } from './option/inject'
78
export { decorator as Emit } from './option/emit'
89
export { decorator as VModel, decorator as Model } from './option/vmodel'
@@ -44,11 +45,10 @@ export const Base = class {
4445
(this as any)[key] = methods[key].bind(vueInstance)
4546
})
4647
}
47-
console.log('pp',this)
4848
}
4949

5050
} as VueCons
5151

5252
export const Vue = Base
5353

54-
export { toNative } from './component'
54+
export { toNative } from './component'

src/option/emit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Cons } from '../component'
22
import type { OptionBuilder } from '../optionBuilder'
3-
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
3+
import { obtainSlot, optionNullableMemberDecorator } from '../utils'
44
export type EmitConfig = null | string
55

6-
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, key?: string) {
6+
export const decorator = optionNullableMemberDecorator(function (proto: any, name: string, key?: string) {
77
const slot = obtainSlot(proto)
88
const map = slot.obtainMap('emit');
99
map.set(name, typeof key === 'undefined' ? null : key)

src/option/inject.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { InjectionKey } from 'vue'
22
import type { Cons } from '../component'
33
import type { OptionBuilder } from '../optionBuilder'
4-
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
5-
import { compatibleMemberDecorator } from '../deco3/utils'
4+
import { obtainSlot, optionNullableMemberDecorator } from '../utils'
5+
66
export interface InjectConfig {
77
from?: string | symbol | Symbol | InjectionKey<any>
88
default?: any
99
}
1010

11-
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: InjectConfig) {
11+
export const decorator = optionNullableMemberDecorator(function (proto: any, name: string, option?: InjectConfig) {
1212
const slot = obtainSlot(proto)
1313
const map = slot.obtainMap('inject')
1414
const opt = Object.assign({}, option ?? {})

src/option/methodsAndHooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Cons } from '../component'
22
import type { OptionBuilder } from '../optionBuilder'
3-
import { obtainSlot, toComponentReverse, excludeNames, getValidNames, optoinNullableMemberDecorator } from '../utils'
3+
import { obtainSlot, toComponentReverse, excludeNames, getValidNames, optionNullableMemberDecorator } from '../utils'
44

55
export const HookNames = [
66
"beforeCreate",
@@ -23,7 +23,7 @@ export const HookNames = [
2323
] as const
2424

2525
export type HookConfig = null
26-
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string) {
26+
export const decorator = optionNullableMemberDecorator(function (proto: any, name: string) {
2727
const slot = obtainSlot(proto)
2828
const map = slot.obtainMap('hooks');
2929
map.set(name, null)

src/option/props.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { Cons } from '../component'
22
import type { OptionBuilder } from '../optionBuilder'
3-
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
4-
import { compatibleMemberDecorator } from '../deco3/utils'
3+
import { obtainSlot, optionNullableMemberDecorator } from '../utils'
4+
55
export interface PropsConfig {
66
type?: any
77
required?: boolean
88
default?: any
99
validator?(value: any): boolean;
1010
}
1111

12-
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, option?: PropsConfig) {
12+
export const decorator = optionNullableMemberDecorator(function (proto: any, name: string, option?: PropsConfig) {
1313
const slot = obtainSlot(proto)
1414
const map = slot.obtainMap('props')
1515
const opt = Object.assign({}, option ?? {})

src/option/provide.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Cons } from '../component';
2+
import type { OptionBuilder } from '../optionBuilder'
3+
import { obtainSlot, optionNullableMemberDecorator } from '../utils'
4+
5+
export type ProvideConfig = null | string
6+
7+
export const decorator = optionNullableMemberDecorator(function (proto: any, name: string, key?: ProvideConfig) {
8+
const slot = obtainSlot(proto)
9+
const map = slot.obtainMap('provide')
10+
map.set(name, typeof key === 'undefined' ? null : key)
11+
})
12+
13+
export function build(cons: Cons, optionBuilder: OptionBuilder, vueInstance: any) {
14+
optionBuilder.provide ??= {}
15+
const sample = new cons(optionBuilder, vueInstance) as any
16+
const slot = obtainSlot(cons.prototype)
17+
const names = slot.obtainMap('provide')
18+
if (!names) return null
19+
names.forEach((value, name) => {
20+
const key = value === null ? name : value
21+
optionBuilder.provide![key] = sample[name]
22+
})
23+
}

src/option/ref.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { Cons } from '../component'
22
import { type OptionBuilder, applyAccessors } from '../optionBuilder'
3-
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
3+
import { obtainSlot, optionNullableMemberDecorator } from '../utils'
44

55
export type RefConfig = null | string
66

7-
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string, key?: string) {
7+
export const decorator = optionNullableMemberDecorator(function (proto: any, name: string, key?: string) {
88
const slot = obtainSlot(proto)
99
const map = slot.obtainMap('ref')
1010
map.set(name, typeof key === 'undefined' ? null : key)

src/option/vanilla.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { obtainSlot, optoinNullableMemberDecorator } from '../utils'
2-
import { compatibleMemberDecorator } from '../deco3/utils'
3-
export const decorator = optoinNullableMemberDecorator(function (proto: any, name: string) {
1+
import { obtainSlot, optionNullableMemberDecorator } from '../utils'
2+
3+
export const decorator = optionNullableMemberDecorator(function (proto: any, name: string) {
44
const slot = obtainSlot(proto)
55
const map = slot.obtainMap('vanilla')
66
map.set(name, true)

0 commit comments

Comments
 (0)