Skip to content

Add Typing support for RxJS fromEvent #96

Open
@huan

Description

@huan

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.

See: https://github.com/ReactiveX/rxjs/blob/49304ffef8d7a0663c57fe8e673359a602e9d3e1/src/internal/observable/fromEvent.ts#L27-L30

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.

https://github.com/wechaty/wechaty-puppet/blob/d7f069e108d79236c65a1cd956800716d2df38fc/tests/from-event-type.spec.ts#L43-L44

Update 4 (Nov 114, 2021)

A typing of FromEvent for typed-emitter has been published and it works as expected:

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions