Commit 8629fd35 authored by Sean Hammond's avatar Sean Hammond Committed by GitHub

Merge pull request #5 from hypothesis/zen-observable-update

zen-observable update
parents 682088d7 e31e91c6
...@@ -50,7 +50,7 @@ function selections(document) { ...@@ -50,7 +50,7 @@ function selections(document) {
observable.buffer(100, selectionEvents), observable.buffer(100, selectionEvents),
// Emit an initial event on the next tick // Emit an initial event on the next tick
observable.Observable.of({}), observable.delay(0, observable.Observable.of({})),
]); ]);
return events.map(function () { return events.map(function () {
......
...@@ -32,6 +32,28 @@ var Observable = require('zen-observable'); ...@@ -32,6 +32,28 @@ var Observable = require('zen-observable');
}); });
} }
/**
* Delay events from a source Observable by `delay` ms.
*/
function delay(delay, src) {
return new Observable(function (obs) {
var timeouts = [];
var sub = src.subscribe({
next: function (value) {
var t = setTimeout(function () {
timeouts = timeouts.filter(function (other) { return other !== t; });
obs.next(value);
}, delay);
timeouts.push(t);
},
});
return function () {
timeouts.forEach(clearTimeout);
sub.unsubscribe();
};
});
}
/** /**
* Buffers events from a source Observable, waiting for a pause of `delay` * Buffers events from a source Observable, waiting for a pause of `delay`
* ms with no events before emitting the last value from `src`. * ms with no events before emitting the last value from `src`.
...@@ -99,6 +121,7 @@ function drop(src, n) { ...@@ -99,6 +121,7 @@ function drop(src, n) {
module.exports = { module.exports = {
buffer: buffer, buffer: buffer,
delay: delay,
drop: drop, drop: drop,
listen: listen, listen: listen,
merge: merge, merge: merge,
......
'use strict';
var observable = require('../observable');
describe('observable', function () {
describe('delay()', function () {
var clock;
beforeEach(function () {
clock = sinon.useFakeTimers();
});
afterEach(function () {
clock.restore();
});
it('defers events', function () {
var received = [];
var obs = observable.delay(50, observable.Observable.of('foo'));
obs.forEach(function (v) {
received.push(v);
});
assert.deepEqual(received, []);
clock.tick(100);
assert.deepEqual(received, ['foo']);
});
it('delivers events in sequence', function () {
var received = [];
var obs = observable.delay(10, observable.Observable.of(1,2));
obs.forEach(function (v) {
received.push(v);
});
clock.tick(20);
assert.deepEqual(received, [1,2]);
});
});
});
This diff is collapsed.
...@@ -92,7 +92,7 @@ ...@@ -92,7 +92,7 @@
"watchify": "^3.7.0", "watchify": "^3.7.0",
"websocket": "^1.0.22", "websocket": "^1.0.22",
"whatwg-fetch": "^0.10.1", "whatwg-fetch": "^0.10.1",
"zen-observable": "^0.2.1" "zen-observable": "^0.3.0"
}, },
"browserify": { "browserify": {
"transform": [ "transform": [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment