-
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
Proposal: attack and release times for refCount #171
Comments
I actually have a use case for this as well, involving WebSockets. It seems like it should almost be a different operator/method. If I understand your proposal correctly, you want to add an optional temporal nature to A totally different angle is maybe there is a use case for a |
It wouldn't change the nature of the operator, because |
Yet another thing to consider is: by having a temporal nature, we are implicitly assuming the setTimeout scheduler. Maybe would make sense to let this scheduler independent by doing something like Let The ConnectableObservable connects when combineLatest(numObservers$, gate$, (num, gate) => ({num, gate}))
.filter(({num, gate}) => num > 0 && gate); emits combineLatest(numObservers$, gate$, (num, gate) => ({num, gate}))
.filter(({num, gate}) => num === 0 && !gate); emits Not sure if this is too generic to be practical. Just tossed ideas. |
I like the idea of using another Observable (which can obviously be scheduled) to trigger: someObservable.publish().refCountCompleteWhen(completions => completions.delay(200)) Where Functionality like this would be enough for me to use inside of my AnonymousSubject I use to create the WebSocket subject. So a new type of Subject (as I was suggesting before) probably isn't necessary. |
Was talking with some folks around here about it, and this would be possible to do with current operators if RxJS had a |
|
@staltz I've never liked I've thought about overloading the const numbers = Observable
.range(0, Number.POSITIVE_INFINITY)
.map((x) => Math.random())
.publish();
numbers
.zip(numbers.skip(1), (x, y) => x * y)
.subscribe(::console.log);
numbers.connect(function connectionStrategy(source, subject) {
var connection = null;
var hasConnected = false;
return Observable.create(function(observer) {
var subscription = subject.subscribe(observer);
if(subject.observers.length === 2 && !hasConnected) {
connection = source.subscribe(subject);
}
return function() {
subscription.unsubscribe();
if(subject.observers.length === 0) {
hasConnected = false;
connection.unsubscribe();
connection = null;
}
}
})
}); |
@trxcllnt ... I like your proposal. I'd be interested in what RxJava folks like @benjchristensen would think about it. Just to get a different perspective. |
numbers.connect(function connectionStrategy(source, subject) {
var connection = null;
var hasConnected = false;
return Observable.create(function(observer) {
var subscription = subject.subscribe(observer);
if(subject.observers.length === 2 && !hasConnected) {
connection = source.subscribe(subject);
}
return function() {
subscription.unsubscribe();
if(subject.observers.length === 0) {
hasConnected = false;
connection.unsubscribe();
connection = null;
}
}
})
}); Good idea. |
@staltz @Blesh @benjchristensen note, |
Yes, got it. |
I feel like there's a reason why this hasn't been done and we're not seeing it... cc/@jhusain and @mattpodwysocki for input. |
Yeah, maybe we need to question the underlying assumption ("why do you need a hot behaving like that? do you even need a hot?"). Summoning @headinthebox also. |
Related to this is a new I'm less convinced of involving time in a 1st class overload of |
@benjchristensen ... I'm more interested in what you think of @trxcllnt's proposal. I find it really interesting. The ability to specify a strategy for subscribing and unsubscribing from the source and the observable in it's entirety is pretty nice. |
Yeah, I'm looking into how to make variants of |
I would really like to see Paul's new |
@staltz @trxcllnt I really like it... we probably need to bike-shed the name a little. The other thing that is up for debate with this is whether or not to leave the current |
@benjchristensen @Blesh @staltz If we're bike-shedding, I think |
Sounds good to me |
@trxcllnt, can you create an issue for your |
Hi guys, I need that but it's unclear to me how your discussion ended up. What is the resulting operator with Paul's connection stretegies? |
@marinho I didn't get around to implementing a generic controlling operator, but you can definitely achieve any behavior you like with |
@trxcllnt could you please elaborate on what you meant by
perhaps by providing a small example? |
source
.multicast(() => new Subject())
.let((source) => {
let connection, subscriptions = new Subscription();
return Observable.using(
() => {
const localSubscription = new Subscription(() => {
subscriptions.remove(localSubscription);
if (connection && subscriptions.length === 3 /* <-- disconnect when the subscriber count drops to 2 (+1 for the connection subscription) */) {
subscriptions.unsubscribe();
subscriptions = new Subscription();
connection = null;
}
});
subscriptions.add(localSubscription);
if (!connection && subscriptions.length === 5 /*<- connect on the 5th subscription */) {
subscriptions.add(connection = source.connect());
}
return localSubscription;
},
() => source
);
}) |
@trxcllnt Thanks a lot for that! Really insightful. So I finally got round to trying it out, and it seems like there is no |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This is a non-breaking change proposal to extend refCount() with two optional parameters,
attackTime
andreleaseTime
.refCount(attack?: number, release?: number)
The motivation: I have discovered a use case where I need a connectable observable to get connected, but not get disconnected/reconnected if the number of observers went from 1 to 0 then from 0 to 1 synchronously (in the same event loop). In the link above, I just used connect() without disconnecting it, which is bad for memory management. I can imagine other use cases where I'd like to delay the auto-disconnect implicit in the refCount.
For symmetry with
release
, I also includedattack
times in case that makes sense in some case.From the Rx.NET gitter chat Lee Campbell also mentioned the existence of a LazyConnect operator they have which would be
refCount(0, Infinity)
("connect when first observer subscribes, but never disconnect").What do you think? This is definitely an advanced feature that most people don't need to use, but as a silent addition I think it's neat to avoid the imperative
connect()
whenever naiverefCount()
isn't enough.The text was updated successfully, but these errors were encountered: