UNPKG

@schoolbelle/common

Version:

74 lines (69 loc) 2.27 kB
import { Injectable, NgZone, defineInjectable, inject } from '@angular/core'; import { Subject } from 'rxjs'; import { takeWhile, map, startWith } from 'rxjs/operators'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class TimerService { /** * @param {?} zone */ constructor(zone) { this.zone = zone; this.tickEvent = new Subject(); /** * setInterval is overriden by Angular to cause a change detection cycle. * avoid unnecessary detection cycles by using zone.runOutsideAnguler. */ this.zone.runOutsideAngular((/** * @return {?} */ () => { setInterval((/** * @return {?} */ () => { this.tickEvent.next(Date.now()); }), 1000); })); } /** * this function return observable that tries to stream time left at every second until it passes zero. * @param {?} timeDiff * @return {?} */ setTimeout(timeDiff) { /** @type {?} */ const at = Date.now() + timeDiff; return this.tickEvent.pipe(takeWhile((/** * @param {?} cts * @return {?} */ cts => cts <= at)), map((/** * @param {?} cts * @return {?} */ cts => (at - cts))), startWith(timeDiff)); } } TimerService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ TimerService.ctorParameters = () => [ { type: NgZone } ]; /** @nocollapse */ TimerService.ngInjectableDef = defineInjectable({ factory: function TimerService_Factory() { return new TimerService(inject(NgZone)); }, token: TimerService, providedIn: "root" }); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { TimerService }; //# sourceMappingURL=schoolbelle-common-timer.js.map