feat(utils-createListener): add overload for shorthand listeners

This commit is contained in:
Víctor Oliva 2020-09-23 10:19:14 +02:00 committed by Víctor Oliva
parent 45f32cc47a
commit 562890f16a
2 changed files with 16 additions and 3 deletions

View File

@ -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(() => {

View File

@ -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,