timeout.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. export class TimeoutService {
  18. private logTag: string = '[TimeoutService]';
  19. // Config
  20. private config: threema.Config;
  21. // Angular services
  22. private $log: ng.ILogService;
  23. private $timeout: ng.ITimeoutService;
  24. // List of registered timeouts
  25. private timeouts: Set<ng.IPromise<any>> = new Set();
  26. public static $inject = ['CONFIG', '$log', '$timeout'];
  27. constructor(config: threema.Config, $log: ng.ILogService, $timeout: ng.ITimeoutService) {
  28. this.config = config;
  29. this.$log = $log;
  30. this.$timeout = $timeout;
  31. }
  32. /**
  33. * Log a message on debug log level, but only if the `DEBUG` flag is enabled.
  34. */
  35. private logDebug(msg: string): void {
  36. if (this.config.DEBUG_TIMER) {
  37. this.$log.debug(this.logTag, msg);
  38. }
  39. }
  40. /**
  41. * Register a timeout.
  42. */
  43. public register<T>(fn: (...args: any[]) => T, delay: number, invokeApply: boolean, name?: string): ng.IPromise<T> {
  44. this.logDebug('Registering timeout' + (name === undefined ? '' : ` (${name})`));
  45. const timeout = this.$timeout(fn, delay, invokeApply);
  46. timeout
  47. .then(() => this.timeouts.delete(timeout))
  48. .catch((reason) => {
  49. if (reason !== 'canceled') { // We can safely ignore cancellation
  50. this.$log.error(this.logTag, 'Registered timeout promise rejected:', reason);
  51. }
  52. });
  53. // Stick name onto promise for debugging purposes
  54. // tslint:disable-next-line: no-string-literal
  55. timeout['_timeout_name'] = name;
  56. this.timeouts.add(timeout);
  57. return timeout;
  58. }
  59. /**
  60. * Cancel the specified timeout.
  61. *
  62. * Return true if the task hasn't executed yet and was successfully canceled.
  63. */
  64. public cancel<T>(timeout: ng.IPromise<T>): boolean {
  65. // Retrieve name from promise for debugging purposes
  66. // tslint:disable-next-line: no-string-literal
  67. const name = timeout['_timeout_name'];
  68. this.logDebug('Cancelling timeout' + (name === undefined ? '' : ` (${name})`));
  69. const cancelled = this.$timeout.cancel(timeout);
  70. this.timeouts.delete(timeout);
  71. return cancelled;
  72. }
  73. /**
  74. * Cancel all pending timeouts.
  75. */
  76. public cancelAll() {
  77. this.$log.debug(this.logTag, 'Cancelling ' + this.timeouts.size + ' timeouts');
  78. for (const timeout of this.timeouts) {
  79. this.$timeout.cancel(timeout);
  80. }
  81. this.timeouts.clear();
  82. }
  83. }