Description
In RxJS, we can use fromEvent
to convert an EventEmitter
to an Observable
:
const message$ = fromEvent(wechaty.puppet, 'message')
However, fromEvent
does not use on()
for type infer, it uses addEventListener
and removeEventListener
.
So we need to add addEventListener
and removeEventListener
for support it.
Update 1
According to https://rxjs-dev.firebaseapp.com/api/index/function/fromEvent
fromEvent accepts as a first argument event target, which is an object with methods for registering event handler functions.
It turns out that it's not suitable for supporting it on Wechaty because wechaty listeners have variable numbers of the arguments.
Thinking about to support it at the puppet level.
Update 2
Move this issue from wechaty to wechaty-puppet
Update 3
Unit test:
The signature '(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string): Observable<EventScanPayload>' of 'fromEvent' is deprecated.ts(6387)
fromEvent.d.ts(36, 5): The declaration was marked as deprecated here.
(alias) fromEvent<EventScanPayload>(target: NodeStyleEventEmitter | ArrayLike<NodeStyleEventEmitter>, eventName: string): Observable<...> (+11 overloads)
import fromEvent
@deprecated — Do not specify explicit type parameters. Signatures with type parameters that cannot be inferred will be removed in v8.
Update 4 (Nov 114, 2021)
A typing of FromEvent
for typed-emitter
has been published and it works as expected: