Skip to content

Commit

Permalink
feat: skip first load
Browse files Browse the repository at this point in the history
separate event & boolean to turn off first load
  • Loading branch information
Tobiah committed Mar 18, 2020
1 parent 81d4cfb commit 3fd49fe
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/FeedEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FeedEmitter extends EventEmitter {
* we create a new instance of this "Class".
* @param {Object} [options={ userAgent: defaultUA }] [description]
*/
constructor(options = { userAgent: DEFAULT_UA }) {
constructor(options = { userAgent: DEFAULT_UA, skipFirstLoad: false }) {
super();

this.feedList = [];
Expand All @@ -74,6 +74,12 @@ class FeedEmitter extends EventEmitter {
* @type {string}
*/
this.userAgent = options.userAgent;

/**
* Whether or not to skip the normal emit event on first load
* @type {boolean}
*/
this.skipFirstLoad = options.skipFirstLoad;
}


Expand Down Expand Up @@ -182,7 +188,7 @@ class FeedEmitter extends EventEmitter {
*/
createSetInterval(feed) {
const feedManager = new FeedManager(this, feed);
feedManager.getContent();
feedManager.getContent(true);
return setInterval(feedManager.getContent.bind(feedManager), feed.refresh);
}

Expand Down
12 changes: 9 additions & 3 deletions src/FeedManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,22 @@ class FeedManager {
* Now that we have all the new items, add them to the
feed item list.
* @param {Object} data data to mutate
* @param {boolean} firstload Whether or not this is the first laod
*/
populateNewItemsInFeed(data) {
populateNewItemsInFeed(data, firstload) {
data.newItems.forEach((item) => {
this.feed.addItem(item);
this.instance.emit(this.feed.eventName, item);
if (!(firstload && this.instance.skipFirstLoad)) {
this.instance.emit(this.feed.eventName, item);
}
});
}

onError(error) {
this.instance.emit('error', error);
}

async getContent() {
async getContent(firstload) {
this.feed.fetchData()
.then((items) => {
const data = {
Expand All @@ -61,6 +64,9 @@ class FeedManager {
this.sortItemsByDate(data);
this.identifyNewItems(data);
this.populateNewItemsInFeed(data);
if (firstload && !this.instance.skipFirstLoad) {
this.instance.emit(`initial-load:${this.feed.url}`, { url: this.feed.url, items: this.feed.items });
}
})
.catch(this.onError.bind(this));
}
Expand Down
29 changes: 29 additions & 0 deletions test/unit/rss-feed-emitter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,33 @@ describe('RssFeedEmitter (unit)', () => {
});
});

describe('when instantiated with skipFirstLoad option', () => {
it('does not emit an initial load event', (done) => {
nock('https://www.nintendolife.com/')
.get('/feeds/latest')
.replyWithFile(200, path.join(__dirname, '/fixtures/nintendo-latest-first-fetch.xml'))
.get('/feeds/latest')
.replyWithFile(200, path.join(__dirname, '/fixtures/nintendo-latest-second-fetch.xml'));

feeder.destroy();
feeder = new RssFeedEmitter({ skipFirstLoad: true });

feeder.add({
url: 'https://www.nintendolife.com/feeds/latest',
refresh: 100,
});

feeder.on('initial-load:https://nintendolife.com/feeds/latest', () => {
expect(false).to.eq(true);
});
feeder.on('new-item', (item) => {
if (item.title === 'Nintendo Life Weekly: Huge Pokémon Reveal Next Month, Arguably the Rarest Nintendo Game, and More') {
done();
}
});
});
});

describe('#add', () => {
it('should be a Function', () => {
expect(feeder.add).to.be.a('function');
Expand Down Expand Up @@ -204,6 +231,8 @@ describe('RssFeedEmitter (unit)', () => {
.twice()
.replyWithFile(200, path.join(__dirname, '/fixtures/nintendo-latest-first-fetch.xml'));

feeder = new RssFeedEmitter({ skipFirstLoad: true });

feeder.add({
url: 'https://www.nintendolife.com/feeds/latest',
refresh: notDefaultRefresh1,
Expand Down

0 comments on commit 3fd49fe

Please sign in to comment.