Skip to content

Commit

Permalink
fix(timeoutmap): expirationTime
Browse files Browse the repository at this point in the history
  • Loading branch information
fupengl committed Mar 17, 2021
1 parent 552ffaf commit fe59c17
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/function/timeout-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ type TimeoutMapOptions<K, V> = {
/**
* delete handler
*/
onTimeout?: (key: K, value: V, map: TimeoutMap<K, V>) => void;
onTimeout?: (key: K, value: V, options: TimeoutMapKeyArgs<K, V>, map: TimeoutMap<K, V>) => void;
};

interface TimeoutMapKeyArgs<K, V> {
timeout: ReturnType<typeof setTimeout>;
expirationTime: number;
options: TimeoutMapOptions<K, V>;
onTimeout?: (key: K, value: V, map: TimeoutMap<K, V>) => void;
}

class TimeoutMap<K, V> extends Map<K, V> {
Expand Down Expand Up @@ -83,19 +82,19 @@ class TimeoutMap<K, V> extends Map<K, V> {
} else {
this._setKeyArgs(key, {
timeout: setTimeout(() => {
_options?.onTimeout?.(key, super.get(key)!, this);
_options?.onTimeout?.(key, super.get(key)!, this._keyArgs.get(key)!, this);
this.delete(key);
}, _options.timeout),
});
}
};

private _checkExpirationData() {
for (const [k, arg] of this._keyArgs) {
if (arg.expirationTime && arg.expirationTime > Date.now().valueOf()) {
this._keyArgs.delete(k);
arg?.onTimeout?.(k, super.get(k)!, this);
this.delete(k);
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);
}
}
}
Expand Down

0 comments on commit fe59c17

Please sign in to comment.