Created
February 19, 2020 08:08
-
-
Save marksyzm/2acd60783bddc2ee11a7963f6436168a to your computer and use it in GitHub Desktop.
Moment from now generator for StencilJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Prop, State } from '@stencil/core'; | |
import { interval, Subject } from 'rxjs'; | |
import { takeUntil, startWith } from 'rxjs/operators'; | |
import moment from 'moment'; | |
@Component({ | |
tag: 'app-now' | |
}) | |
export class Now { | |
@Prop() time: number; | |
@Prop() interval = 10000; | |
@State() now: string; | |
private readonly destroySubs$ = new Subject(); | |
connectedCallback() { | |
interval(this.interval) | |
.pipe( | |
startWith(0), | |
takeUntil(this.destroySubs$) | |
) | |
.subscribe(() => (this.now = moment(this.time).fromNow())); | |
} | |
disconnectedCallback() { | |
this.destroySubs$.next(); | |
this.destroySubs$.complete(); | |
} | |
render() { | |
return this.now; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment