Skip to content
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

Concurrently pull feed updates #39

Merged
merged 1 commit into from
Aug 25, 2024
Merged
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
85 changes: 43 additions & 42 deletions src/app/services/feed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,51 @@ export class FeedService implements OnDestroy {
this.entries = storage_feed;
}

public async syncEntriesWithUpstream(event: any) {
const tempFeedMasterData: Array<any> = [];
const feedList = this.sourcesService.getSources();

// Update the cache for any feeds that need updating
for (const feed of feedList) {
const nextPollDate = feed.lastRetrieved + (feed.pollingFrequency * 1000); // Milliseconds
let feedData = null;

if (Date.now() < nextPollDate) {
console.warn('[FeedService] Too soon to retrieve: ' + feed.url);
feedData = await this.storageService.getObjectFromStorage(feed.url);
}
else {
try {
const feedDataDownloaded = await this.sourcesService.downloadAndParseFeed(feed.url);
feedData = this.sourcesService.updateLocalCache(feed, feedDataDownloaded);
}
catch (error) {
console.error('[FeedService] ' + error);
// Get the cache as the feed isn't accessible
feedData = await this.storageService.getObjectFromStorage(feed.url);
}
}

// Update master feed
if (feedData !== null) {
for (const item of feedData) {
tempFeedMasterData.push(item);
}
}
else {
console.error('[FeedService] No Feed Data for ' + feed.url);
continue;
}
public async syncEntriesWithUpstream(event: any): Promise<void> {
try {
const feedList = this.sourcesService.getSources();
const tempFeedMasterData = await Promise.all(
feedList.map(async (feed) => {
const nextPollDate = feed.lastRetrieved + (feed.pollingFrequency * 1000); // Milliseconds
let feedData: Array<any> | null = null;

if (Date.now() < nextPollDate) {
console.warn(`[FeedService] Too soon to retrieve: ${feed.url}`);
feedData = await this.storageService.getObjectFromStorage(feed.url);
} else {
feedData = await this.fetchAndUpdateFeed(feed);
}

if (!feedData) {
console.error(`[FeedService] No Feed Data for ${feed.url}`);
return [];
}

return feedData;
})
);

// Flatten the array of arrays and update master feed
this.entries = this.sortByDate(tempFeedMasterData.flat());
this.storageService.set(STORAGE_FEED_DATA, JSON.stringify(this.entries));
console.log('[FeedService] Rebuilt master feed from upstream');
this.lastUpdated = Date.now();
this.storageService.set(STORAGE_FEED_DATA_TIMESTAMP, this.lastUpdated);
event.target.complete();
} catch (error) {
console.error('[FeedService] An error occurred:', error);
}
}

// Update cache and values
this.entries = this.sortByDate(tempFeedMasterData);
this.storageService.set(STORAGE_FEED_DATA, JSON.stringify(this.entries));
console.log('[FeedService] Rebuilt master feed from upstream');
this.lastUpdated = Date.now();
this.storageService.set(STORAGE_FEED_DATA_TIMESTAMP, this.lastUpdated);
event.target.complete();
private async fetchAndUpdateFeed(feed: any): Promise<any> {
try {
const feedDataDownloaded = await this.sourcesService.downloadAndParseFeed(feed.url);
return this.sourcesService.updateLocalCache(feed, feedDataDownloaded);
} catch (error) {
console.error(`[FeedService] ${error}`);
// Fallback to cached data
return await this.storageService.getObjectFromStorage(feed.url);
}
}

public async appendEntryFromCache(feedUrl: string) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"importHelpers": true,
"target": "es2022",
"module": "es2020",
"lib": ["es2018", "dom"],
"lib": ["es2019", "dom"],
"useDefineForClassFields": false
},
"angularCompilerOptions": {
Expand Down
Loading