* Method that initializes the store. * * @param options {Options} - The options used to setup the middleware.
(options: Options)
| 58 | * @param options {Options} - The options used to setup the middleware. |
| 59 | */ |
| 60 | init(options: Options): void { |
| 61 | // Get the duration of a window from the options. |
| 62 | this.windowMs = options.windowMs |
| 63 | |
| 64 | // check for a valid value |
| 65 | this.validations?.windowMs(this.windowMs) |
| 66 | |
| 67 | // Indicates that init was called more than once. |
| 68 | // Could happen if a store was shared between multiple instances. |
| 69 | if (this.interval) clearInterval(this.interval) |
| 70 | |
| 71 | // Reset all clients left in previous every `windowMs`. |
| 72 | this.interval = setInterval(() => { |
| 73 | this.clearExpired() |
| 74 | }, this.windowMs) |
| 75 | |
| 76 | // Cleaning up the interval will be taken care of by the `shutdown` method. |
| 77 | this.interval.unref?.() |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Method to fetch a client's hit count and reset time. |
no test coverage detected