UNPKG

4.04 kBTypeScriptView Raw
1import { IClosable } from './IClosable';
2import { INotifiable } from './INotifiable';
3/**
4 * Timer that is triggered in equal time intervals.
5 *
6 * It has summetric cross-language implementation
7 * and is often used by Pip.Services toolkit to
8 * perform periodic processing and cleanup in microservices.
9 *
10 * @see [[INotifiable]]
11 *
12 * ### Example ###
13 *
14 * class MyComponent {
15 * private timer: FixedRateTimer = new FixedRateTimer(() => { this.cleanup }, 60000);
16 * ...
17 * public open(correlationId: string, callback: (err: any) => void): void {
18 * ...
19 * timer.start();
20 * ...
21 * }
22 *
23 * public open(correlationId: string, callback: (err: any) => void): void {
24 * ...
25 * timer.stop();
26 * ...
27 * }
28 *
29 * private cleanup(): void {
30 * ...
31 * }
32 * ...
33 * }
34 */
35export declare class FixedRateTimer implements IClosable {
36 private _task;
37 private _callback;
38 private _delay;
39 private _interval;
40 private _timer;
41 private _timeout;
42 /**
43 * Creates new instance of the timer and sets its values.
44 *
45 * @param taskOrCallback (optional) a Notifiable object or callback function to call when timer is triggered.
46 * @param interval (optional) an interval to trigger timer in milliseconds.
47 * @param delay (optional) a delay before the first triggering in milliseconds.
48 *
49 * @see [[setTask]]
50 * @see [[setCallback]]
51 * @see [[setInterval]]
52 * @see [[setDelay]]
53 */
54 constructor(taskOrCallback?: any, interval?: number, delay?: number);
55 /**
56 * Gets the INotifiable object that receives notifications from this timer.
57 *
58 * @returns the INotifiable object or null if it is not set.
59 */
60 getTask(): INotifiable;
61 /**
62 * Sets a new INotifiable object to receive notifications from this timer.
63 *
64 * @param value a INotifiable object to be triggered.
65 */
66 setTask(value: INotifiable): void;
67 /**
68 * Gets the callback function that is called when this timer is triggered.
69 *
70 * @returns the callback function or null if it is not set.
71 */
72 getCallback(): () => void;
73 /**
74 * Sets the callback function that is called when this timer is triggered.
75 *
76 * @param value the callback function to be called.
77 */
78 setCallback(value: () => void): void;
79 /**
80 * Gets initial delay before the timer is triggered for the first time.
81 *
82 * @returns the delay in milliseconds.
83 */
84 getDelay(): number;
85 /**
86 * Sets initial delay before the timer is triggered for the first time.
87 *
88 * @param value a delay in milliseconds.
89 */
90 setDelay(value: number): void;
91 /**
92 * Gets periodic timer triggering interval.
93 *
94 * @returns the interval in milliseconds
95 */
96 getInterval(): number;
97 /**
98 * Sets periodic timer triggering interval.
99 *
100 * @param value an interval in milliseconds.
101 */
102 setInterval(value: number): void;
103 /**
104 * Checks if the timer is started.
105 *
106 * @returns true if the timer is started and false if it is stopped.
107 */
108 isStarted(): boolean;
109 /**
110 * Starts the timer.
111 *
112 * Initially the timer is triggered after delay.
113 * After that it is triggered after interval until it is stopped.
114 *
115 * @see [[stop]]
116 */
117 start(): void;
118 /**
119 * Stops the timer.
120 *
121 * @see [[start]]
122 */
123 stop(): void;
124 /**
125 * Closes the timer.
126 *
127 * This is required by [[ICloseable]] interface,
128 * but besides that it is identical to stop().
129 *
130 * @param correlationId (optional) transaction id to trace execution through call chain.
131 * @param callback callback function that receives error or null no errors occured.
132 *
133 * @see [[stop]]
134 */
135 close(correlationId: string, callback?: (err: any) => void): void;
136}