verstak
Version:
Verstak - Front-End Library
65 lines (64 loc) • 2.32 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { ObservableObject, atomic, reactive } from "reactronic";
export class RealTimeClock extends ObservableObject {
constructor(interval = 1000) {
super();
this.hour = 0;
this.minute = 0;
this.second = 0;
this.ms = 0;
this.interval = 0;
this.paused = false;
this.interval = interval;
this.put(new Date());
}
pause(value = true) {
this.paused = value;
}
tick() {
let calibration = 0;
try {
const now = new Date();
this.put(now);
calibration = now.getTime() % this.interval;
}
finally {
setTimeout(() => this.tick(), this.interval - calibration);
}
}
activate() {
this.tick();
}
put(time) {
this.hour = time.getHours();
this.minute = time.getMinutes();
this.second = time.getSeconds();
this.ms = time.getMilliseconds();
}
}
__decorate([
atomic,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Boolean]),
__metadata("design:returntype", void 0)
], RealTimeClock.prototype, "pause", null);
__decorate([
atomic,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], RealTimeClock.prototype, "tick", null);
__decorate([
reactive,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], RealTimeClock.prototype, "activate", null);