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

Support options { leading, trailing } in throttle #714

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions dist/Bacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ var Bacon = {
CompositeUnsubscribe: CompositeUnsubscribe,
never: never,
constant: constant,
version: '2.0.5'
version: '<version>'
};

Bacon.Bacon = Bacon;
Expand Down Expand Up @@ -3489,12 +3489,53 @@ Observable.prototype.takeWhile = function (f) {
});
};

var defaultOptions = { trailing: true };

Observable.prototype.throttle = function (delay) {
return this.delayChanges(new Desc(this, "throttle", [delay]), function (changes) {
return changes.bufferWithTime(delay).map(function (values) {
return values[values.length - 1];
});
});
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOptions;
var leading = options.leading,
trailing = options.trailing;

var lastCallTime = 0;
var trailingValue, timeoutId, trailingEnd;
var cancelTrailing = function () {
if (timeoutId !== null) {
Bacon.scheduler.clearTimeout(timeoutId);
timeoutId = null;
}
};
var stream = void 0;
var trailingCall = function () {
stream.dispatcher.push(trailingValue);
timeoutId = null;
trailingValue = null;
lastCallTime = Bacon.scheduler.now();
if (trailingEnd) {
stream.dispatcher.push(trailingEnd);
}
};
return stream = withDesc(new Desc(this, "throttle", options === defaultOptions ? [delay] : [delay, options]), this.withHandler(function (event) {
if (event.isNext) {
var curTime = Bacon.scheduler.now();
if (lastCallTime === 0 && !leading) {
lastCallTime = curTime;
}
var remaining = delay - (curTime - lastCallTime);
if (remaining <= 0) {
cancelTrailing();
lastCallTime = curTime;
return this.push(event);
} else if (trailing) {
trailingValue = event;
cancelTrailing();
timeoutId = Bacon.scheduler.setTimeout(trailingCall, remaining);
}
} else if (event.isEnd && timeoutId !== null) {
trailingEnd = event;
} else {
return this.push(event);
}
}));
};

Property.prototype.toEventStream = function (options) {
Expand Down
4 changes: 2 additions & 2 deletions dist/Bacon.min.js

Large diffs are not rendered by default.

55 changes: 48 additions & 7 deletions dist/Bacon.noAssert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@
CompositeUnsubscribe: CompositeUnsubscribe,
never: never,
constant: constant,
version: '2.0.5'
version: '<version>'
};
Bacon.Bacon = Bacon;
function map(p) {
Expand Down Expand Up @@ -3264,12 +3264,53 @@
}));
});
};
var defaultOptions = { trailing: true };
Observable.prototype.throttle = function (delay) {
return this.delayChanges(new Desc(this, 'throttle', [delay]), function (changes) {
return changes.bufferWithTime(delay).map(function (values) {
return values[values.length - 1];
});
});
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOptions;
var leading = options.leading, trailing = options.trailing;
var lastCallTime = 0;
var trailingValue, timeoutId, trailingEnd;
var cancelTrailing = function () {
if (timeoutId !== null) {
Bacon.scheduler.clearTimeout(timeoutId);
timeoutId = null;
}
};
var stream = void 0;
var trailingCall = function () {
stream.dispatcher.push(trailingValue);
timeoutId = null;
trailingValue = null;
lastCallTime = Bacon.scheduler.now();
if (trailingEnd) {
stream.dispatcher.push(trailingEnd);
}
};
return stream = withDesc(new Desc(this, 'throttle', options === defaultOptions ? [delay] : [
delay,
options
]), this.withHandler(function (event) {
if (event.isNext) {
var curTime = Bacon.scheduler.now();
if (lastCallTime === 0 && !leading) {
lastCallTime = curTime;
}
var remaining = delay - (curTime - lastCallTime);
if (remaining <= 0) {
cancelTrailing();
lastCallTime = curTime;
return this.push(event);
} else if (trailing) {
trailingValue = event;
cancelTrailing();
timeoutId = Bacon.scheduler.setTimeout(trailingCall, remaining);
}
} else if (event.isEnd && timeoutId !== null) {
trailingEnd = event;
} else {
return this.push(event);
}
}));
};
Property.prototype.toEventStream = function (options) {
var _this = this;
Expand Down Expand Up @@ -3371,4 +3412,4 @@
], f || Array));
};
return Bacon;
}));
}));
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions spec/specs/throttle.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ describe "EventStream.throttle(delay)", ->
expectStreamTimings(
-> series(2, [1, 2, 3]).throttle(t(3))
[[5, 2], [8, 3]])
describe "options", ->
describe "{ leading: true }", ->
expectStreamTimings(
-> series(2, [1, 2, 3]).throttle(t(3), { leading: true })
[[2, 1], [6, 3]])
describe "{ trailing: true, leading: true }", ->
expectStreamTimings(
-> series(2, [1, 2, 3]).throttle(t(3), { leading: true, trailing: true })
[[2, 1], [5, 2], [8, 3]])
describe "works with synchronous source", ->
expectStreamEvents(
-> fromArray([1, 2, 3]).throttle(t(3))
Expand Down
59 changes: 53 additions & 6 deletions src/throttle.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,56 @@
import "./buffer";
import "./delaychanges";
import "./map";
import "./scheduler";
import Bacon from "./core";
import Observable from "./observable";
import { Desc } from "./describe";
import { Desc, withDesc } from "./describe";

Observable.prototype.throttle = function (delay) {
return this.delayChanges(new Desc(this, "throttle", [delay]), (changes) => changes.bufferWithTime(delay).map((values) => values[values.length - 1]));
const defaultOptions = { trailing: true }

Observable.prototype.throttle = function (delay, options = defaultOptions ) {
var { leading, trailing } = options
var lastCallTime = 0
var trailingValue, timeoutId, trailingEnd
const cancelTrailing = () => {
if (timeoutId !== null) {
Bacon.scheduler.clearTimeout(timeoutId)
timeoutId = null
}
}
let stream
const trailingCall = () => {
//console.log("delayed push", trailingValue, trailingEnd)
stream.dispatcher.push(trailingValue)
timeoutId = null
trailingValue = null
lastCallTime = Bacon.scheduler.now()
if (trailingEnd) {
stream.dispatcher.push(trailingEnd)
}
}
return stream = withDesc(
new Desc(this, "throttle", options === defaultOptions ? [delay] : [delay, options]),
this.withHandler(function (event) {
if (event.isNext) {
let curTime = Bacon.scheduler.now()
if (lastCallTime === 0 && !leading) {
lastCallTime = curTime
}
let remaining = delay - (curTime - lastCallTime)
if (remaining <= 0) {
cancelTrailing()
lastCallTime = curTime
return this.push(event)
} else if (trailing) {
//console.log('scheduling', event, 'in', remaining)
trailingValue = event
cancelTrailing()
timeoutId = Bacon.scheduler.setTimeout(trailingCall, remaining)
}
} else if (event.isEnd && timeoutId !== null) {
trailingEnd = event
} else {
//console.log('immediate', event)
return this.push(event)
}
})
)
};