Skip to content

Commit

Permalink
fix: complete support handler types in function mapWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
JOU-amjs committed Nov 23, 2023
1 parent 2498a0a commit a8cea0b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/mapWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WatchOptionsWithHandler } from 'vue';
import { AlovaWatcherHandlers, VueWatchHandler } from '../typings';
import { isFn } from './helper';
import { isFn, isPlainObject } from './helper';

const mapWatcher = (watcherHandlers: AlovaWatcherHandlers, withPrefix = true, parentKey = '') => {
const handlerKeys = Object.keys(watcherHandlers || {});
Expand All @@ -9,7 +9,7 @@ const mapWatcher = (watcherHandlers: AlovaWatcherHandlers, withPrefix = true, pa
const handlerNames = key.split(/\s*,\s*/);
const handlerOrHandlers = watcherHandlers[key];
handlerNames.forEach(handlerName => {
if (isFn(handlerOrHandlers) || isFn((handlerOrHandlers as WatchOptionsWithHandler<any>).handler)) {
if (!isPlainObject(handlerOrHandlers) || isFn((handlerOrHandlers as WatchOptionsWithHandler<any>).handler)) {
const prefix = withPrefix ? 'alovaHook$.' : '';
finalHandlers[prefix + parentKey + handlerName] = handlerOrHandlers as VueWatchHandler;
} else {
Expand Down
28 changes: 20 additions & 8 deletions test/mapWatcher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mapWatcher from '../src/mapWatcher';
import { mapWatcher } from '../src';

describe('mapWatcher', () => {
test('single watch with handler', () => {
Expand All @@ -7,12 +7,16 @@ describe('mapWatcher', () => {
let watchers = mapWatcher({
testRequest: {
loading: fn1,
data: fn2
data: fn2,
error: 'methodName1',
data2: [fn1, fn2, 'methodName2']
}
});
expect(watchers).toStrictEqual({
'alovaHook$.testRequest.loading': fn1,
'alovaHook$.testRequest.data': fn2
'alovaHook$.testRequest.data': fn2,
'alovaHook$.testRequest.error': 'methodName1',
'alovaHook$.testRequest.data2': [fn1, fn2, 'methodName2']
});

watchers = mapWatcher({
Expand All @@ -36,12 +40,14 @@ describe('mapWatcher', () => {
let watchers = mapWatcher({
testRequest: {
loading: obj1,
data: obj2
data: obj2,
error: [obj1, obj2]
}
});
expect(watchers).toStrictEqual({
'alovaHook$.testRequest.loading': obj1,
'alovaHook$.testRequest.data': obj2
'alovaHook$.testRequest.data': obj2,
'alovaHook$.testRequest.error': [obj1, obj2]
});

watchers = mapWatcher({
Expand All @@ -60,14 +66,20 @@ describe('mapWatcher', () => {
let watchers = mapWatcher({
'testRequest, testRequest2': {
loading: fn1,
data: fn2
data: fn2,
error: 'methodName1',
data2: [fn1, fn2, 'methodName2']
}
});
expect(watchers).toStrictEqual({
'alovaHook$.testRequest.loading': fn1,
'alovaHook$.testRequest.data': fn2,
'alovaHook$.testRequest2.loading': fn1,
'alovaHook$.testRequest2.data': fn2
'alovaHook$.testRequest2.data': fn2,
'alovaHook$.testRequest.error': 'methodName1',
'alovaHook$.testRequest2.error': 'methodName1',
'alovaHook$.testRequest.data2': [fn1, fn2, 'methodName2'],
'alovaHook$.testRequest2.data2': [fn1, fn2, 'methodName2']
});

watchers = mapWatcher({
Expand All @@ -91,7 +103,7 @@ describe('mapWatcher', () => {
});
});

test('batch watch with detail oject', () => {
test('batch watch with detail object', () => {
const obj1 = {
handler() {},
deep: true
Expand Down
5 changes: 4 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ interface VueHookMapperMixin<GR extends UseHookCallers> {
*/
declare function mapAlovaHook<GR extends UseHookCallers>(mapGetter: UseHookMapGetter<GR>): VueHookMapperMixin<GR>[];

type VueWatchHandler = WatchOptionsWithHandler<any> | WatchHandler<any>;
type VueWatchHandler =
| WatchOptionsWithHandler<any>
| WatchHandler<any>
| Array<WatchOptionsWithHandler<any> | WatchHandler<any>>;
type AlovaWatcherHandlers = Record<string, VueWatchHandler | Record<string, VueWatchHandler>>;

/**
Expand Down

0 comments on commit a8cea0b

Please sign in to comment.