activitymonitor.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. IDisposable
  5. } from '@phosphor/disposable';
  6. import {
  7. ISignal, Signal
  8. } from '@phosphor/signaling';
  9. /**
  10. * A class that monitors activity on a signal.
  11. */
  12. export
  13. class ActivityMonitor<Sender, Args> implements IDisposable {
  14. /**
  15. * Construct a new activity monitor.
  16. */
  17. constructor(options: ActivityMonitor.IOptions<Sender, Args>) {
  18. options.signal.connect(this._onSignalFired, this);
  19. this._timeout = options.timeout || 1000;
  20. }
  21. /**
  22. * A signal emitted when activity has ceased.
  23. */
  24. get activityStopped(): ISignal<this, ActivityMonitor.IArguments<Sender, Args>> {
  25. return this._activityStopped;
  26. }
  27. /**
  28. * The timeout associated with the monitor, in milliseconds.
  29. */
  30. get timeout(): number {
  31. return this._timeout;
  32. }
  33. set timeout(value: number) {
  34. this._timeout = value;
  35. }
  36. /**
  37. * Test whether the monitor has been disposed.
  38. *
  39. * #### Notes
  40. * This is a read-only property.
  41. */
  42. get isDisposed(): boolean {
  43. return this._isDisposed;
  44. }
  45. /**
  46. * Dispose of the resources used by the activity monitor.
  47. */
  48. dispose(): void {
  49. if (this._isDisposed) {
  50. return;
  51. }
  52. this._isDisposed = true;
  53. Signal.clearData(this);
  54. }
  55. /**
  56. * A signal handler for the monitored signal.
  57. */
  58. private _onSignalFired(sender: Sender, args: Args): void {
  59. clearTimeout(this._timer);
  60. this._sender = sender;
  61. this._args = args;
  62. this._timer = window.setTimeout(() => {
  63. this._activityStopped.emit({
  64. sender: this._sender,
  65. args: this._args
  66. });
  67. }, this._timeout);
  68. }
  69. private _timer = -1;
  70. private _timeout = -1;
  71. private _sender: Sender;
  72. private _args: Args;
  73. private _isDisposed = false;
  74. private _activityStopped = new Signal<this, ActivityMonitor.IArguments<Sender, Args>>(this);
  75. }
  76. /**
  77. * The namespace for `ActivityMonitor` statics.
  78. */
  79. export
  80. namespace ActivityMonitor {
  81. /**
  82. * The options used to construct a new `ActivityMonitor`.
  83. */
  84. export
  85. interface IOptions<Sender, Args> {
  86. /**
  87. * The signal to monitor.
  88. */
  89. signal: ISignal<Sender, Args>;
  90. /**
  91. * The activity timeout in milliseconds.
  92. *
  93. * The default is 1 second.
  94. */
  95. timeout?: number;
  96. }
  97. /**
  98. * The argument object for an activity timeout.
  99. *
  100. */
  101. export
  102. interface IArguments<Sender, Args> {
  103. /**
  104. * The most recent sender object.
  105. */
  106. sender: Sender;
  107. /**
  108. * The most recent argument object.
  109. */
  110. args: Args;
  111. }
  112. }