angular-cn/public/docs/_examples/lifecycle-hooks/ts/app/logger.service.ts

27 lines
665 B
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
@Injectable()
export class LoggerService {
2016-04-27 14:28:22 -04:00
logs: string[] = [];
prevMsg = '';
prevMsgCount = 1;
2016-04-27 14:28:22 -04:00
log(msg: string) {
if (msg === this.prevMsg) {
// Repeat message; update last log entry with count.
2016-04-27 14:28:22 -04:00
this.logs[this.logs.length - 1] = msg + ` (${this.prevMsgCount += 1}x)`;
} else {
// New message; log it.
this.prevMsg = msg;
this.prevMsgCount = 1;
this.logs.push(msg);
}
}
2016-04-27 14:28:22 -04:00
clear() { this.logs.length = 0; }
// schedules a view refresh to ensure display catches up
tick() { this.tick_then(() => { }); }
tick_then(fn: () => any) { setTimeout(fn, 0); }
}