-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Merge Observer and Subscription Types? #75
Comments
Why not. Historically they came from IDisposable, which was used by itself, but that is not the case here. |
Based on the parts of the discussion in #71, I think this is a solid idea. Can I get more opinions, @trxcllnt? @vsavkin? @staltz? The proposal is this... the two interfaces: interface Observer {
next(value: any): void
error(err: any): void
complete(value: any): void
subscribe(subscription: Subscription)
}
interface Subscription {
unsubscribe(): void
isUnsubscribed: boolean
add(subscription: Subscription)
remove(subscription: Subscription)
} be implemented/combined on a single class: interface Subscriber {
next(value: any): void
error(err: any): void
complete(value: any): void
subscribe()
unsubscribe(): void
isUnsubscribed: boolean
add(subscription: Subscription)
remove(subscription: Subscription)
} Notice the |
I'd also love @zenparsing's input, since they are bike shedding the related things in the es-observable spec. |
+1, but call it |
RxJS Next meeting notes |
How would this single interface work to support the use case of Observers in the lift operator? interface Subscriber {
next(value: any): void // <---- notice the void return
error(err: any): void
complete(value: any): void
subscribe()
unsubscribe(): void
isUnsubscribed: boolean
add(subscription: Subscription)
remove(subscription: Subscription)
} The map example from issue #60 class Observable {
constructor(subscribe) {
if(subscribe) {
this.subscribe = subscribe;
}
}
subscribe(observer) {
return this.source.subscribe(this.observerFactory.create(observer));
}
lift(observerFactory) {
const o = new Observable();
o.source = this;
o.observerFactory = observerFactory;
return o;
}
map(selector) {
return this.lift(new MapObserverFactory(selector));
}
}
class MapObserverFactory {
constructor(selector) {
this.selector = selector;
}
create(destination) {
return new MapObserver(destination, this.selector);
}
}
class MapObserver {
constructor(destination, selector) {
this.selector = selector;
this.destination = destination;
}
next(x) {
return this.destination.next(this.selector(x)); // <--- notice, not a void return
}
throw(e) {
return this.destination.throw(e);
}
return(e) {
return this.destination.return(e);
}
} |
To clarify a bit: I find it weird when I see an Observer returning a value in |
Well, it's it's not really a single interface, it's a single class that implements two interfaces. A
@staltz ... void return is a void return. It's the result of an absence of a return. Just TypeScript semantics. FWIW: the ES7 Observable spec is bike shedding an interface here that shows |
@Blesh I know 🌝 |
Done in |
Based on discussions in #69, #70 and (mostly) #71.
Should the interfaces of Observer and Subscription be joined into a single type? Right now, generally, Subscriptions are created 1:1 with (safe) Observers. They tend to have to "know" about each other or at least notify one another to update state such as
isUnsubscribed
. And there might be some wins to joining them into a single class.This would also definitely affect the
lift
method, and ObserverFactories would have to become SubscriptionFactories or the like, I suppose.Pros? Cons? Good idea? Horrible idea?
The text was updated successfully, but these errors were encountered: