Skip to content

Instantly share code, notes, and snippets.

@marksyzm
Created February 19, 2020 08:08
Show Gist options
  • Save marksyzm/2acd60783bddc2ee11a7963f6436168a to your computer and use it in GitHub Desktop.
Save marksyzm/2acd60783bddc2ee11a7963f6436168a to your computer and use it in GitHub Desktop.
Moment from now generator for StencilJS
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