npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rxjs-from-emitter

v0.0.9

Published

A statically typed, more powerful alternative to RxJS's `fromEvent`

Downloads

6

Readme

rxjs-from-emitter

A statically typed, more powerful alternative to RxJS's fromEvent.

$ npm i rxjs-from-emitter

Also there's proposal (rxjs #4891) to put this in RxJS itself

Features & Comparison with RxJS's fromEvent

In the following examples, by "error" I mean compile-time static errors not runtime. Also examples work for all kinds of event emitters not just DOM's EventTarget or node's EventEmitter.

Observable inferences corresponding to listener's arguments

fromEvent(document.body, "click") // Observable<Event>
fromEmitter(document.body).event("click") // Observable<MouseEvent>

fromEvent(spawn("echo", ["hello"]), "exit") // Observable<unknown>
fromEmitter(spawn("echo", ["hello"])).event("exit") // Observable<[number | null, string | null]>

const myEmitter = new class {
    on(
        name: "event-1",
        listener: (arg1: "something", arg2: number) => void
    ): void
    on(
        name: "event-2",
        listener: (arg1: "onlyOneArgumentSoNoArray") => void
    ): void
    on(name: string, listener: (...args: any[]) => any  ) {}

    off(name: string, listener: Function) {}
}

fromEvent(myEmitter, "event-1"); // Observable<unknown>
fromEvent(myEmitter, "event-2"); // Observable<unknown>
// in strict mode both of them will give unwarrant errors

fromEmitter(myEmitter).event("event-1"); // Observable<["something", number]>
fromEmitter(myEmitter).event("event-2"); // Observable<"onlyOneArgumentSoNoArray">

Error on invalid event identifiers

fromEmitter(document.body).event("foo");
// allowed because you can do document.body.dispatch(new Event("foo"))

fromEmitter(document.body).eventStrict("foo"); // error
// not allowed in strict version which takes only literals defined in the type.

fromEmitter(process).event("foo");
fromEmitter(process).eventStrict("foo")
// error in both because `process.addListener("foo", () => {})` gives error

fromEvent(document.body, "foo") // no error
fromEvent(process, "foo") // no error

Extras arguments taken into consideration

const myEmitter = new class {
    on(
        name: "event-1",
        listener: (arg1: "something", arg2: number) => void,
        emitInterval: number
    ) {}

    off(name: "event-1", listener: (arg1: "something", arg2: number) => void) {}
}

fromEmitter(myEmitter).event("event-1"); // error: Expected 2 arguments, but got 1.
fromEmitter(myEmitter).event("event-1", 1000); // no error, Observable<["something", number]>

fromEvent(myEmitter, "event-1") // no error

Support for custom method names without any compromise on types

const myEmitter = new class {
    anotherMethod() { }
    aProperty = "hello";

    register(name: "event-1", listener: (arg1: "something", arg2: number) => void) {}
    unregister(name: "event-1", listener: (arg1: "something", arg2: number) => void) {}
}

fromEmitter(myEmitter).event("event-1") // error couldn't identify methods

fromEmitter(myEmitter)
.withMethods("register", "unregister")
.event("event-1");
// ok, Observable<["something", number]>

fromEmitter(myEmitter)
.withMethods("register", "anotherMethod")
.event("event-1");
// error `anotherMethod` doesn't satisfy the required type

fromEmitter(myEmitter)
.withMethods("register", "register")
.event("event-1");
// error, can't pass the same methods.

const io = require("socket.io")();

fromEmitter(io).event("connect")
// error because couldn't find "off" method

fromEmitter(io)
.withMethods("on", null)
.event("connect");
// ok, Observable<Socket>

// fromEvent supports non of this, you'll have to use fromEventPattern

Doesn't assume the event identifiers type

const myEmitter = new class {
    on(
        event: 0,
        listener: (arg1: "something", arg2: number) => void
    ): void
    on(
        event: { type: "foo" },
        listener: (arg1: "onlyOneArgumentSoNoArray") => void
    ): void
    on(name: string, listener: (...args: any[]) => any  ) {}

    off(name: any, listener: Function) {}
}

fromEmitter(myEmitter).event(0) // no error, Observable<["something", number]>
fromEmitter(myEmitter).event({ type: "foo" }) // no error, Observable<"onlyOneArgumentSoNoArray">

fromEvent(myEmitter, 0) // error

Type-level code, but no hacks

There is a lot of TypeScript type-level code involved. As a matter of fact type-level code is almost 6 times more than the runtime code. But there is no hackish stuff like recursive types that drain CPU & RAM and are also not recommend to be used by the TypeScript folks. They can also break in future as they broke in past. rxjs-from-emitter has none of those.

Auto-completion, better DX

You get auto-completion for event identifiers, and also since the Observable is correctly inferred you get auto-completions because of that too.

Auto-completion, better DX

Don't like the API? You can still have some features using fromEvent and fromEventStrict exported from rxjs-from-emitter/compat. It's not fully compatible with RxJS's fromEvent though

import { fromEvent } from "rxjs-from-emitter/compat"

fromEvent(process, "exit" as "exit"); // Observable<number>

It also solves some *cough* flaws *cough* of fromEvent

What do you lose?

fromEvent can take array of event emitters, but fromEmitter takes only one. This is to keep static cheking easier and avoid complexity

Tested with TS 3.5 with strict mode, probably won't work for previous versions. For previous versions you can still try writing "click" as "click" instead of just "click", it might work.

Also document.querySelector returns Element, make sure to make it HTMLElement either via assertion (cast) or via type parameter