Skip to content

Commit 8efac6e

Browse files
committed
watcher array
1 parent 331e27d commit 8efac6e

File tree

5 files changed

+79
-17
lines changed

5 files changed

+79
-17
lines changed

docs/en/class-component/watcher/code-usage.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ Vue options API
1717
1818
}
1919
}
20+
property2:[{
21+
handler:function property2Watcher1(newValue: string, oldValue: string){
22+
23+
}
24+
},{
25+
handler:function property2Watcher2(newValue: string, oldValue: string){
26+
27+
}
28+
}]
2029
}
2130
}
2231
*/
@@ -29,4 +38,13 @@ export default class MyComponent extends Vue {
2938
propertyWatcher(newValue: string, oldValue: string) {
3039

3140
}
41+
42+
@Watch("property2")
43+
property2Watcher1(newValue: string, oldValue: string) {
44+
45+
}
46+
@Watch("property2")
47+
property2Watcher2(newValue: string, oldValue: string) {
48+
49+
}
3250
}

docs/zh-cn/class-component/watcher/code-usage.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ Vue options API
1717
1818
}
1919
}
20+
property2:[{
21+
handler:function property2Watcher1(newValue: string, oldValue: string){
22+
23+
}
24+
},{
25+
handler:function property2Watcher2(newValue: string, oldValue: string){
26+
27+
}
28+
}]
2029
}
2130
}
2231
*/
@@ -29,4 +38,13 @@ export default class MyComponent extends Vue {
2938
propertyWatcher(newValue: string, oldValue: string) {
3039

3140
}
41+
42+
@Watch("property2")
43+
property2Watcher1(newValue: string, oldValue: string) {
44+
45+
}
46+
@Watch("property2")
47+
property2Watcher2(newValue: string, oldValue: string) {
48+
49+
}
3250
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-facing-decorator",
3-
"version": "2.1.6",
3+
"version": "2.1.7",
44
"description": "Vue typescript class and decorator based component.",
55
"main": "dist/index.js",
66
"keywords": [

src/option/watch.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,34 @@ export function decorator(key: string, option?: Option) {
1313

1414
return function (proto: any, name: string) {
1515
const slot = obtainSlot(proto)
16-
let map = slot.obtainMap<Map<string, WatchConfig>>('watch');
16+
let map = slot.obtainMap<Map<string, WatchConfig | WatchConfig[]>>('watch');
1717

1818

1919
const opt = Object.assign({}, option ?? {}, {
2020
key: key,
2121
handler: proto[name]
2222
})
23-
map.set(name, opt)
23+
if (map.has(key)) {
24+
const t = map.get(key)!
25+
if (Array.isArray(t)) {
26+
t.push(opt)
27+
} else {
28+
map.set(key, [t, opt])
29+
}
30+
}
31+
else {
32+
map.set(key, opt)
33+
}
2434
}
2535
}
2636

2737
export function build(cons: Cons, optionBuilder: OptionBuilder) {
2838
optionBuilder.watch ??= {}
2939
const slot = obtainSlot(cons.prototype)
30-
const names = slot.obtainMap('watch')
31-
if (names) {
32-
names.forEach(value => {
33-
optionBuilder.watch![value.key] = value
40+
const keys = slot.obtainMap('watch')
41+
if (keys) {
42+
keys.forEach((value, key) => {
43+
optionBuilder.watch![key] = value
3444
})
3545
}
3646

test/option/watch.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,31 @@ import { Component, Watch, Base } from '../../dist'
55

66
@Component
77
export class Comp extends Base {
8-
8+
99
@Watch('defaultWatchKey')
10-
defaultWatcher(){
10+
defaultWatcher() {
1111
return 'defaultWatcher test value'
1212
}
1313

14-
@Watch('fullWatchKey',{
15-
deep:true,
16-
immediate:true,
17-
flush:'post'
14+
@Watch('fullWatchKey', {
15+
deep: true,
16+
immediate: true,
17+
flush: 'post'
1818
})
19-
fullWatcher(){
19+
20+
fullWatcher() {
2021
return 'fullWatcher test value'
2122
}
23+
@Watch('arrayWatchKey')
24+
arrayWatcher1() {
25+
return 'arrayWatcher1 value'
26+
}
27+
@Watch('arrayWatchKey', {
28+
immediate: true
29+
})
30+
arrayWatcher2() {
31+
return 'arrayWatcher2 value'
32+
}
2233
}
2334
const CompContext = Comp as any
2435

@@ -27,16 +38,21 @@ describe('decorator Watch',
2738
it('default', () => {
2839
expect('function').to.equal(typeof CompContext?.watch?.defaultWatchKey?.handler)
2940
expect('defaultWatcher test value').to.equal(CompContext.watch.defaultWatchKey.handler())
30-
41+
3142
})
32-
it('full option',()=>{
43+
it('full option', () => {
3344
expect('function').to.equal(typeof CompContext?.watch?.fullWatchKey?.handler)
3445
expect('fullWatcher test value').to.equal(CompContext.watch.fullWatchKey.handler())
3546
expect(true).to.equal(CompContext.watch.fullWatchKey.deep)
3647
expect(true).to.equal(CompContext.watch.fullWatchKey.immediate)
3748
expect('post').to.equal(CompContext.watch.fullWatchKey.flush)
3849
})
39-
it('method',()=>{
50+
it('array', () => {
51+
expect(true).to.equal(Array.isArray(CompContext?.watch?.arrayWatchKey))
52+
expect('arrayWatcher1 value').to.equal(CompContext.watch.arrayWatchKey[0].handler())
53+
expect(true).to.equal(CompContext.watch.arrayWatchKey[1].immediate)
54+
})
55+
it('method', () => {
4056
expect('function').to.equal(typeof CompContext?.methods?.defaultWatcher)
4157
expect('defaultWatcher test value').to.equal(CompContext.methods.defaultWatcher())
4258
})

0 commit comments

Comments
 (0)