Skip to content
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
37 changes: 36 additions & 1 deletion src/app/shared/services/hackernews-api.service.ts

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔍 Missing required contributor credit

The PR omits the contributor-list update required by the contribution checklist. Neither credited author appears in the contributors list.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import fetch from 'unfetch';
import {map } from 'rxjs/operators';
import { Subscription } from 'rxjs';

import { Story } from '../models/story';
import { User } from '../models/user';
Expand All @@ -17,7 +18,8 @@ export class HackerNewsAPIService {
}

fetchFeed(feedType: string, page: number): Observable<Story[]> {
return lazyFetch(`${this.baseUrl}/${feedType}?page=${page}`);
const url = `${this.baseUrl}/${feedType}?page=${page}`;
return takePrefetchedFeed<Story[]>(url) || lazyFetch(url);
}

fetchItemContent(id: number): Observable<Story> {
Expand Down Expand Up @@ -64,3 +66,36 @@ function lazyFetch<T>(url, options?) {
});
}

// Single-use hand-off of the feed request started by the inline script in index.html.
// Falls back to a normal fetch if that request failed.
function takePrefetchedFeed<T>(url: string): Observable<T> | null {
const prefetch = (window as any).__hnFeedPrefetch;
if (!prefetch || prefetch.url !== url) {
return null;
}
delete (window as any).__hnFeedPrefetch;
return new Observable<T>(observer => {
let fallback: Subscription;
let cancelToken = false;
// Promise.resolve adopts the native promise into the Angular zone so change detection runs.
Promise.resolve(prefetch.data).then(
data => {
if (!cancelToken) {
observer.next(data);
observer.complete();
}
},
() => {
if (!cancelToken) {
fallback = lazyFetch<T>(url).subscribe(observer);
}
}
);
return () => {
cancelToken = true;
if (fallback) {
fallback.unsubscribe();
}
};
});
}
12 changes: 12 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
<meta charset="utf-8">
<title>Angular 2 HN</title>
<base href="/">
<script>
// Start the landing feed request now instead of after the app bundle boots;
// HackerNewsAPIService.fetchFeed picks it up (see takePrefetchedFeed).
(function () {
var m = /^\/(?:(news|newest|show|ask|jobs)\/(\d+)\/?)?$/.exec(location.pathname);
if (!m || !window.fetch || !window.Promise) { return; }
var url = 'https://node-hnapi.herokuapp.com/' + (m[1] || 'news') + '?page=' + (m[2] ? +m[2] : 1);
var data = fetch(url).then(function (res) { return res.json(); });
data.catch(function () {});
window.__hnFeedPrefetch = { url: url, data: data };
})();
</script>

<meta name="description" content="A Hacker News client built with Angular CLI, RxJS and Webpack"/>

Expand Down