* Ensure the callback is only executed one at a time and throttles the calls * to every 100ms.
(callback: OIDCCallbackFunction)
| 154 | * to every 100ms. |
| 155 | */ |
| 156 | protected withLock(callback: OIDCCallbackFunction): OIDCCallbackFunction { |
| 157 | let lock: Promise<any> = Promise.resolve(); |
| 158 | return async (params: OIDCCallbackParams): Promise<OIDCResponse> => { |
| 159 | // We do this to ensure that we would never return the result of the |
| 160 | // previous lock, only the current callback's value would get returned. |
| 161 | await lock; |
| 162 | lock = lock |
| 163 | |
| 164 | .catch(() => null) |
| 165 | |
| 166 | .then(async () => { |
| 167 | const difference = Date.now() - this.lastExecutionTime; |
| 168 | if (difference <= THROTTLE_MS) { |
| 169 | await setTimeout(THROTTLE_MS - difference, { signal: params.timeoutContext }); |
| 170 | } |
| 171 | this.lastExecutionTime = Date.now(); |
| 172 | return await callback(params); |
| 173 | }); |
| 174 | return await lock; |
| 175 | }; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
nothing calls this directly
no test coverage detected