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

[rstream] Subscription without last value #81

Closed
andrew8er opened this issue Apr 3, 2019 · 5 comments
Closed

[rstream] Subscription without last value #81

andrew8er opened this issue Apr 3, 2019 · 5 comments

Comments

@andrew8er
Copy link
Contributor

andrew8er commented Apr 3, 2019

I have a use case, where I do not want Subscription<A, B> to remember the last value. I just want to react to events as they happen. Currently I use this:

class AmnesticSubscriber<A, B> extends Subscription<A, B> {
	protected dispatch(x: B) {
		try {
			super.dispatch(x);
		}
		finally {
			this.last = SEMAPHORE;
		}
	}
}

But I do not think this is very elegant. How would this fit into @thi.ng/rstream?

Maybe related: #74

@andrew8er andrew8er changed the title [rstream] Subscription without last [rstream] Subscription without last value Apr 3, 2019
@postspectacular
Copy link
Member

Hey @andrew8er - so you want to not store it in general or just avoid that new subscribers receive the last value?

@andrew8er
Copy link
Contributor Author

For my particular use case, both approaches would solve the issue. Not storing the value would let the GC reclaim the passed value earlier if it is not referenced elsewhere.

@postspectacular
Copy link
Member

sorry for the delay @andrew8er - I've been thinking about this and no great obvious solution to disable this yet. Been thinking about introducing ctor options objects (e.g. as done for StreamSync) more widely to configure custom behaviors like this. In the interim, one other quick-and-not-so-dirty solution would be to move the this.last setter in dispatch() into its own method, which you could then override with a blank one.

In Subscription class:

protected dispatch(x: B) {
    this.setValue(x);
    ...
}

protected setValue(x: B) {
    this.last = x;
}

Then AmnesticSubscriber only needs to:

class AmnesticSubscriber<A, B> extends Subscription<A, B> {
	protected setValue(x: B) { }
}

@andrew8er
Copy link
Contributor Author

Ok, that would be slightly better, but I wonder if it really warrants the extra function call. Maybe I just wait for a more generic solution to this problem, I can live with mine right now.

@postspectacular
Copy link
Member

Yep, that's what I've been wondering too. Since dispatch is a hotspot, I'd really like to keep as barebones as possible...

Let's leave this issue open as mental note. Will create a new issue about potential config options soon...

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

No branches or pull requests

2 participants