Nested streams not closing
See original GitHub issueIt seems as though streams which are nested using flatMap
are not closed out correctly. Code sample:
var most = require('most'),
jQuery = require('jquery'),
socket = require('sockjs')('/api/ws'),
client = require('stomp').over(socket),
connect = jQuery.Deferred();
client.connect({}, function() {
connect.resolve();
});
function getResource(resource, headers) {
return most.fromPromise(connect).flatMap(function() {
return getSnapshot(resource, headers).flatMap(function(data) {
return getUpdates(resource, data, headers);
});
});
}
function getSnapshot(resource, headers) {
return stream('/resource/' + resource, headers)
.take(1);
}
function getUpdates(resource, data, headers) {
return stream('/topic/' + resource, headers)
.startWith([])
.scan(patch, data);
}
function patch(data, patch) {
return jiff.patch(patch, data);
}
function stream(destination, headers) {
return most.create(function(add) {
var subscription = client.subscribe(destination, function(msg) {
add(JSON.parse(msg.body));
}, headers);
return subscription.unsubscribe.bind(subscription);
})
}
When running code such as:
getResource('resource').take(1).observe(console.log.bind(console));
I expect both streams which are created (getSnapshot
and getUpdates
) to be disposed of. Currently only the stream generated by getSnapshot
is disposed of correctly while the stream generated by getUpdates
remains un-disposed.
Issue Analytics
- State:
- Created 9 years ago
- Comments:43 (27 by maintainers)
Top Results From Across the Web
Correct way to close nested streams and writers in Java
When closing chained streams, you only need to close the outermost stream. Any errors will be propagated up the chain and be caught....
Read more >Do you have to close all streams of a nested set of streams?
So, one only has to call close on one stream in order to close (and flush, if applicable) an entire series of related...
Read more >Right way to Close InputStream and OutputStream in Java
Right way of closing stream is by closing them in their own try catch block, so that failure of closing one stream should...
Read more >Thinking in nested streams with RxJS - Rangle.io
Working with nested data structures. One of my experiments with RxJS was traversing directories of files on my computer and returning a single ......
Read more >Working with nested JSON using ksqlDB - Confluent Developer
... from a stream of Kafka records that are contained in nested JSON using ksqlDB, ... Start Docker if it's not already running,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep, there was a one-character typo in
takeWhile
that broke dispose. Fixed in 0395555, updated the PR.Hmmm, I’m not seeing a delay in my test. The dispose function returned from the
create
callback is invoked very quickly. Quick question: What exactly do you mean by “closing the inner stream”? All most.js can do is call the disposer function (which it seems to be doing quickly in my test) … maybe stompjs’s unsubscribe function takes some time to complete? Is there any more info you can provide on the delay (maybe a timestamped log of some sort) that might help us track it down?One thing that may help is if we think of
join
differently (and more correctly). Mechanically speaking, inside the imperative implementation of most.js, there are inner and outer streams being managed by flatMap.Mathematically speaking however, there are not: the stream named
join
is a first-order stream containing the string “Inner” at time t = 11 seconds. Any higher-order-ness has been prevented byflatMap
.I think it comes down to what is the larger goal. I’m still not quite sure I have a handle on that, but I’ll try to answer the next question as best I can.
In the code exactly as it is in the gist right now, there is no way to emit the string “Inner” in 1 second. because “Inner” doesn’t even exist until time t = 11 seconds. If inner immediately emitted “Inner” without setTimeout, it would appear on the console at t = 1 second. Afaict, removing the
setTimeout
is the only way (barring time travel, of course! 😃 )Another option would be to use
map
instead offlatMap
. That will, however, emit theinner
stream itself (ie the JavaScript object) as an event injoin
after 1 second, at which pointouter
will end and be disposed, and theinner
stream will never have been started. That doesn’t seem like what you’re after, though.