mirror of
https://github.com/re-rxjs/react-rxjs.git
synced 2025-12-08 18:01:51 +00:00
feat(utils-createListener): add overload for shorthand listeners
This commit is contained in:
parent
45f32cc47a
commit
562890f16a
@ -14,7 +14,17 @@ describe("createListener", () => {
|
||||
onFooBar(0, "1")
|
||||
expect(receivedValue).toEqual({ foo: 0, bar: "1" })
|
||||
})
|
||||
it('returns a tuple with a void observable and its corresponding event-emitter when no "event creator" is provided', () => {
|
||||
it('returns a tuple with a typed observable and its corresponding event-emitter when no "event creator" is provided', () => {
|
||||
const [foo$, onFoo] = createListener<string>()
|
||||
let receivedValue
|
||||
foo$.subscribe((val) => {
|
||||
receivedValue = val
|
||||
})
|
||||
expect(receivedValue).toBe(undefined)
|
||||
onFoo("foo")
|
||||
expect(receivedValue).toEqual("foo")
|
||||
})
|
||||
it('returns a tuple with a void observable and its corresponding event-emitter when no "event creator" and no type is provided', () => {
|
||||
const [clicks$, onClick] = createListener()
|
||||
let count = 0
|
||||
clicks$.subscribe(() => {
|
||||
|
||||
@ -1,11 +1,14 @@
|
||||
import { Observable, Subject } from "rxjs"
|
||||
|
||||
const defaultMapper: any = () => {}
|
||||
const defaultMapper: any = (v: unknown) => v
|
||||
|
||||
export function createListener<A extends unknown[], T>(
|
||||
mapper: (...args: A) => T,
|
||||
): [Observable<T>, (...args: A) => void]
|
||||
export function createListener(): [Observable<void>, () => void]
|
||||
export function createListener<T = void>(): [
|
||||
Observable<T>,
|
||||
(payload: T) => void,
|
||||
]
|
||||
|
||||
export function createListener<A extends unknown[], T>(
|
||||
mapper: (...args: A) => T = defaultMapper,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user