add all as an event emitter

This commit is contained in:
Dylan Greene 2016-04-28 22:23:21 -04:00
parent 57d00504f4
commit fadd256aa8
4 changed files with 68 additions and 6 deletions

View file

@ -2,6 +2,7 @@
const got = require('got'); const got = require('got');
const uniqueRandomArray = require('unique-random-array'); const uniqueRandomArray = require('unique-random-array');
const EventEmitter = require('eventemitter3');
const randomCache = {}; const randomCache = {};
@ -32,6 +33,23 @@ function randomPuppy(subreddit) {
.then(getRandomImage => formatResult(getRandomImage)); .then(getRandomImage => formatResult(getRandomImage));
} }
// silly feature to play with observables
function all(subreddit) {
const eventEmitter = new EventEmitter();
function emitRandomImage(subreddit) {
randomPuppy(subreddit).then(imageUrl => {
eventEmitter.emit('data', imageUrl + '#' + subreddit);
if (eventEmitter.listeners('data').length) {
setTimeout(() => emitRandomImage(subreddit), 200);
}
});
}
emitRandomImage(subreddit);
return eventEmitter;
}
function callback(subreddit, cb) { function callback(subreddit, cb) {
randomPuppy(subreddit) randomPuppy(subreddit)
.then(url => cb(null, url)) .then(url => cb(null, url))
@ -49,3 +67,5 @@ module.exports = (subreddit, cb) => {
return randomPuppy(subreddit); return randomPuppy(subreddit);
} }
}; };
module.exports.all = all;

View file

@ -1,7 +1,7 @@
{ {
"name": "random-puppy", "name": "random-puppy",
"version": "1.0.1", "version": "1.1.0",
"description": "Get a random puppy image url.", "description": "Get a random imgur image url, by default a puppy.",
"license": "MIT", "license": "MIT",
"repository": "dylang/random-puppy", "repository": "dylang/random-puppy",
"author": { "author": {
@ -13,7 +13,8 @@
"node": ">=4.0.0" "node": ">=4.0.0"
}, },
"scripts": { "scripts": {
"test": "xo && ava" "test": "xo && ava",
"watch": "ava --watch"
}, },
"files": [ "files": [
"index.js" "index.js"
@ -27,11 +28,13 @@
"placeholder" "placeholder"
], ],
"dependencies": { "dependencies": {
"eventemitter3": "^1.2.0",
"got": "^6.3.0", "got": "^6.3.0",
"unique-random-array": "^1.0.0" "unique-random-array": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"ava": "^0.14.0", "ava": "^0.14.0",
"rx-lite": "^4.0.8",
"xo": "^0.14.0" "xo": "^0.14.0"
}, },
"xo": { "xo": {

View file

@ -27,14 +27,33 @@ randomPuppy()
## API ## API
### randomPuppy() ### `randomPuppy()`
Returns a `promise` for a random puppy image url from http://imgur.com/ from https://www.reddit.com/r/puppy Returns a `promise` for a random puppy image url from http://imgur.com/ from https://www.reddit.com/r/puppy
### randomPuppy(subreddit) ### `randomPuppy(subreddit)`
Returns a `promise` for a random image url from the selected subreddit. *Warning: We cannot promise it will be a image of a puppy!* Returns a `promise` for a random image url from the selected subreddit. *Warning: We cannot promise it will be a image of a puppy!*
### `randomPuppy.all(subreddit)`
Returns an `eventemitter` for getting all random images for a subreddit.
```js
const event = randomPuppy.all(subreddit);
event.on('data', url => console.log(url));
```
Or:
```js
const event = randomPuppy.all('puppies');
Observable.fromEvent(event, 'data')
.subscribe(data => {
console.log(data);
});
```
## Notes ## Notes
* Node 4 or newer. * Node 4 or newer.

22
test.js
View file

@ -1,7 +1,8 @@
import test from 'ava'; import test from 'ava';
import {Observable} from 'rx-lite';
import randomPuppy from './'; import randomPuppy from './';
const imgurRegEx = /^https?:\/\/(\w+\.)?imgur.com\/[a-zA-Z0-9]+(\.[a-zA-Z]{3})?$/; const imgurRegEx = /^https?:\/\/(\w+\.)?imgur.com\/[a-zA-Z0-9]+(\.[a-zA-Z]{3})?(#[a-zA-Z]*)?$/;
test('get random', async t => { test('get random', async t => {
const result = await randomPuppy(); const result = await randomPuppy();
@ -58,3 +59,22 @@ test('invalid subreddit', async t => {
const result4 = await randomPuppy(false); const result4 = await randomPuppy(false);
t.regex(result4, imgurRegEx); t.regex(result4, imgurRegEx);
}); });
test('all', t => {
t.plan(10);
const puppyEmitter = randomPuppy.all('puppies');
const robotEmitter = randomPuppy.all('robots');
const puppySource = Observable.fromEvent(puppyEmitter, 'data');
const robotSource = Observable.fromEvent(robotEmitter, 'data');
const sharedSource = Observable
.merge(puppySource, robotSource)
.take(10);
sharedSource.subscribe(data => {
t.regex(data, imgurRegEx);
// console.log(data);
});
return sharedSource.toPromise();
});