react-rss
Version:
React component capable of reading and rendering any RSS feed
225 lines (224 loc) • 10.9 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rssEnhancer = void 0;
var react_1 = __importStar(require("react"));
var getRss_1 = require("../utils/getRss");
var enhanceUrl = function (url, requestEnhancer) { return requestEnhancer ? requestEnhancer(url) : { input: url }; };
var RSS = /** @class */ (function (_super) {
__extends(RSS, _super);
function RSS() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.interval = null;
_this.state = {
contents: null,
loading: false,
};
_this.clearInterval = function () {
if (_this.interval !== null) {
clearInterval(_this.interval);
}
};
_this.getRss = function () { return __awaiter(_this, void 0, void 0, function () {
var props, enhanced, contents;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
props = this.props;
enhanced = enhanceUrl(props.url, props.requestEnhancer);
return [4 /*yield*/, getRss_1.getRss(enhanced, window.fetch, props.headerEnhancer, props.itemEnhancer)];
case 1:
contents = _a.sent();
return [2 /*return*/, contents];
}
});
}); };
_this.sendRequest = function () { return __awaiter(_this, void 0, void 0, function () {
var props, contents, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
props = this.props;
this.setState({ loading: true });
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
if (!props.url) {
throw Error('Cannot set up rss feed of nonexistent url!');
}
return [4 /*yield*/, this.getRss()];
case 2:
contents = _a.sent();
this.setState({ contents: contents, loading: false, error: null });
if (props.subscribe) {
this.setupSubscription(contents);
}
return [3 /*break*/, 4];
case 3:
e_1 = _a.sent();
this.setState({ error: e_1, loading: false });
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); };
_this.setupSubscription = function (contents) {
var props = _this.props;
var subscription = parseInt(contents.header.ttl, 10);
_this.interval = window.setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
var contents_1, e_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
if (!props.url) {
throw Error('Cannot set up rss feed of nonexistent url!');
}
this.setState({ loading: true });
return [4 /*yield*/, this.getRss()];
case 1:
contents_1 = _a.sent();
this.setState({ contents: contents_1, loading: false, error: null });
return [3 /*break*/, 3];
case 2:
e_2 = _a.sent();
this.setState({ error: e_2, loading: false });
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); }, (isNaN(subscription) ? 10 : subscription) * 60 * 1000);
};
return _this;
}
RSS.prototype.componentDidMount = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.sendRequest();
return [2 /*return*/];
});
});
};
RSS.prototype.componentDidUpdate = function (prevProps) {
var props = this.props;
if (prevProps.subscribe !== props.subscribe || prevProps.url !== props.url) {
this.clearInterval();
this.sendRequest();
}
};
RSS.prototype.componentWillUnmount = function () {
this.clearInterval();
};
RSS.prototype.render = function () {
var _a = this, state = _a.state, props = _a.props;
if (state.loading) {
if (props.loadingComponent) {
var Loader = props.loadingComponent;
return react_1.default.createElement(Loader, null);
}
}
else if (state.error) {
if (props.errorComponent) {
var Error_1 = props.errorComponent;
return react_1.default.createElement(Error_1, { contents: state.contents, error: state.error });
}
}
else if (state.contents) {
return props.children(state.contents);
}
return null;
};
return RSS;
}(react_1.Component));
/**
* Higher-order component enhancer for rss feed
* @param Component Component to enhance
* @param url Static url from which to draw
* @param requestEnhancer You can use this to statically enhance requests by setting request headers, method etc.
* @param headerEnhancer Use this function to add properties to resulting rss object. You can use standard media queries on rssElement to do so.
* @param itemEnhacer Use this to enhance individual news from feed. You can use standard media queries on item to do so.
*/
exports.rssEnhancer = function (Component, url, requestEnhancer, headerEnhancer, itemEnhancer) {
if (url === void 0) { url = null; }
return function (props) {
return (react_1.default.createElement(RSS, { url: (props.url || url), loadingComponent: props.loadingComponent, errorComponent: props.errorComponent, requestEnhancer: requestEnhancer, headerEnhancer: headerEnhancer, itemEnhancer: itemEnhancer }, function (rss) { return (react_1.default.createElement(Component, __assign({}, props, { rss: rss }))); }));
};
};