Comparing version 2.4.0 to 2.5.0
@@ -29,2 +29,4 @@ 'use strict'; | ||
if (typeof filter === 'string' && filter !== type) return; | ||
if (Array.isArray(filter) && !filter.includes(type)) return; | ||
if (filter instanceof RegExp && !filter.test(type)) return; | ||
if (typeof filter === 'function' && !filter(...args)) return; | ||
@@ -31,0 +33,0 @@ callback(...args); |
@@ -24,2 +24,4 @@ import { useEffect } from 'react'; | ||
if (typeof filter === 'string' && filter !== type) return; | ||
if (Array.isArray(filter) && !filter.includes(type)) return; | ||
if (filter instanceof RegExp && !filter.test(type)) return; | ||
if (typeof filter === 'function' && !filter(...args)) return; | ||
@@ -26,0 +28,0 @@ callback(...args); |
@@ -78,2 +78,80 @@ /* eslint-env jest */ | ||
done(); | ||
}); | ||
it('should register an array and call the callback', done => { | ||
let hitCount = 0; | ||
let missCount = 0; | ||
const { | ||
unmount: unmountHit | ||
} = renderHook(() => useBus(['foo', 'bar'], event => { | ||
hitCount += 1; | ||
expect(event).toEqual({ | ||
channel: 'ui', | ||
type: expect.stringMatching(/^(foo|bar)$/), | ||
payload: 'some payload' | ||
}); | ||
})); | ||
const { | ||
unmount: unmountMiss | ||
} = renderHook(() => useBus(['baz'], () => { | ||
missCount += 1; | ||
})); | ||
dispatch({ | ||
channel: 'ui', | ||
type: 'foo', | ||
payload: 'some payload' | ||
}); | ||
dispatch({ | ||
channel: 'ui', | ||
type: 'bar', | ||
payload: 'some payload' | ||
}); | ||
dispatch({ | ||
channel: 'ui', | ||
type: 'baz', | ||
payload: 'some payload' | ||
}); | ||
unmountHit(); | ||
unmountMiss(); | ||
expect(hitCount).toEqual(2); | ||
expect(missCount).toEqual(1); | ||
done(); | ||
}); | ||
it('should register a RegExp and call the callback', done => { | ||
let hitCount = 0; | ||
let missCount = 0; | ||
const { | ||
unmount: unmountHit | ||
} = renderHook(() => useBus(/^b/, event => { | ||
hitCount += 1; | ||
expect(event).toEqual({ | ||
channel: 'ui', | ||
type: expect.stringMatching(/^(bar|baz)$/), | ||
payload: 'some payload' | ||
}); | ||
})); | ||
const { | ||
unmount: unmountMiss | ||
} = renderHook(() => useBus(/^f/, () => { | ||
missCount += 1; | ||
})); | ||
dispatch({ | ||
channel: 'ui', | ||
type: 'foo', | ||
payload: 'some payload' | ||
}); | ||
dispatch({ | ||
channel: 'ui', | ||
type: 'bar', | ||
payload: 'some payload' | ||
}); | ||
dispatch({ | ||
channel: 'ui', | ||
type: 'baz', | ||
payload: 'some payload' | ||
}); | ||
unmountHit(); | ||
unmountMiss(); | ||
expect(hitCount).toEqual(2); | ||
expect(missCount).toEqual(1); | ||
done(); | ||
}); |
@@ -11,3 +11,5 @@ declare module "use-bus" { | ||
export default function useBus(name: string, callback: (event: EventAction) => void, deps: any[]): typeof dispatch; | ||
export default function useBus(name: string[], callback: (event: EventAction) => void, deps: any[]): typeof dispatch; | ||
export default function useBus(name: RegExp, callback: (event: EventAction) => void, deps: any[]): typeof dispatch; | ||
export default function useBus(filter: (event: EventAction) => boolean, callback: (event: EventAction) => void, deps: any[]): typeof dispatch; | ||
} |
@@ -25,2 +25,4 @@ import { useEffect } from 'react'; | ||
if (typeof filter === 'string' && filter !== type) return; | ||
if (Array.isArray(filter) && !filter.includes(type)) return; | ||
if (filter instanceof RegExp && !filter.test(type)) return; | ||
if (typeof filter === 'function' && !filter(...args)) return; | ||
@@ -27,0 +29,0 @@ callback(...args); |
{ | ||
"name": "use-bus", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -16,4 +16,6 @@ # use-bus | ||
- `useBus(filter, callback, deps)`: register the given `callback` to the given `filter` | ||
* `filter`: it can be a string or a function | ||
* `filter`: it can be a string, array of strings, RegExp or a function | ||
- `string`: if filter is a string, then the action type is test over this given string, **if the filter match the type, the callback is called** | ||
- `string[]`: **if the filter array includes the type, the callback is called** | ||
- `RegExp`: **if the filter expression matches the type, the callback is called** | ||
- `function`: **the callback is called if the function returns a truthy value** | ||
@@ -20,0 +22,0 @@ * `callback`: take the action as the first argument so you can retrieve its type and its payload for example |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
18722
266
94