-
Notifications
You must be signed in to change notification settings - Fork 4
/
timeout-map.ts
207 lines (182 loc) · 5.47 KB
/
timeout-map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { prefSetTimeout, clearPrefTimeout } from '../function/pref-setTimeout';
type TimeoutMapConstructorOptions = {
/**
* Control storage length
* @default undefined not Control
*/
maxLength?: number;
/**
* Priority cleaning type
* @default filo
*/
clearUpPriority?: 'shortPeriod' | 'longPeriod' | 'fifo' | 'filo';
};
type TimeoutMapOptions<K, V> = TimeoutMapConstructorOptions & {
/**
* expiration interval
* @default undefined no timeout
*/
timeout?: number;
/**
* Whether to delete passively
* onTimeout will not be called on time
* @default true
*/
passiveDeletion?: boolean;
/**
* delete handler
*/
onTimeout?: (key: K, value: V, options: TimeoutMapKeyArgs<K, V>, map: TimeoutMap<K, V>) => void;
/**
* when over limit
*/
onOverLimit?: (key: K, value: V, options: TimeoutMapKeyArgs<K, V>, map: TimeoutMap<K, V>) => void;
};
interface TimeoutMapKeyArgs<K, V> {
timerId: number;
expirationTime: number;
options: TimeoutMapOptions<K, V>;
}
/**
* Map with set validity period
* @category Object
*/
class TimeoutMap<K, V> extends Map<K, V> {
private readonly _keyArgs: Map<K, TimeoutMapKeyArgs<K, V>> = new Map();
private readonly _options: TimeoutMapOptions<K, V> = {};
constructor(entries?: readonly (readonly [K, V])[] | null, options?: TimeoutMapOptions<K, V>) {
super(entries);
if (options) {
this._options = {
passiveDeletion: true,
clearUpPriority: 'filo',
...options,
};
}
}
get(key: K): V | undefined {
this._cleanOverLimitElement();
return super.get(key);
}
has(key: K): boolean {
this._cleanOverLimitElement();
return super.has(key);
}
keys(): IterableIterator<K> {
this._cleanOverLimitElement();
return super.keys();
}
values(): IterableIterator<V> {
this._cleanOverLimitElement();
return super.values();
}
entries(): IterableIterator<[K, V]> {
this._cleanOverLimitElement();
return super.entries();
}
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any) {
this._cleanOverLimitElement();
return super.forEach(callbackfn, thisArg);
}
set(
key: K,
value: V,
options?: Omit<TimeoutMapOptions<K, V>, keyof TimeoutMapConstructorOptions>,
) {
this._setTimeout(key, options);
this._cleanExpirationElement();
this._cleanOverLimitElement();
return super.set(key, value);
}
delete(key) {
this._clearTimeout(key);
this._keyArgs.delete(key);
return super.delete(key);
}
clear() {
for (const key of this._keyArgs.keys()) {
this._clearTimeout(key);
}
this._keyArgs.clear();
return super.clear();
}
private _mergeOptions = (options?: Partial<TimeoutMapOptions<K, V>>): TimeoutMapOptions<K, V> => {
return {
...this._options,
...options,
};
};
private _setKeyArgs(k: K, args: Partial<TimeoutMapKeyArgs<K, V>>): TimeoutMapKeyArgs<K, V> {
const _args = this._keyArgs.get(k);
const opt = { ..._args, ...args } as TimeoutMapKeyArgs<K, V>;
this._keyArgs.set(k, opt);
return opt;
}
private _setTimeout = (key: K, options?: TimeoutMapOptions<K, V>) => {
const _options = this._mergeOptions(options);
this._setKeyArgs(key, { options: _options });
if (!_options.timeout || _options.timeout === Infinity) return;
this._clearTimeout(key);
if (_options.passiveDeletion) {
this._setKeyArgs(key, { expirationTime: Date.now().valueOf() + _options.timeout });
} else {
this._setKeyArgs(key, {
timerId: prefSetTimeout(() => {
_options?.onTimeout?.(key, super.get(key)!, this._keyArgs.get(key)!, this);
this.delete(key);
}, _options.timeout),
});
}
};
private _clearTimeout(key: K): boolean {
const arg = this._keyArgs.get(key);
if (arg?.timerId !== undefined) {
this._keyArgs.delete(key);
clearPrefTimeout(arg!.timerId!);
return true;
}
return false;
}
private _cleanExpirationElement() {
for (const [key, arg] of this._keyArgs) {
if (arg.expirationTime && arg.expirationTime < Date.now().valueOf()) {
arg.options?.onTimeout?.(key, super.get(key)!, this._keyArgs.get(key)!, this);
this._keyArgs.delete(key);
this.delete(key);
}
}
}
private _cleanOverLimitElement() {
if (this._options.maxLength !== undefined && super.size > this._options.maxLength) {
const invalidKeys: K[] = [];
const sortKeys = () =>
[...super.keys()].sort(
(a, b) =>
(this._keyArgs.get(a)?.expirationTime || 0) -
(this._keyArgs.get(b)?.expirationTime || 0),
);
switch (this._options.clearUpPriority) {
case 'filo':
invalidKeys.push(...[...super.keys()].slice(this._options.maxLength, super.size));
break;
case 'fifo':
invalidKeys.push(...[...super.keys()].slice(0, super.size - this._options.maxLength));
break;
case 'longPeriod':
invalidKeys.push(...sortKeys().slice(this._options.maxLength, super.size));
break;
case 'shortPeriod':
invalidKeys.push(...sortKeys().slice(0, super.size - this._options.maxLength));
break;
}
invalidKeys.forEach((key) => {
const arg = this._keyArgs.get(key);
if (arg) {
arg.options?.onOverLimit?.(key, super.get(key)!, this._keyArgs.get(key)!, this);
}
this.delete(key);
});
}
}
}
export default TimeoutMap;