* Clears the cached values, current value stays intact, but it gets removed from the cache. \ * Meaning, if you dispatch a new value you can't goBack. \ * To keep the last value in the cache, pass `{leaveLast: true}` in the param `options`. * * It only works if the Unit i
(options?: ClearCacheOptions)
| 509 | * @category Common Units |
| 510 | */ |
| 511 | clearCache(options?: ClearCacheOptions): boolean { |
| 512 | const leaveFirst: boolean = options?.leaveFirst === true; |
| 513 | const leaveLast: boolean = options?.leaveLast === true; |
| 514 | |
| 515 | if ( |
| 516 | this.isFrozen || |
| 517 | this.cachedValuesCount === 0 || |
| 518 | (this.cachedValuesCount === 1 && (leaveFirst || leaveLast)) || |
| 519 | (this.cachedValuesCount === 2 && leaveFirst && leaveLast) |
| 520 | ) { |
| 521 | return false; |
| 522 | } |
| 523 | const start = leaveFirst ? 1 : 0; |
| 524 | const deleteCount = this.cachedValuesCount - start - (leaveLast ? 1 : 0); |
| 525 | this._cachedValues.splice(start, deleteCount); |
| 526 | this._cacheIndex = Math.max(0, this.cachedValuesCount - 1); |
| 527 | |
| 528 | if (this.eventsSubject?.observers.length && !this.isMuted) { |
| 529 | this.eventsSubject.next(new EventUnitClearCache(options)); |
| 530 | } |
| 531 | return true; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Clears the value by dispatching the default value. \ |