-
Notifications
You must be signed in to change notification settings - Fork 3k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Statically typed fromEvent
#4891
Comments
@benlesh Is this any good? 🤔😅 |
I think this is really cool. I'm not sure I agree with all of the design choices, but I like the spirit of it. If we did have a design we agreed on, we'd need to have a migration plan. How do we get millions of users there gracefully over a series of major releases? What does the deprecation track look like? Are there code transformations we can do? Etc. RxJS is unfortunately way too popular to make big, breaking changes to API design without careful considerations like that. |
I'm so happy that you find it cool! :D And I really appreciate that you asked these questions instead of just dismissing the proposal. I think we can at least agree upon the fact that if we want type-safety Let's say we have an emitter Now imagine someone wrote The point I want to make is As a matter of fact I started working on type-safety keeping the API same but when I realised fromEvent also takes array of emitters then things became a little too much. That is when I thought let me roll my own fromEvent-like thing. And beyond all this what I like about Regarding other design disagreements, if you can be specific we can come to a common ground. Regarding migration, // 01 ---
// before:
fromEvent(emitter, name);
// after:
fromEmitter(emitter).event(name);
// 02 ---
// before:
fromEvent<T>(emitter, name);
// after:
fromEmitter(emitter).event(name) as Observable<T>;
// 03 ---
// before:
fromEvent(emitter, name, options);
// after:
fromEmitter(emitter).event(name, options);
// 04 ---
// before:
fromEvent<T>(emitter, name, options);
// after:
fromEmitter(emitter).event(name, options) as Observable<T>;
// 05 ---
// before:
fromEvent(arrayOfEmitters, name);
// after:
merge(...
arrayOfEmitters
.map(e => fromEmitter(e).event(name))
);
// 06 ---
// before:
fromEvent<T>(arrayOfEmitters, name);
// after:
merge(...
arrayOfEmitters
.map(e => fromEmitter(e).event(name))
) as Observable<T>;
// 07 ---
// before:
fromEvent(arrayOfEmitters, name, options);
// after:
merge(...
arrayOfEmitters
.map(e => fromEmitter(e).event(name, options))
);
// 08 ---
// before:
fromEvent<T>(arrayOfEmitters, name, options);
// after:
merge(...
arrayOfEmitters
.map(e => fromEmitter(e).event(name, options))
) as Observable<T>;
// 09 ---
// before:
fromEvent(arrayLikeOfEmitters, name);
// after:
merge(...
[...arrayLikeOfEmitters]
.map(e => fromEmitter(e).event(name))
);
// 10 ---
// before:
fromEvent<T>(arrayLikeOfEmitters, name);
// after:
merge(...
[...arrayLikeOfEmitters]
.map(e => fromEmitter(e).event(name))
) as Observable<T>;
// 11 ---
// before:
fromEvent(arrayLikeOfEmitters, name, options);
// after:
merge(...
[...arrayLikeOfEmitters]
.map(e => fromEmitter(e).event(name, options))
);
// 12 ---
// before:
fromEvent<T>(arrayLikeOfEmitters, name, options);
// after:
merge(...
[...arrayLikeOfEmitters]
.map(e => fromEmitter(e).event(name, options))
) as Observable<T>; This assumes that users are using In most cases users can and should remove const assertType =
<T>() =>
($: Observable<unknown>) =>
$ as Observable<T> Also if the I don't know how difficult it will be to make code transformers but I have a feeling it shouldn't be much hard in case of TypeScript. In case of JavaScript I guess we can't really tell if it's a single emitter or array of emitters with 100% accuracy. Also this refactoring will require spread version of So first step now is to agree upon the design and API. If the current design is good, I can explore writing code transformers. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
TLDR: I have implemented a
fromEvent
alternative that is type-safe and has some additional features. And I want to know if I can open a PR. :)So, as you people might know
fromEvent
's types are not really great which is fair considering when they were written it wasn't possible to statically type it. But now we can have better types forfromEvent
.So in this light, I implemented rxjs-from-emitter. I recommend checking out the readme to see all it's features, but in short these are it's two major features -
Observable
inferences corresponding to listener's arguments.If this implementation is in alignment with RxJS's goals and interests, I would be more than happy to send a PR :) I have answers to all your questions. Also don't get distracted with the change in API, it's the types that are important. To put it in the perspective runtime code is ~100 LOC where as type-level code is ~600 LOC.
Also additional to the type-safety, according to me there are some (respectfully) design-level flaws of
fromEvent
that rxjs-from-emitter solves which I have documented here.PS: In case this comes across a publicity stunt to promote my package or whatever, it's not. I love RxJS and I genuinely want to make it better :)
Also I will take this opportunity to thank everyone that constantly works on RxJS to make it better!
The text was updated successfully, but these errors were encountered: