|
| 1 | +import { IRBSegment } from '../../dtos/types'; |
| 2 | +import { ILogger } from '../../logger/types'; |
| 3 | +import { ISettings } from '../../types'; |
| 4 | +import { isFiniteNumber, isNaNNumber, toNumber } from '../../utils/lang'; |
| 5 | +import { setToArray } from '../../utils/lang/sets'; |
| 6 | +import { usesSegments } from '../AbstractSplitsCacheSync'; |
| 7 | +import { KeyBuilderCS } from '../KeyBuilderCS'; |
| 8 | +import { IRBSegmentsCacheSync } from '../types'; |
| 9 | +import { LOG_PREFIX } from './constants'; |
| 10 | + |
| 11 | +export class RBSegmentsCacheInLocal implements IRBSegmentsCacheSync { |
| 12 | + |
| 13 | + private readonly keys: KeyBuilderCS; |
| 14 | + private readonly log: ILogger; |
| 15 | + private hasSync?: boolean; |
| 16 | + |
| 17 | + constructor(settings: ISettings, keys: KeyBuilderCS) { |
| 18 | + this.keys = keys; |
| 19 | + this.log = settings.log; |
| 20 | + } |
| 21 | + |
| 22 | + clear() { |
| 23 | + this.getNames().forEach(name => this.remove(name)); |
| 24 | + localStorage.removeItem(this.keys.buildRBSegmentsTillKey()); |
| 25 | + this.hasSync = false; |
| 26 | + } |
| 27 | + |
| 28 | + update(toAdd: IRBSegment[], toRemove: IRBSegment[], changeNumber: number): boolean { |
| 29 | + this.setChangeNumber(changeNumber); |
| 30 | + const updated = toAdd.map(toAdd => this.add(toAdd)).some(result => result); |
| 31 | + return toRemove.map(toRemove => this.remove(toRemove.name)).some(result => result) || updated; |
| 32 | + } |
| 33 | + |
| 34 | + private setChangeNumber(changeNumber: number) { |
| 35 | + try { |
| 36 | + localStorage.setItem(this.keys.buildRBSegmentsTillKey(), changeNumber + ''); |
| 37 | + localStorage.setItem(this.keys.buildLastUpdatedKey(), Date.now() + ''); |
| 38 | + this.hasSync = true; |
| 39 | + } catch (e) { |
| 40 | + this.log.error(LOG_PREFIX + e); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private updateSegmentCount(diff: number){ |
| 45 | + const segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey(); |
| 46 | + const count = toNumber(localStorage.getItem(segmentsCountKey)) + diff; |
| 47 | + // @ts-expect-error |
| 48 | + if (count > 0) localStorage.setItem(segmentsCountKey, count); |
| 49 | + else localStorage.removeItem(segmentsCountKey); |
| 50 | + } |
| 51 | + |
| 52 | + private add(rbSegment: IRBSegment): boolean { |
| 53 | + try { |
| 54 | + const name = rbSegment.name; |
| 55 | + const rbSegmentKey = this.keys.buildRBSegmentKey(name); |
| 56 | + const rbSegmentFromLocalStorage = localStorage.getItem(rbSegmentKey); |
| 57 | + const previous = rbSegmentFromLocalStorage ? JSON.parse(rbSegmentFromLocalStorage) : null; |
| 58 | + |
| 59 | + localStorage.setItem(rbSegmentKey, JSON.stringify(rbSegment)); |
| 60 | + |
| 61 | + let usesSegmentsDiff = 0; |
| 62 | + if (previous && usesSegments(previous)) usesSegmentsDiff--; |
| 63 | + if (usesSegments(rbSegment)) usesSegmentsDiff++; |
| 64 | + if (usesSegmentsDiff !== 0) this.updateSegmentCount(usesSegmentsDiff); |
| 65 | + |
| 66 | + return true; |
| 67 | + } catch (e) { |
| 68 | + this.log.error(LOG_PREFIX + e); |
| 69 | + return false; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private remove(name: string): boolean { |
| 74 | + try { |
| 75 | + const rbSegment = this.get(name); |
| 76 | + if (!rbSegment) return false; |
| 77 | + |
| 78 | + localStorage.removeItem(this.keys.buildRBSegmentKey(name)); |
| 79 | + |
| 80 | + if (usesSegments(rbSegment)) this.updateSegmentCount(-1); |
| 81 | + |
| 82 | + return true; |
| 83 | + } catch (e) { |
| 84 | + this.log.error(LOG_PREFIX + e); |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private getNames(): string[] { |
| 90 | + const len = localStorage.length; |
| 91 | + const accum = []; |
| 92 | + |
| 93 | + let cur = 0; |
| 94 | + |
| 95 | + while (cur < len) { |
| 96 | + const key = localStorage.key(cur); |
| 97 | + |
| 98 | + if (key != null && this.keys.isRBSegmentKey(key)) accum.push(this.keys.extractKey(key)); |
| 99 | + |
| 100 | + cur++; |
| 101 | + } |
| 102 | + |
| 103 | + return accum; |
| 104 | + } |
| 105 | + |
| 106 | + get(name: string): IRBSegment | null { |
| 107 | + const item = localStorage.getItem(this.keys.buildRBSegmentKey(name)); |
| 108 | + return item && JSON.parse(item); |
| 109 | + } |
| 110 | + |
| 111 | + contains(names: Set<string>): boolean { |
| 112 | + const namesArray = setToArray(names); |
| 113 | + const namesInStorage = this.getNames(); |
| 114 | + return namesArray.every(name => namesInStorage.indexOf(name) !== -1); |
| 115 | + } |
| 116 | + |
| 117 | + getChangeNumber(): number { |
| 118 | + const n = -1; |
| 119 | + let value: string | number | null = localStorage.getItem(this.keys.buildRBSegmentsTillKey()); |
| 120 | + |
| 121 | + if (value !== null) { |
| 122 | + value = parseInt(value, 10); |
| 123 | + |
| 124 | + return isNaNNumber(value) ? n : value; |
| 125 | + } |
| 126 | + |
| 127 | + return n; |
| 128 | + } |
| 129 | + |
| 130 | + usesSegments(): boolean { |
| 131 | + // If cache hasn't been synchronized, assume we need segments |
| 132 | + if (!this.hasSync) return true; |
| 133 | + |
| 134 | + const storedCount = localStorage.getItem(this.keys.buildSplitsWithSegmentCountKey()); |
| 135 | + const splitsWithSegmentsCount = storedCount === null ? 0 : toNumber(storedCount); |
| 136 | + |
| 137 | + if (isFiniteNumber(splitsWithSegmentsCount)) { |
| 138 | + return splitsWithSegmentsCount > 0; |
| 139 | + } else { |
| 140 | + return true; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments