Skip to content

add throttleImmediate #712

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions spec/specs/throttle.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ expect = require("chai").expect
expectStreamTimings,
expectPropertyEvents,
error,
semiunstable,
lessThan,
map,
fromArray
Expand All @@ -28,6 +29,22 @@ describe "EventStream.throttle(delay)", ->
it "toString", ->
expect(Bacon.never().throttle(1).toString()).to.equal("Bacon.never().throttle(1)")

describe "EventStream.throttleImmediate(delay)", ->
describe "outputs first event immediately, then ignores events for given amount of milliseconds", ->
expectStreamTimings(
-> series(2, [1, 2, 3, 4]).throttleImmediate(t(3))
[[2, 1], [6, 3], [11, 4]], semiunstable) # why 11?
describe "works with synchronous source", ->
expectStreamEvents(
-> fromArray([1, 2, 3, 4]).throttleImmediate(t(3))
[1, 4], semiunstable)
describe "does not emit double events with synchronous source", ->
expectStreamEvents(
-> fromArray([1]).throttleImmediate(t(1))
[1], semiunstable)
it "toString", ->
expect(Bacon.never().throttleImmediate(1).toString()).to.equal("Bacon.never().throttleImmediate(1)")

describe "Property.throttle", ->
describe "throttles changes, but not initial value", ->
expectPropertyEvents(
Expand All @@ -39,3 +56,15 @@ describe "Property.throttle", ->
[1])
it "toString", ->
expect(Bacon.constant(0).throttle(1).toString()).to.equal("Bacon.constant(0).throttle(1)")

describe "Property.throttleImmediate", ->
describe "throttles changes, but not initial value", ->
expectPropertyEvents(
-> series(1, [1,2,3]).toProperty(0).throttleImmediate(t(4))
[0,3], semiunstable)
describe "works with Bacon.once", ->
expectPropertyEvents(
-> once(1).toProperty().throttleImmediate(1)
[1], semiunstable)
it "toString", ->
expect(Bacon.constant(0).throttleImmediate(1).toString()).to.equal("Bacon.constant(0).throttleImmediate(1)")
17 changes: 16 additions & 1 deletion src/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ import "./delaychanges";
import "./map";
import Observable from "./observable";
import { Desc } from "./describe";
import Bacon from "./core";

Observable.prototype.throttle = function (delay) {
return this.delayChanges(new Desc(this, "throttle", [delay]), (changes) => changes.bufferWithTime(delay).map((values) => values[values.length - 1]));
return this.delayChanges(new Desc(this, "throttle", [delay]), (changes) =>
changes
.bufferWithTime(delay)
.map((values) => values[values.length - 1]));
};
Observable.prototype.throttleImmediate = function (delay) {
return this.delayChanges(new Desc(this, "throttleImmediate", [delay]), (changes) =>
Bacon.mergeAll(
changes.debounceImmediate(delay),
changes.debounce(delay)
)
/*changes.flatMapFirst(value =>
Bacon.once(value).concat(Bacon.later(delay).filter(false))
Copy link
Author

@Phoscur Phoscur Feb 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raimohanska this is debounceImmediate, but it doesn't work because it throws away events while debouncing.. it has to somehow know when something happens inbetween and in that case emit the latest event after the stream calmed down.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! Needs a bit more thought then...

)*/
);
};