-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mapwatcher): support watch hook states and add mapWatcher helper
- Loading branch information
Showing
8 changed files
with
282 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { WatchOptionsWithHandler } from 'vue'; | ||
import { AlovaWatcherHandlers, VueWatchHandler } from '../typings'; | ||
import { isFn } from './helper'; | ||
|
||
const mapWatcher = (watcherHandlers: AlovaWatcherHandlers, withPrefix = true, parentKey = '') => { | ||
const handlerKeys = Object.keys(watcherHandlers || {}); | ||
let finalHandlers = {} as Record<string, VueWatchHandler>; | ||
handlerKeys.forEach(key => { | ||
const handlerNames = key.split(/\s*,\s*/); | ||
const handlerOrHandlers = watcherHandlers[key]; | ||
handlerNames.forEach(handlerName => { | ||
if (isFn(handlerOrHandlers) || isFn((handlerOrHandlers as WatchOptionsWithHandler<any>).handler)) { | ||
const prefix = withPrefix ? 'alovaHook$.' : ''; | ||
finalHandlers[prefix + parentKey + handlerName] = handlerOrHandlers as VueWatchHandler; | ||
} else { | ||
finalHandlers = { | ||
...finalHandlers, | ||
...mapWatcher(handlerOrHandlers as AlovaWatcherHandlers, withPrefix, handlerName + '.') | ||
}; | ||
} | ||
}); | ||
}); | ||
return finalHandlers; | ||
}; | ||
|
||
export default mapWatcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import mapWatcher from '../src/mapWatcher'; | ||
|
||
describe('mapWatcher', () => { | ||
test('single watch with handler', () => { | ||
const fn1 = () => {}, | ||
fn2 = () => {}; | ||
let watchers = mapWatcher({ | ||
testRequest: { | ||
loading: fn1, | ||
data: fn2 | ||
} | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': fn1, | ||
'alovaHook$.testRequest.data': fn2 | ||
}); | ||
|
||
watchers = mapWatcher({ | ||
'testRequest.loading': fn1, | ||
'testRequest.data': fn2 | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': fn1, | ||
'alovaHook$.testRequest.data': fn2 | ||
}); | ||
}); | ||
|
||
test('single watch with detail object', () => { | ||
const obj1 = { | ||
handler() {}, | ||
deep: true | ||
}, | ||
obj2 = { | ||
handler() {} | ||
}; | ||
let watchers = mapWatcher({ | ||
testRequest: { | ||
loading: obj1, | ||
data: obj2 | ||
} | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': obj1, | ||
'alovaHook$.testRequest.data': obj2 | ||
}); | ||
|
||
watchers = mapWatcher({ | ||
'testRequest.loading': obj1, | ||
'testRequest.data': obj2 | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': obj1, | ||
'alovaHook$.testRequest.data': obj2 | ||
}); | ||
}); | ||
|
||
test('batch watch', () => { | ||
const fn1 = () => {}, | ||
fn2 = () => {}; | ||
let watchers = mapWatcher({ | ||
'testRequest, testRequest2': { | ||
loading: fn1, | ||
data: fn2 | ||
} | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': fn1, | ||
'alovaHook$.testRequest.data': fn2, | ||
'alovaHook$.testRequest2.loading': fn1, | ||
'alovaHook$.testRequest2.data': fn2 | ||
}); | ||
|
||
watchers = mapWatcher({ | ||
'testRequest.loading, testRequest.data': fn1 | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': fn1, | ||
'alovaHook$.testRequest.data': fn1 | ||
}); | ||
|
||
watchers = mapWatcher({ | ||
'testRequest, testRequest2': { | ||
'loading, data': fn1 | ||
} | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': fn1, | ||
'alovaHook$.testRequest.data': fn1, | ||
'alovaHook$.testRequest2.loading': fn1, | ||
'alovaHook$.testRequest2.data': fn1 | ||
}); | ||
}); | ||
|
||
test('batch watch with detail oject', () => { | ||
const obj1 = { | ||
handler() {}, | ||
deep: true | ||
}, | ||
obj2 = { | ||
handler() {} | ||
}; | ||
let watchers = mapWatcher({ | ||
'testRequest, testRequest2': { | ||
loading: obj1, | ||
data: obj2 | ||
} | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': obj1, | ||
'alovaHook$.testRequest.data': obj2, | ||
'alovaHook$.testRequest2.loading': obj1, | ||
'alovaHook$.testRequest2.data': obj2 | ||
}); | ||
|
||
watchers = mapWatcher({ | ||
'testRequest.loading, testRequest.data': obj1 | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': obj1, | ||
'alovaHook$.testRequest.data': obj1 | ||
}); | ||
|
||
watchers = mapWatcher({ | ||
'testRequest, testRequest2': { | ||
'loading, data': obj1 | ||
} | ||
}); | ||
expect(watchers).toStrictEqual({ | ||
'alovaHook$.testRequest.loading': obj1, | ||
'alovaHook$.testRequest.data': obj1, | ||
'alovaHook$.testRequest2.loading': obj1, | ||
'alovaHook$.testRequest2.data': obj1 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters