server: migrate feedService to TypeScript

This commit is contained in:
Jesse Chan
2020-10-21 19:50:47 +08:00
parent d173bf9322
commit b0ff983600
21 changed files with 929 additions and 685 deletions
-75
View File
@@ -1,75 +0,0 @@
import FeedSub from 'feedsub';
// TODO: Use a type-checked Feed parser
class Feed {
constructor(options) {
this.options = options || {};
this.options.maxItemHistory = options.maxItemHistory || 100;
this.items = [];
if (!options.url) {
console.error('Feed URL must be defined.');
return null;
}
this.reader = new FeedSub(options.url, {
autoStart: true,
emitOnStart: true,
maxHistory: this.options.maxItemHistory,
interval: options.interval ? Number(options.interval) : 15,
forceInterval: true,
readEveryItem: true,
});
this.initReader();
}
modify(options) {
Object.assign(this.options, options);
this.items = [];
this.reader = new FeedSub(options.url, {
autoStart: true,
emitOnStart: true,
maxHistory: this.options.maxItemHistory,
interval: options.interval ? Number(options.interval) : 15,
forceInterval: true,
readEveryItem: true,
});
this.initReader();
}
getItems() {
return this.items;
}
handleFeedItems(items) {
const nextLength = this.items.length + items.length;
if (nextLength >= this.options.maxItemHistory) {
const diff = nextLength - this.options.maxHistory;
this.items = this.items.splice(0, diff);
}
this.items = this.items.concat(items);
this.options.onNewItems({
feed: this.options,
items,
});
}
initReader() {
this.reader.on('items', this.handleFeedItems.bind(this));
this.reader.on('error', (error) => {
console.log('Feed reader error:', error);
});
this.reader.start();
}
stopReader() {
this.reader.stop();
}
}
export default Feed;
+74
View File
@@ -0,0 +1,74 @@
import FeedSub, {FeedItem} from 'feedsub';
export interface FeedReaderOptions {
feedID: string;
feedLabel: string;
url: string;
interval: number;
maxHistory: number;
onNewItems: (options: FeedReaderOptions, items: Array<FeedItem>) => void;
}
class FeedReader {
private options: FeedReaderOptions;
private items: Array<FeedItem> = [];
private reader: FeedSub | null = null;
constructor(options: FeedReaderOptions) {
this.options = options;
this.initReader();
}
modify(options: Partial<FeedReaderOptions>) {
this.options = {...this.options, ...options};
this.items = [];
this.initReader();
}
getOptions() {
return this.options;
}
getItems() {
return this.items;
}
handleFeedItems(items: Array<FeedItem>) {
const nextLength = this.items.length + items.length;
if (nextLength >= this.options.maxHistory) {
const diff = nextLength - this.options.maxHistory;
this.items = this.items.splice(0, diff);
}
this.items = this.items.concat(items);
this.options.onNewItems(this.options, items);
}
initReader() {
this.reader = new FeedSub(this.options.url, {
autoStart: true,
emitOnStart: true,
maxHistory: this.options.maxHistory,
interval: this.options.interval,
forceInterval: true,
});
this.reader.on('items', this.handleFeedItems.bind(this));
this.reader.on('error', (error) => {
console.log('Feed reader error:', error);
});
this.reader.start();
}
stopReader() {
if (this.reader != null) {
this.reader.stop();
this.reader = null;
}
}
}
export default FeedReader;