Skip to content
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

Remove Subject.create, refactor WebSocketSubject #7381

Merged
merged 15 commits into from Nov 22, 2023

Conversation

benlesh
Copy link
Member

@benlesh benlesh commented Nov 20, 2023

An incremental refactor of WebSocketSubject to clean it up quite a bit. best reviewed per commit

Adds long-awaited type fix. WebSocketSubject can now take two type parameters that allow it to accept a different input type than it outputs:

const socket = webSocket<string, number>('wss://somesocketurl')

socket.next('string in');

socket.subscribe(n => {
  // number out.
})

Includes the following BREAKING CHANGES:

  1. WebSocketSubject is no longer instanceof Subject. It really didn't make any sense to do this, it didn't leverage anything from Subject to begin with.
  2. Subject.create, a long-deprecated API has been removed. If you recall, Subject.create(observer, observable) basically "glued" the observer to the observable.

This removes AnonymousSubject, which was never exported, and was overall weird.

BREAKING CHANGE: Removed the deprecated `Subject.create` method. If you need to create an object that is "half Observable, half Observer", you'll need to either bolt `next`, `error`, and `complete` handlers onto an `Observable` and property type the return... or you'll need to create your own class that is backed by an `Observable`. In any case, if the `Observer` and the `Observable` are so unrelated that you have to bolt them together, you're probably better off with those two objects separately. This is why `Subject.create` has been deprecated for so long.
BREAKING CHANGE: `WebSocketSubject` is no longer `instanceof Subject`. Check for `instanceof WebSocketSubject` instead.
@benlesh benlesh marked this pull request as ready for review November 20, 2023 22:58
Comment on lines +157 to +167
private _inputBuffer: In[] = [];

private _hasError = false;

private _error: any;

private _isComplete = false;

private _subscriberCounter = 0;

private _subscribers = new Map<number, Subscriber<Out>>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be real private fields without the ugly _ prefix instead of TS private?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I think overall, RxJS is due for an overhaul in this area. The upside is we can't accidentally publicly expose any #private properties. The downside is you can't destructure them. Which is mostly a superficial downside.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benlesh, I might try to do this.

@benlesh benlesh merged commit c408acd into ReactiveX:master Nov 22, 2023
3 checks passed
@benlesh
Copy link
Member Author

benlesh commented Nov 22, 2023

Thank you for the review @ssilve1989! ❤️

// Setting this here because a previous version of this allowed
// WebSocket to be polyfilled later than DEFAULT_WEBSOCKET_CONFIG
// was defined.
WebSocketCtor: WebSocket,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this throw an exception if the WebSocket is not available globally?

WebSocketCtor becomes almost useless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants