c# - Window Operator does not work with itself? -
i have bit of code expect work in way, , doesn't, wondering doing wrong :
class program { static void main(string[] args) { var ints = observable.interval(timespan.frommilliseconds(1000)); var windowed = ints.window(() => ints.select(i => / 3).distinctuntilchanged()); windowed.subscribe(handlenewwindow); console.readline(); } public static void handlenewwindow(iobservable<long> ints) { console.writeline("new sequence received"); ints.subscribe(console.writeline); } }
output should :
new sequence received
0
1
2
new sequence received
3
4
5
new sequence received
6
7
8
...
but :
new sequence received
0
new sequence received
1
new sequence received
2
new sequence received
3
new sequence received
4
new sequence received
5
new sequence received
6
...
note if use different line define window, such :
var windowed = ints.window(() => observable.interval(timespan.frommilliseconds(3000)));
then works fine.
does window have problem using window closings derived observable windowing, or missing important here ?
you need use publish
operator create observable who's subscriptions source can shared. looks every time window closed internally sets new subscription source. using publish ensures not starting new interval every time
you need change window close selector fire when want window closed.
class program { static void main(string[] args) { var ints = observable.interval(timespan.frommilliseconds(1000)) .publish(new subject<long>()); var closeonvalues = ints.where(shouldclose); var windowed = ints.window(() => closeonvalues); windowed.subscribe(handlenewwindow); console.readline(); } public static void handlenewwindow(iobservable<long> ints) { console.writeline("new sequence received"); ints.subscribe(console.writeline); } public static bool shouldclose(long index) { var notzero = index != 0; var countismultipleofthree = (index + 1) % 3 == 0; return notzero && countismultipleofthree; } }
Comments
Post a Comment