Update crawl run duration at intervals (#155)

fixes #138
This commit is contained in:
sua yoo 2022-02-23 16:14:01 -08:00 committed by GitHub
parent bb9c953d92
commit 3fe3691e74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ import humanizeDuration from "pretty-ms";
/** /**
* Show time passed from date in human-friendly format * Show time passed from date in human-friendly format
* Updates every 5 seconds
* *
* Usage example: * Usage example:
* ```ts * ```ts
@ -16,17 +17,36 @@ export class RelativeDuration extends LitElement {
@property({ type: String }) @property({ type: String })
value?: string; // `new Date` compatible date format value?: string; // `new Date` compatible date format
@state()
private now = Date.now();
// For long polling:
private timerId?: number;
static humanize(duration: number) { static humanize(duration: number) {
return humanizeDuration(duration, { return humanizeDuration(duration, {
secondsDecimalDigits: 0, secondsDecimalDigits: 0,
}); });
} }
connectedCallback(): void {
super.connectedCallback();
this.timerId = window.setInterval(() => this.updateValue(), 1000 * 5);
}
disconnectedCallback(): void {
window.clearInterval(this.timerId);
super.disconnectedCallback();
}
render() { render() {
if (!this.value) return ""; if (!this.value) return "";
return RelativeDuration.humanize( return RelativeDuration.humanize(this.now - new Date(this.value).valueOf());
Date.now() - new Date(this.value).valueOf() }
);
private updateValue() {
this.now = Date.now();
} }
} }