Skip to content

Instantly share code, notes, and snippets.

@homam
Last active October 17, 2015 17:54
Show Gist options
  • Select an option

  • Save homam/eb4fc5c4b4412b6b696a to your computer and use it in GitHub Desktop.

Select an option

Save homam/eb4fc5c4b4412b6b696a to your computer and use it in GitHub Desktop.
Rx.Observable.prototype.bufferWithTimeHasty = function(interval, refresh){
var source;
source = this;
return Rx.Observable.create(function(observer){
var bundle, refereshInterval;
bundle = [];
refereshInterval = setInterval(function(){
var now, expiration, j, i;
now = Date.now();
expiration = now - interval;
j = null;
i = -1;
while (i < bundle.length - 1) {
if (bundle[++i][0] > expiration) {
j = i;
break;
}
}
if (j !== null) {
bundle = bundle.slice(j);
}
return observer.onNext(bundle.map(function(it){
return it[1];
}));
}, refresh);
return source.subscribe(function(data){
var now;
now = Date.now();
return bundle.push([now, data]);
}, observer.onError.bind(observer), function(){
clearInterval(refereshInterval);
return observer.onCompleted.apply(observer, arguments);
});
});
};
Rx.Observable.prototype.buffer-with-time-hasty = (interval, refresh) ->
source = this
Rx.Observable.create (observer) ->
bundle = []
referesh-interval = set-interval do
->
now = Date.now!
expiration = now - interval
j = null
i = -1
while i < bundle.length-1
if bundle[++i].0 > expiration
j := i
break
if j != null
bundle := bundle.slice j
observer.onNext (bundle |> map (.1))
refresh
source.subscribe do
(data) ->
now = Date.now!
bundle.push [now, data]
observer.onError.bind observer
->
clear-interval referesh-interval
observer.onCompleted.apply observer, arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment