Skip to content

Commit 562890f

Browse files
volivaVíctor Oliva
authored andcommitted
feat(utils-createListener): add overload for shorthand listeners
1 parent 45f32cc commit 562890f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/utils/src/createListener.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ describe("createListener", () => {
1414
onFooBar(0, "1")
1515
expect(receivedValue).toEqual({ foo: 0, bar: "1" })
1616
})
17-
it('returns a tuple with a void observable and its corresponding event-emitter when no "event creator" is provided', () => {
17+
it('returns a tuple with a typed observable and its corresponding event-emitter when no "event creator" is provided', () => {
18+
const [foo$, onFoo] = createListener<string>()
19+
let receivedValue
20+
foo$.subscribe((val) => {
21+
receivedValue = val
22+
})
23+
expect(receivedValue).toBe(undefined)
24+
onFoo("foo")
25+
expect(receivedValue).toEqual("foo")
26+
})
27+
it('returns a tuple with a void observable and its corresponding event-emitter when no "event creator" and no type is provided', () => {
1828
const [clicks$, onClick] = createListener()
1929
let count = 0
2030
clicks$.subscribe(() => {

packages/utils/src/createListener.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { Observable, Subject } from "rxjs"
22

3-
const defaultMapper: any = () => {}
3+
const defaultMapper: any = (v: unknown) => v
44

55
export function createListener<A extends unknown[], T>(
66
mapper: (...args: A) => T,
77
): [Observable<T>, (...args: A) => void]
8-
export function createListener(): [Observable<void>, () => void]
8+
export function createListener<T = void>(): [
9+
Observable<T>,
10+
(payload: T) => void,
11+
]
912

1013
export function createListener<A extends unknown[], T>(
1114
mapper: (...args: A) => T = defaultMapper,

0 commit comments

Comments
 (0)