@creejs/commons-collection 2.0.6 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- "use strict";function t(t){return null==t}function e(t){return null===t}function r(t){return null!=t&&"number"==typeof t}function n(t){return null!==t&&"object"==typeof t&&(t.constructor===Object||void 0===t.constructor)}function i(t){return null!=t&&"string"==typeof t}function s(t){return null!=t&&"symbol"==typeof t}Object.defineProperty(exports,"__esModule",{value:!0});var o={assertNumber:h,assertPositive:function(t,e){if(!function(t){return!!r(t)&&t>0}(t))throw new Error(`${e?'"'+e+'" ':""}Not Positive: ${t}`)},assertNegative:function(t,e){if(!function(t){return!!r(t)&&t<0}(t))throw new Error(`${e?'"'+e+'" ':""}Not Negative: ${t}`)},assertNotNegative:function(t,e){if(!function(t){return!!r(t)&&t>=0}(t))throw new Error(`${e?'"'+e+'" ':""}Not "0 or Positive": ${t}`)},assertBoolean:function(t,e){if(!function(t){return"boolean"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Boolean: type=${typeof t} value=${_(t)}`)},assertObject:function(t,e){if(!function(t){return null!=t&&"object"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Object: type=${typeof t} value=${_(t)}`)},assertPlainObject:function(t,e){if(!n(t))throw new Error(`${e?'"'+e+'" ':""}Not PlainObject: type=${typeof t} value=${_(t)}`)},assertSymbol:function(t,e){if(!s(t))throw new Error(`${e?'"'+e+'" ':""}Not Symbol: type=${typeof t} value=${_(t)}`)},assertFunction:function(t,e){if(!function(t){return"function"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Function: type=${typeof t} value=${_(t)}`)},assertInstance:function(t,e){if(!function(t){return null!=t&&"object"==typeof t&&!n(t)}(t))throw new Error(`${e?'"'+e+'" ':""}Not Class Instance: type=${typeof t} value=${_(t)}`)},assertPromise:function(t,e){if(!function(t){return null!=t&&"function"==typeof t.then}(t))throw new Error(`${e?'"'+e+'" ':""}Not Promise: type=${typeof t} value=${_(t)}`)},assertNil:function(e,r){if(!t(e))throw new Error(`${r?'"'+r+'" ':""}Neither Null nor Undefined: type=${typeof e} value=${_(e)}`)},assertNotNil:function(e,r){if(t(e))throw new Error((r?'"'+r+'" ':"")+"Should Not Nil")},assertNull:function(t,r){if(!e(t))throw new Error(`${r?'"'+r+'" ':""}Not Null: type=${typeof t} value=${_(t)}`)},assertNotNull:function(t,r){if(e(t))throw new Error((r?'"'+r+'" ':"")+"Should Not Null")},assertUndefined:function(t,e){if(!function(t){return void 0===t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Undefined: type=${typeof t} value=${_(t)}`)},assertString:a,assertArray:function(t,e){if(!Array.isArray(t))throw new Error(`${e?'"'+e+'" ':""}Not Array: type=${typeof t} value=${_(t)}`)},assertStringOrSymbol:function(t,e){if(!i(t)&&!s(t))throw new Error(`${e?'"'+e+'" ':""}Not String or Symbol: type=${typeof t} value=${_(t)}`)},assertInt8Array:function(t,e){if(function(t){return t instanceof Int8Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Int8Array")},assertUint8Array:function(t,e){if(function(t){return t instanceof Uint8Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Uint8Array")},assertUint8ClampedArray:function(t,e){if(function(t){return t instanceof Uint8ClampedArray}(t))throw new Error((e?'"'+e+'" ':"")+"Not Uint8ClampedArray")},assertInt16Array:function(t,e){if(function(t){return t instanceof Int16Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Int16Array")},assertUint16Array:function(t,e){if(function(t){return t instanceof Uint16Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Uint16Array")},assertInt32Array:function(t,e){if(function(t){return t instanceof Int32Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Int32Array")},assertUint32Array:function(t,e){if(function(t){return t instanceof Uint32Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Uint32Array")},assertFloat32Array:function(t,e){if(function(t){return t instanceof Float32Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Float32Array")},assertFloat64Array:function(t,e){if(function(t){return t instanceof Float64Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Float64Array")},assertBigInt64Array:function(t,e){if(function(t){return t instanceof BigInt64Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not BigInt64Array")},assertBigUint64Array:function(t,e){if(function(t){return t instanceof BigUint64Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not BigUint64Array")},assertTypedArray:function(t,e){if(function(t){return ArrayBuffer.isView(t)&&t.constructor!==DataView}(t))throw new Error((e?'"'+e+'" ':"")+"Not TypedArray")},assertArrayBuffer:function(t,e){if(!function(t){return t instanceof ArrayBuffer}(t))throw new Error((e?'"'+e+'" ':"")+"Not ArrayBuffer")}};function a(t,e){if(!i(t))throw new Error(`${e?'"'+e+'" ':""}Not String: type=${typeof t} value=${_(t)}`)}function h(t,e){if(!r(t))throw new Error(`${e?'"'+e+'" ':""}Not Number: type=${typeof t} value=${_(t)}`)}var u={isEmpty:l,assertNotEmpty:c,isBlank:f,assertNotBlank:function(t,e){if(f(t))throw new Error(`${e?'"'+e+'" ':""}Is Blank: ${t}`)},capitalize:function(t){if(a(t),0===t.length)return t;const e=t.charAt(0),r=e.toUpperCase();return e===r?t:r+t.slice(1)},decapitalize:function(t){if(a(t),0===t.length)return t;const e=t.charAt(0),r=e.toLowerCase();return e===r?t:r+t.slice(1)},splitWithFixedLength:function(t,e,r=" "){if(a(t),h(e),a(r),0===t.length)return[];if(e<=0)throw new Error("length muse >=0");if(t.length<e)return[t.padEnd(e,r)];const n=[];for(let i=0;i<t.length;i+=e){const s=t.substring(i,i+e);n.push(s.padEnd(e,r))}return n},split:function(t,...e){if(a(t),0===t.length)return[];const r=[...e];0===e.length&&e.push(",");const n=d(t,...r);if(0===n.length)return[];const i=[];let s="",o=0;for(const{marker:e,index:r}of n)s=t.substring(o,r),i.push(s),o=r+e.length;return s=t.substring(o),i.push(s),i},findMarkerPositions:function(t,...e){if(a(t),0===e.length)throw new Error("At least one marker must be provided");const r=[];for(const n of new Set(e)){if(l(n))continue;a(n);let e=t.indexOf(n);for(;-1!==e;)r.push({marker:n,index:e}),e=t.indexOf(n,e+n.length)}return r.sort((t,e)=>t.index-e.index),r},findMarkerPositionsRegex:d,substringBefore:function(t,e){if(a(t),a(e),0===t.length||0===e.length)return;const r=t.indexOf(e);return-1!==r?t.substring(0,r):void 0},substringBeforeLast:function(t,e){if(a(t),a(e),0===t.length||0===e.length)return;const r=t.lastIndexOf(e);return-1!==r?t.substring(0,r):void 0},substringAfter:function(t,e){if(a(t),a(e),0===t.length||0===e.length)return;const r=t.indexOf(e);return-1!==r?t.substring(r+e.length):void 0},substringAfterLast:function(t,e){if(a(t),a(e),0===t.length||0===e.length)return;const r=t.lastIndexOf(e);return-1!==r?t.substring(r+e.length):void 0},substringBetween:function(t,e,r){c(t),c(e),c(r);const n=t.indexOf(e);if(-1===n)return;const i=t.indexOf(r,n+e.length);return-1!==i?t.substring(n+e.length,i):void 0},substringBetweenGreedy:function(t,e,r){c(t),c(e),c(r);const n=t.indexOf(e);if(-1===n)return;const i=t.lastIndexOf(r);return-1===i||i<=n?void 0:t.substring(n+e.length,i)},substringsBetween:function(t,e,r){c(t),c(e),c(r);const n=[];let i=0;for(;;){const s=t.indexOf(e,i);if(-1===s)break;const o=t.indexOf(r,s+e.length);if(-1===o)break;n.push(t.substring(s+e.length,o)),i=o+r.length}return n},safeToString:_};function l(t){return null==t||(a(t),0===t.length)}function c(t,e){if(l(t))throw new Error(`${e?'"'+e+'" ':""}IsEmpty String: ${t}`)}function f(t){return null==t||(a(t),0===t.trim().length)}function d(t,...e){if(a(t),0===e.length)throw new Error("At least one marker must be provided");const r=[...new Set(e.filter(t=>null!=t))].map(t=>(a(t),t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"))),n=new RegExp(r.map(t=>`(${t})`).join("|"),"g"),i=[];let s=null;for(;null!==(s=n.exec(t));){for(let t=1;t<s.length;t++)if(s[t]){i.push({marker:e[t-1],index:s.index});break}0===s[0].length&&n.lastIndex++}return i}function _(t){if(null===t)return"null";if(void 0===t)return"undefined";const e=typeof t;if("string"===e)return t;if("symbol"===e)return`Symbol(${t.description})`;if("function"===e)return`Function ${t.name}(){}`;if(t instanceof String)return t.toString();if(Number.isNaN(t))return"NaN";if(t===1/0)return"Infinity";if(t===-1/0)return"-Infinity";if(t instanceof Error)return`${t.constructor.name}: ${t.message}`;if(t instanceof Promise)return"Promise";if(t instanceof Set)return`Set: ${_(Array.from(t))}`;if(t instanceof Map)return`Map: ${_(Array.from(t.entries()))}`;if(t instanceof RegExp)return t.toString();if(Array.isArray(t))return`[${t.map(_).join(", ")}]`;let r;try{r=JSON.stringify(t)}catch(e){r=t.toString()}return r}new TextDecoder,new TextEncoder;const m=1e6;var p=function(){if("undefined"!=typeof performance&&"number"==typeof performance.timeOrigin){const t=performance.timeOrigin,e=performance.now();return Math.ceil((t+e)/m)}return Date.now()};const{assertPositive:v}=o;class g{constructor(t){v(t,"capacity"),this.capacity=t,this._map=new Map,this._head=void 0,this._tail=void 0}get size(){return this._map.size}get oldest(){return this._head?.value}get newest(){return this._tail?.value}[Symbol.iterator](){return this._map.keys()}add(t){if(this._map.has(t)){const e=this._map.get(t);e&&this._removeNode(e)}else this.size>=this.capacity&&this._removeOldest();this._addNew(t)}has(t){return this._map.has(t)}delete(t){if(this._map.has(t)){const e=this._map.get(t);return e&&this._removeNode(e),!0}return!1}clear(){this._map.clear(),this._head=void 0,this._tail=void 0}values(){return this._map.keys()}_addNew(t){const e={value:t,prev:this._tail,next:void 0};this._tail?this._tail.next=e:this._head=e,this._tail=e,this._map.set(t,e)}_removeNode(t){t.prev?t.prev.next=t.next:this._head=t.next,t.next?t.next.prev=t.prev:this._tail=t.prev,this._map.delete(t.value)}_removeOldest(){this._head&&this._removeNode(this._head)}}var w={isFunction:y,isNil:x};function y(t){return"function"==typeof t}function x(t){return null==t}function E(t){return null!=t&&"string"==typeof t}var N={assertNumber:function(t,e){if(!function(t){return null!=t&&"number"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Number: type=${typeof t} value=${L(t)}`)},assertFunction:function(t,e){if(!y(t))throw new Error(`${e?'"'+e+'" ':""}Not Function: type=${typeof t} value=${L(t)}`)},assertNotNil:function(t,e){if(x(t))throw new Error((e?'"'+e+'" ':"")+"Should Not Nil")},assertString:function(t,e){if(!E(t))throw new Error(`${e?'"'+e+'" ':""}Not String: type=${typeof t} value=${L(t)}`)},assertStringOrSymbol:function(t,e){if(!E(t)&&!function(t){return null!=t&&"symbol"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not String or Symbol: type=${typeof t} value=${L(t)}`)}};function L(t){if(null===t)return"null";if(void 0===t)return"undefined";const e=typeof t;if("string"===e)return t;if("symbol"===e)return`Symbol(${t.description})`;if("function"===e)return`Function ${t.name}(){}`;if(t instanceof String)return t.toString();if(Number.isNaN(t))return"NaN";if(t===1/0)return"Infinity";if(t===-1/0)return"-Infinity";if(t instanceof Error)return`${t.constructor.name}: ${t.message}`;if(t instanceof Promise)return"Promise";if(t instanceof Set)return`Set: ${L(Array.from(t))}`;if(t instanceof Map)return`Map: ${L(Array.from(t.entries()))}`;if(t instanceof RegExp)return t.toString();if(Array.isArray(t))return`[${t.map(L).join(", ")}]`;let r;try{r=JSON.stringify(t)}catch(e){r=t.toString()}return r}new TextDecoder,new TextEncoder;const $="DOwner$#$",{assertFunction:b,assertNotNil:k}=N;class S{constructor(t,e,r=!1){k(t,"event"),b(e,"callback"),this._event=t,this._callback=e,this._isOnce=!!r,this._owner=void 0}set owner(t){this._owner=t}get owner(){return this._owner===$?void 0:this._owner}get event(){return this._event}get isOnce(){return this._isOnce}isSameCallback(t){return this._callback===t}get callback(){return this._callback}invoke(...t){try{return this._callback(...t)}finally{if(this._isOnce)try{this._event._remove(this)}catch(t){console.warn(t)}}}listener(...t){return this.invoke(...t)}}const{isFunction:A,isNil:O}=w,{assertStringOrSymbol:z,assertFunction:W}=N;class I{static get DefaultOwner(){return $}constructor(t){z(t,"eventName"),this._name=t,this._callbacks=new Set,this._listeners=[],this._callback2Listeners=new Map,this._listener2Owner=new Map,this._owner2Listeners=new Map}get name(){return this._name}isEmpty(){return 0===this._callbacks.size}rawListeners(){return[...this._listeners]}listenerCount(t){return null==t?this._listeners.length:this._callback2Listeners.get(t)?.size??0}callbacks(){return[...this.rawListeners().map(t=>t.callback)]}emit(...t){if(0===this._listeners.length)return!1;for(const e of[...this._listeners])e.invoke(...t);return!0}emitOwner(t,...e){if(0===this._listeners.length)return!1;const r=this._owner2Listeners.get(t);if(null==r)return!1;for(const t of[...r])t.invoke(...e);return!0}hasListener(t){return!!A(t)&&this._callbacks.has(t)}hasOwner(t){return!O(t)&&this._owner2Listeners.has(t)}addListener(t,e){return this._addListener(t,e,!1,!1)}prependListener(t,e){return this._addListener(t,e,!1,!0)}addOnceListener(t,e){return this._addListener(t,e,!0,!1)}prependOnceListener(t,e){return this._addListener(t,e,!0,!0)}_addListener(t,e,r,n){if(O(t))return!1;W(t),this._callbacks.has(t)||this._callbacks.add(t),e=e??$;const i=new S(this,t,r);i.owner=e,n?this._listeners.unshift(i):this._listeners.push(i),this._listener2Owner.set(i,e);let s=this._callback2Listeners.get(t);null==s&&(s=new Set,this._callback2Listeners.set(t,s)),s.add(i);let o=this._owner2Listeners.get(e);return null==o&&(o=new Set,this._owner2Listeners.set(e,o)),o.add(i),!0}removeListener(t){if(O(t))return!1;if(!this._callbacks.has(t))return!1;this._callbacks.delete(t);const e=this._callback2Listeners.get(t);if(null==e)return!1;this._callback2Listeners.delete(t);for(const t of e){-1!==this._listeners.indexOf(t)&&this._listeners.splice(this._listeners.indexOf(t),1);const e=this._listener2Owner.get(t);if(null==e)continue;this._listener2Owner.delete(t);const r=this._owner2Listeners.get(e);null!=r&&(r.delete(t),0===r.size&&this._owner2Listeners.delete(e))}return!0}_remove(t){const e=this._listeners.indexOf(t);-1!==e&&this._listeners.splice(e,1);const{callback:r}=t,n=this._callback2Listeners.get(r);null!=n&&(n.delete(t),0===n.size&&(this._callback2Listeners.delete(r),this._callbacks.delete(r)));const i=this._listener2Owner.get(t);if(null==i)return;this._listener2Owner.delete(t);const s=this._owner2Listeners.get(i);null!=s&&(s.delete(t),0===s.size&&this._owner2Listeners.delete(i))}removeAllListeners(t){if(O(t))return this._callbacks.clear(),this._listeners.length=0,this._callback2Listeners.clear(),this._listener2Owner.clear(),this._owner2Listeners.clear(),this;const e=this._owner2Listeners.get(t);if(null==e)return this;this._owner2Listeners.delete(t);for(const t of e){-1!==this._listeners.indexOf(t)&&this._listeners.splice(this._listeners.indexOf(t),1),this._listener2Owner.delete(t);const{callback:e}=t,r=this._callback2Listeners.get(e);null!=r&&(r.delete(t),0===r.size&&(this._callback2Listeners.delete(e),this._callbacks.delete(e)))}return this}}const{isNil:T}=w,{assertString:C,assertFunction:M,assertNumber:F,assertStringOrSymbol:B,assertNotNil:P}=N,U=["on","once","addListener","prependListener","prependOnceListener","off","offAll","offOwner","removeAllListeners","removeListener","emit","emitOwner","setMaxListeners","getMaxListeners","hasOwner","listeners","listenerCount","eventNames","rawListeners"];let R=10;class j{static mixin(t){const e=new j;t.__emitter=e;for(const r of U){const n=e[r];t[r]=n.bind(e)}return t}static get defaultMaxListeners(){return R}static set defaultMaxListeners(t){F(t),R=t??10}constructor(){this._name2Event=new Map,this._maxListeners=R}addListener(t,e,r){return this.on(t,e,r)}prependListener(t,e,r){return C(t),M(e),this._checkMaxListeners(t),this._getOrCreateEvent(t).prependListener(e,r),this}prependOnceListener(t,e,r){return C(t),M(e),this._checkMaxListeners(t),this._getOrCreateEvent(t).prependOnceListener(e,r),this}emit(t,...e){const r=this._name2Event.get(t);return null!=r&&!r.isEmpty()&&(r.emit(...e),!0)}emitOwner(t,e,...r){if(null==e)throw new Error('Missing "owner"');const n=this._name2Event.get(t);return null!=n&&!n.isEmpty()&&(n.emitOwner(e,...r),!0)}eventNames(){return[...this._name2Event.keys()]}getMaxListeners(){return this._maxListeners}listenerCount(t,e){B(t,"eventName");const r=this._name2Event.get(t);return null==r||r.isEmpty()?0:r.listenerCount(e)}listeners(t){B(t,"eventName");const e=this._name2Event.get(t);return null==e||e.isEmpty()?[]:e.callbacks()}off(t,e){const r=this._name2Event.get(t);return null==r?this:(r.removeListener(e),r.isEmpty()?(this._name2Event.delete(t),this):this)}offAll(t,e){B(t,"eventName");const r=this._name2Event.get(t);return null==r?this:(r.removeAllListeners(e),r.isEmpty()?(this._name2Event.delete(t),this):this)}offOwner(t){P(t,"owner");const e=[...this._name2Event.values()];for(const r of e)r.removeAllListeners(t),r.isEmpty()&&this._name2Event.delete(r.name);return this}on(t,e,r){return C(t),M(e),this._checkMaxListeners(t),this._getOrCreateEvent(t).addListener(e,r),this}_checkMaxListeners(t){let e=0;0!==this._maxListeners&&this._maxListeners!==1/0&&(e=this.listenerCount(t))>=this._maxListeners&&console.warn(`maxlistenersexceededwarning: Possible EventEmitter memory leak detected. ${e} ${t} listeners added to [${this}]. Use emitter.setMaxListeners() to increase limit`)}once(t,e,r){return C(t),M(e),this._getOrCreateEvent(t).addOnceListener(e,r),this}rawListeners(t){return this._name2Event.get(t)?.rawListeners()||[]}removeAllListeners(t,e){return this.offAll(t,e)}removeListener(t,e){return this.off(t,e)}setMaxListeners(t){if(F(t),t<0)throw new RangeError("maxListeners must >=0");return this._maxListeners=t,this}_getOrCreateEvent(t){if(this._name2Event.has(t))return this._name2Event.get(t);const e=new I(t);return this._name2Event.set(t,e),e}hasOwner(t){if(T(t))return!1;for(const e of this._name2Event.values())if(e.hasOwner(t))return!0;return!1}}const D={Advance:"advance",Expired:"expired"};class H extends j{get Event(){return D}constructor(t){super(),this.options=t??{},this._tickInterval=this.options.tickInterval??1e3,this._tickCount=this.options.tickCount??60,this._autoStart=this.options.autoStart??!0,this._slots=Array(this._tickCount).fill(void 0).map(()=>new Map),this._cache=new Map,this._currentSlotIndex=0,this._timer=void 0,this._autoStart&&this._startAutoEvict()}get tickInterval(){return this._tickInterval}get tickCount(){return this._tickCount}get maxTtl(){return null==this._maxTtl&&(this._maxTtl=this._tickCount*this._tickInterval),this._maxTtl}assertStarted(){if(!this.autoEvictRunning)throw new Error(`${this.constructor.name} is not started`)}get autoEvictRunning(){return null!=this._timer}get autoStart(){return this._autoStart}_startAutoEvict(){this._timer=setInterval(()=>this._advance(),this._tickInterval)}_stopAutoEvict(){this._timer&&clearInterval(this._timer),this._timer=void 0}_advance(){const t=(this._currentSlotIndex+1)%this._tickCount,e=this._slots[t],r=this._slots[this._currentSlotIndex];if(this.emit(D.Advance,this._currentSlotIndex,r,e),r.size>0){for(const t of r.keys()){const e=this._cache.get(t);this._cache.delete(t),this.emit(D.Expired,t,e.value,e.expireTimestamp)}r.clear()}this._currentSlotIndex=t}start(){this.autoEvictRunning||this._startAutoEvict()}stop(){this.autoEvictRunning&&this._stopAutoEvict()}destroy(){this._stopAutoEvict(),this.clear()}set(t,e,r){if(this.assertStarted(),o.assertNotNil(t,"key"),o.assertNotNil(e,"value"),r<=0)throw new Error(`Bad ttl "${r}", must > 0`);if(r>this.maxTtl)throw new Error(`Bad ttl "${r}", must <= maxTtl(${this.maxTtl})`);this._cache.has(t)&&this.delete(t);const n=p()+r,i=Math.floor((r-1)/this._tickInterval),s=(this._currentSlotIndex+i)%this._tickCount,a={value:e,slotIndex:s,expireTimestamp:n};return this._slots[s].set(t,a),this._cache.set(t,a),!0}delete(t){const e=this._cache.get(t);if(!e)return!1;this._cache.delete(t);const{slotIndex:r}=e;return this._slots[r].delete(t),!0}has(t){const e=this._cache.get(t);return!!e&&(!(e.expireTimestamp<=p())||(this.delete(t),this.emit(D.Expired,t,e.value,e.expireTimestamp),!1))}clear(){this._cache.clear(),this._slots.forEach(t=>t.clear()),this._currentSlotIndex=0}get(t){const e=this._cache.get(t);if(e)return e.expireTimestamp<=p()?(this.delete(t),void this.emit("expired",t,e.value,e.expireTimestamp)):e.value}size(){const t=this._slots[this._currentSlotIndex];if(t.size>0){const e=p();for(const[r,{expireTimestamp:n}]of t.entries())if(n<=e){const e=this._cache.get(r);this.emit("expired",r,e.value,e.expireTimestamp),t.delete(r),this._cache.delete(r)}}return this._cache.size}}const J=6e4,V=36e5,G=864e5,q={HourToSecond:"hour->second",HourToMinute:"hour->minute",MinuteToSecond:"minute->second"},K={Downgrade:"downgrade",Expired:"expired"};class Q extends j{static get DowngradType(){return q}static get Event(){return K}constructor(t=!0){super(),this._autoStart=t??!0,this._secondWheel=new H({tickInterval:1e3,tickCount:60,autoStart:t}),this._minuteWheel=new H({tickInterval:J,tickCount:60,autoStart:t}),this._hourWheel=new H({tickInterval:V,tickCount:24,autoStart:t}),this._cache=new Map,this._init()}get maxTtl(){return 86399999}get autoEvictRunning(){return this._secondWheel.autoEvictRunning||this._minuteWheel.autoEvictRunning||this._hourWheel.autoEvictRunning}get autoStart(){return this._autoStart}_init(){this._hourWheel.on("advance",(t,e,r)=>{for(const[t,{value:e,expireTimestamp:n}]of r){let r,i=n-p();i<=0?(i=1e3,r=q.HourToSecond,this._secondWheel.set(t,e,i),this.emit(K.Downgrade,t,e,r,i)):i<J?(r=q.HourToSecond,this._secondWheel.set(t,e,i),this.emit(K.Downgrade,t,e,r,i)):(r=q.HourToMinute,this._minuteWheel.set(t,e,i)),this._hourWheel.delete(t),this.emit(K.Downgrade,t,e,r,i)}}),this._minuteWheel.on("advance",(t,e,r)=>{for(const[t,{value:e,expireTimestamp:n}]of r){let r=n-p();r<=0&&(r=999),this._secondWheel.set(t,e,r),this.emit(K.Downgrade,t,e,q.MinuteToSecond,r),this._minuteWheel.delete(t)}}),this._secondWheel.on("expired",(t,e,r)=>{this.emit(K.Expired,t,e,r)})}start(){this._secondWheel.start(),this._minuteWheel.start(),this._hourWheel.start()}stop(){this._secondWheel.stop(),this._minuteWheel.stop(),this._hourWheel.stop()}destroy(){this._secondWheel.destroy(),this._minuteWheel.destroy(),this._hourWheel.destroy()}set(t,e,r){if(r<=0)throw new Error(`Bad ttl "${r}", must > 0`);let n;if(r<J)n=this._secondWheel;else if(r<V)n=this._minuteWheel;else{if(!(r<G))throw new Error('"ttl" Should <= Millisencod Of 24 Hours');n=this._hourWheel}return n.set(t,e,r),this._cache.set(t,n),!0}delete(t){const e=this._cache.get(t);return!!e&&e.delete(t)}has(t){const e=this._cache.get(t);return!!e&&(!!e.has(t)||e!==this._secondWheel&&(e===this._hourWheel?this._minuteWheel.has(t):e===this._minuteWheel&&this._secondWheel.has(t)))}clear(){this._secondWheel.clear(),this._minuteWheel.clear(),this._hourWheel.clear(),this._cache.clear()}get(t){const e=this._cache.get(t);if(!e)return;const r=e.get(t);return null!=r?r:e!==this._secondWheel?e===this._hourWheel?this._minuteWheel.get(t):e===this._minuteWheel?this._secondWheel.get(t):void 0:void 0}size(){return this._hourWheel.size()+this._minuteWheel.size()+this._secondWheel.size()}}const{assertNotNegative:X,assertPositive:Y}=o;class Z{constructor(){this._head=void 0,this._tail=void 0,this._size=0}get size(){return this._size}get first(){return this._head?.value}get last(){return this._tail?.value}[Symbol.iterator](){let t=this._head;return{next(){if(t){const e=t.value;return t=t.next,{value:e,done:!1}}return{value:void 0,done:!0}},[Symbol.iterator](){return this}}}addFirst(t){const e={value:t,prev:void 0,next:this._head};return this._head?this._head.prev=e:this._tail=e,this._head=e,this._size++,this}addLast(t){const e={value:t,prev:this._tail,next:void 0};return this._tail?this._tail.next=e:this._head=e,this._tail=e,this._size++,this}add(t){return this.addLast(t)}removeFirst(){if(!this._head)return;const t=this._head.value;return this._head=this._head.next,this._head?this._head.prev=void 0:this._tail=void 0,this._size--,t}removeLast(){if(!this._tail)return;const t=this._tail.value;return this._tail=this._tail.prev,this._tail?this._tail.next=void 0:this._head=void 0,this._size--,t}remove(t,e){const r=e||this._defaultComparator;let n=this._head;for(;n;){if(r(n.value,t))return this._removeNode(n),!0;n=n.next}return!1}removeAll(t,e){const r=e||this._defaultComparator;let n=0,i=this._head;for(;i;){const e=i.next;r(i.value,t)&&(this._removeNode(i),n++),i=e}return n}removeAt(t){if(X(t,"index"),t>=this._size)return;if(0===t)return this.removeFirst();if(t===this._size-1)return this.removeLast();let e=this._head;for(let r=0;r<t;r++)e=e?.next;if(e){const t=e.value;return this._removeNode(e),t}}removeFromHead(t){if(Y(t,"count"),0===this._size)return!1;if(t>=this._size)return this.clear(),!0;let e=this._head;for(let r=1;r<t;r++){if(!e)return!1;e=e.next}return!!e&&(e.next?(this._head=e.next,this._head.prev=void 0):(this._head=void 0,this._tail=void 0),this._size=this._size-t,!0)}removeFromTail(t){if(Y(t,"count"),0===this._size)return!1;if(t>=this._size)return this.clear(),!0;let e=this._tail;for(let r=1;r<t;r++){if(!e)return!1;e=e.prev}return!!e&&(e.prev?(this._tail=e.prev,this._tail.next=void 0):(this._head=void 0,this._tail=void 0),this._size=this._size-t,!0)}contains(t,e){const r=e||this._defaultComparator;let n=this._head;for(;n;){if(r(n.value,t))return!0;n=n.next}return!1}indexOf(t,e){const r=e||this._defaultComparator;let n=this._head,i=0;for(;n;){if(r(n.value,t))return i;n=n.next,i++}return-1}indexLastOf(t,e){const r=e||this._defaultComparator;let n=this._tail,i=this._size-1;for(;n;){if(r(n.value,t))return i;n=n.prev,i--}return-1}get(t){if(X(t,"index"),t>=this._size)return;let e=this._head;for(let r=0;r<t;r++)e=e?.next;return e?.value}insertAt(t,e){if(X(t,"index"),0===t)return this.addFirst(e);if(t>=this._size)return this.addLast(e);let r=this._head;for(let e=0;e<t-1;e++)r=r?.next;if(r){const t={value:e,prev:r,next:r.next};r.next&&(r.next.prev=t),r.next=t,this._size++}return this}clear(){this._head=void 0,this._tail=void 0,this._size=0}toArray(){const t=[];let e=this._head;for(;e;)t.push(e.value),e=e.next;return t}values(){return this[Symbol.iterator]()}slice(t=0,e=this.size){const r=t<0?Math.max(this.size+t,0):Math.min(t,this.size),n=e<0?Math.max(this.size+e,0):Math.min(e,this.size);return r>=n?[]:t<0?this._sliceNegative(r,n):this._slicePositive(r,n)}toString(){const t=this.slice(0,3),e=this.size>3?u.safeToString(t)+"...":u.safeToString(t);return`LinkedList[${this.size}](${e})`}_slicePositive(t,e){const r=[];let n=this._head,i=0;for(;n&&i<t;)n=n.next,i++;for(;n&&i<e;)r.push(n.value),n=n.next,i++;return r}_sliceNegative(t,e){const r=[];let n=this._tail,i=this.size-1;for(;n&&i>t;)n=n.prev,i--;for(;n&&i<e;)r.push(n.value),n=n.next,i++;return r}_removeNode(t){t.prev?t.prev.next=t.next:this._head=t.next,t.next?t.next.prev=t.prev:this._tail=t.prev,this._size--}_defaultComparator(t,e){return t===e}}const{assertPositive:tt}=o;class et extends Z{constructor(t){tt(t,"capacity"),super(),this.capacity=Math.ceil(t)}addFirst(t){return this.size>=this.capacity&&this.removeLast(),super.addFirst(t),this}addLast(t){return this.size>=this.capacity&&this.removeFirst(),super.addLast(t),this}add(t){return this.addLast(t)}insertAt(t,e){return this.size>=this.capacity&&this.removeFirst(),super.insertAt(t,e),this}get remainingCapacity(){return this.capacity-this.size}get isFull(){return this.size>=this.capacity}toString(){const t=this.slice(0,3),e=this.size>3?u.safeToString(t)+"...":u.safeToString(t);return`FixedLinkedList[${this.size}/${this.capacity}](${e})`}}var rt={CappedSet:g,TimeWheelCache:H,Hour24TimeWheelCache:Q,LinkedList:Z,FixedLinkedList:et};exports.CappedSet=g,exports.FixedLinkedList=et,exports.Hour24TimeWheelCache=Q,exports.LinkedList=Z,exports.TimeWheelCache=H,exports.default=rt;
1
+ "use strict";function t(t){return null==t}function e(t){return null===t}function r(t){return null!=t&&"number"==typeof t}function n(t){return null!==t&&"object"==typeof t&&(t.constructor===Object||void 0===t.constructor)}function i(t){return null!=t&&"string"==typeof t}function s(t){return null!=t&&"symbol"==typeof t}Object.defineProperty(exports,"__esModule",{value:!0});var o={assertNumber:h,assertPositive:function(t,e){if(!function(t){return!!r(t)&&t>0}(t))throw new Error(`${e?'"'+e+'" ':""}Not Positive: ${t}`)},assertNegative:function(t,e){if(!function(t){return!!r(t)&&t<0}(t))throw new Error(`${e?'"'+e+'" ':""}Not Negative: ${t}`)},assertNotNegative:function(t,e){if(!function(t){return!!r(t)&&t>=0}(t))throw new Error(`${e?'"'+e+'" ':""}Not "0 or Positive": ${t}`)},assertBoolean:function(t,e){if(!function(t){return"boolean"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Boolean: type=${typeof t} value=${_(t)}`)},assertObject:function(t,e){if(!function(t){return null!=t&&"object"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Object: type=${typeof t} value=${_(t)}`)},assertPlainObject:function(t,e){if(!n(t))throw new Error(`${e?'"'+e+'" ':""}Not PlainObject: type=${typeof t} value=${_(t)}`)},assertSymbol:function(t,e){if(!s(t))throw new Error(`${e?'"'+e+'" ':""}Not Symbol: type=${typeof t} value=${_(t)}`)},assertFunction:function(t,e){if(!function(t){return"function"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Function: type=${typeof t} value=${_(t)}`)},assertInstance:function(t,e){if(!function(t){return null!=t&&"object"==typeof t&&!n(t)}(t))throw new Error(`${e?'"'+e+'" ':""}Not Class Instance: type=${typeof t} value=${_(t)}`)},assertPromise:function(t,e){if(!function(t){return null!=t&&"function"==typeof t.then}(t))throw new Error(`${e?'"'+e+'" ':""}Not Promise: type=${typeof t} value=${_(t)}`)},assertNil:function(e,r){if(!t(e))throw new Error(`${r?'"'+r+'" ':""}Neither Null nor Undefined: type=${typeof e} value=${_(e)}`)},assertNotNil:function(e,r){if(t(e))throw new Error((r?'"'+r+'" ':"")+"Should Not Nil")},assertNull:function(t,r){if(!e(t))throw new Error(`${r?'"'+r+'" ':""}Not Null: type=${typeof t} value=${_(t)}`)},assertNotNull:function(t,r){if(e(t))throw new Error((r?'"'+r+'" ':"")+"Should Not Null")},assertUndefined:function(t,e){if(!function(t){return void 0===t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Undefined: type=${typeof t} value=${_(t)}`)},assertString:a,assertArray:function(t,e){if(!Array.isArray(t))throw new Error(`${e?'"'+e+'" ':""}Not Array: type=${typeof t} value=${_(t)}`)},assertStringOrSymbol:function(t,e){if(!i(t)&&!s(t))throw new Error(`${e?'"'+e+'" ':""}Not String or Symbol: type=${typeof t} value=${_(t)}`)},assertInt8Array:function(t,e){if(function(t){return t instanceof Int8Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Int8Array")},assertUint8Array:function(t,e){if(function(t){return t instanceof Uint8Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Uint8Array")},assertUint8ClampedArray:function(t,e){if(function(t){return t instanceof Uint8ClampedArray}(t))throw new Error((e?'"'+e+'" ':"")+"Not Uint8ClampedArray")},assertInt16Array:function(t,e){if(function(t){return t instanceof Int16Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Int16Array")},assertUint16Array:function(t,e){if(function(t){return t instanceof Uint16Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Uint16Array")},assertInt32Array:function(t,e){if(function(t){return t instanceof Int32Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Int32Array")},assertUint32Array:function(t,e){if(function(t){return t instanceof Uint32Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Uint32Array")},assertFloat32Array:function(t,e){if(function(t){return t instanceof Float32Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Float32Array")},assertFloat64Array:function(t,e){if(function(t){return t instanceof Float64Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not Float64Array")},assertBigInt64Array:function(t,e){if(function(t){return t instanceof BigInt64Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not BigInt64Array")},assertBigUint64Array:function(t,e){if(function(t){return t instanceof BigUint64Array}(t))throw new Error((e?'"'+e+'" ':"")+"Not BigUint64Array")},assertTypedArray:function(t,e){if(function(t){return ArrayBuffer.isView(t)&&t.constructor!==DataView}(t))throw new Error((e?'"'+e+'" ':"")+"Not TypedArray")},assertArrayBuffer:function(t,e){if(!function(t){return t instanceof ArrayBuffer}(t))throw new Error((e?'"'+e+'" ':"")+"Not ArrayBuffer")}};function a(t,e){if(!i(t))throw new Error(`${e?'"'+e+'" ':""}Not String: type=${typeof t} value=${_(t)}`)}function h(t,e){if(!r(t))throw new Error(`${e?'"'+e+'" ':""}Not Number: type=${typeof t} value=${_(t)}`)}var u={isEmpty:l,assertNotEmpty:c,isBlank:f,assertNotBlank:function(t,e){if(f(t))throw new Error(`${e?'"'+e+'" ':""}Is Blank: ${t}`)},capitalize:function(t){if(a(t),0===t.length)return t;const e=t.charAt(0),r=e.toUpperCase();return e===r?t:r+t.slice(1)},decapitalize:function(t){if(a(t),0===t.length)return t;const e=t.charAt(0),r=e.toLowerCase();return e===r?t:r+t.slice(1)},splitWithFixedLength:function(t,e,r=" "){if(a(t),h(e),a(r),0===t.length)return[];if(e<=0)throw new Error("length muse >=0");if(t.length<e)return[t.padEnd(e,r)];const n=[];for(let i=0;i<t.length;i+=e){const s=t.substring(i,i+e);n.push(s.padEnd(e,r))}return n},split:function(t,...e){if(a(t),0===t.length)return[];const r=[...e];0===e.length&&e.push(",");const n=d(t,...r);if(0===n.length)return[];const i=[];let s="",o=0;for(const{marker:e,index:r}of n)s=t.substring(o,r),i.push(s),o=r+e.length;return s=t.substring(o),i.push(s),i},findMarkerPositions:function(t,...e){if(a(t),0===e.length)throw new Error("At least one marker must be provided");const r=[];for(const n of new Set(e)){if(l(n))continue;a(n);let e=t.indexOf(n);for(;-1!==e;)r.push({marker:n,index:e}),e=t.indexOf(n,e+n.length)}return r.sort((t,e)=>t.index-e.index),r},findMarkerPositionsRegex:d,substringBefore:function(t,e){if(a(t),a(e),0===t.length||0===e.length)return;const r=t.indexOf(e);return-1!==r?t.substring(0,r):void 0},substringBeforeLast:function(t,e){if(a(t),a(e),0===t.length||0===e.length)return;const r=t.lastIndexOf(e);return-1!==r?t.substring(0,r):void 0},substringAfter:function(t,e){if(a(t),a(e),0===t.length||0===e.length)return;const r=t.indexOf(e);return-1!==r?t.substring(r+e.length):void 0},substringAfterLast:function(t,e){if(a(t),a(e),0===t.length||0===e.length)return;const r=t.lastIndexOf(e);return-1!==r?t.substring(r+e.length):void 0},substringBetween:function(t,e,r){c(t),c(e),c(r);const n=t.indexOf(e);if(-1===n)return;const i=t.indexOf(r,n+e.length);return-1!==i?t.substring(n+e.length,i):void 0},substringBetweenGreedy:function(t,e,r){c(t),c(e),c(r);const n=t.indexOf(e);if(-1===n)return;const i=t.lastIndexOf(r);return-1===i||i<=n?void 0:t.substring(n+e.length,i)},substringsBetween:function(t,e,r){c(t),c(e),c(r);const n=[];let i=0;for(;;){const s=t.indexOf(e,i);if(-1===s)break;const o=t.indexOf(r,s+e.length);if(-1===o)break;n.push(t.substring(s+e.length,o)),i=o+r.length}return n},safeToString:_};function l(t){return null==t||(a(t),0===t.length)}function c(t,e){if(l(t))throw new Error(`${e?'"'+e+'" ':""}IsEmpty String: ${t}`)}function f(t){return null==t||(a(t),0===t.trim().length)}function d(t,...e){if(a(t),0===e.length)throw new Error("At least one marker must be provided");const r=[...new Set(e.filter(t=>null!=t))].map(t=>(a(t),t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"))),n=new RegExp(r.map(t=>`(${t})`).join("|"),"g"),i=[];let s=null;for(;null!==(s=n.exec(t));){for(let t=1;t<s.length;t++)if(s[t]){i.push({marker:e[t-1],index:s.index});break}0===s[0].length&&n.lastIndex++}return i}function _(t){if(null===t)return"null";if(void 0===t)return"undefined";const e=typeof t;if("string"===e)return t;if("symbol"===e)return`Symbol(${t.description})`;if("function"===e)return`Function ${t.name}(){}`;if(t instanceof String)return t.toString();if(Number.isNaN(t))return"NaN";if(t===1/0)return"Infinity";if(t===-1/0)return"-Infinity";if(t instanceof Error)return`${t.constructor.name}: ${t.message}`;if(t instanceof Promise)return"Promise";if(t instanceof Set)return`Set: ${_(Array.from(t))}`;if(t instanceof Map)return`Map: ${_(Array.from(t.entries()))}`;if(t instanceof RegExp)return t.toString();if(Array.isArray(t))return`[${t.map(_).join(", ")}]`;let r;try{r=JSON.stringify(t)}catch(e){r=t.toString()}return r}new TextDecoder,new TextEncoder;const m=1e6;var p=function(){if("undefined"!=typeof performance&&"number"==typeof performance.timeOrigin){const t=performance.timeOrigin,e=performance.now();return Math.ceil((t+e)/m)}return Date.now()};const{assertPositive:v}=o;class g{constructor(t){v(t,"capacity"),this.capacity=t,this._map=new Map,this._head=void 0,this._tail=void 0}get size(){return this._map.size}get oldest(){return this._head?.value}get newest(){return this._tail?.value}[Symbol.iterator](){return this._map.keys()}add(t){if(this._map.has(t)){const e=this._map.get(t);e&&this._removeNode(e)}else this.size>=this.capacity&&this._removeOldest();this._addNew(t)}has(t){return this._map.has(t)}delete(t){if(this._map.has(t)){const e=this._map.get(t);return e&&this._removeNode(e),!0}return!1}clear(){this._map.clear(),this._head=void 0,this._tail=void 0}values(){return this._map.keys()}_addNew(t){const e={value:t,prev:this._tail,next:void 0};this._tail?this._tail.next=e:this._head=e,this._tail=e,this._map.set(t,e)}_removeNode(t){t.prev?t.prev.next=t.next:this._head=t.next,t.next?t.next.prev=t.prev:this._tail=t.prev,this._map.delete(t.value)}_removeOldest(){this._head&&this._removeNode(this._head)}}var w={isFunction:y,isNil:x};function y(t){return"function"==typeof t}function x(t){return null==t}function E(t){return null!=t&&"string"==typeof t}var N={assertNumber:function(t,e){if(!function(t){return null!=t&&"number"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not Number: type=${typeof t} value=${L(t)}`)},assertFunction:function(t,e){if(!y(t))throw new Error(`${e?'"'+e+'" ':""}Not Function: type=${typeof t} value=${L(t)}`)},assertNotNil:function(t,e){if(x(t))throw new Error((e?'"'+e+'" ':"")+"Should Not Nil")},assertString:function(t,e){if(!E(t))throw new Error(`${e?'"'+e+'" ':""}Not String: type=${typeof t} value=${L(t)}`)},assertStringOrSymbol:function(t,e){if(!E(t)&&!function(t){return null!=t&&"symbol"==typeof t}(t))throw new Error(`${e?'"'+e+'" ':""}Not String or Symbol: type=${typeof t} value=${L(t)}`)}};function L(t){if(null===t)return"null";if(void 0===t)return"undefined";const e=typeof t;if("string"===e)return t;if("symbol"===e)return`Symbol(${t.description})`;if("function"===e)return`Function ${t.name}(){}`;if(t instanceof String)return t.toString();if(Number.isNaN(t))return"NaN";if(t===1/0)return"Infinity";if(t===-1/0)return"-Infinity";if(t instanceof Error)return`${t.constructor.name}: ${t.message}`;if(t instanceof Promise)return"Promise";if(t instanceof Set)return`Set: ${L(Array.from(t))}`;if(t instanceof Map)return`Map: ${L(Array.from(t.entries()))}`;if(t instanceof RegExp)return t.toString();if(Array.isArray(t))return`[${t.map(L).join(", ")}]`;let r;try{r=JSON.stringify(t)}catch(e){r=t.toString()}return r}new TextDecoder,new TextEncoder;const $="DOwner$#$",{assertFunction:b,assertNotNil:k}=N;class S{constructor(t,e,r=!1){k(t,"event"),b(e,"callback"),this._event=t,this._callback=e,this._isOnce=!!r,this._owner=void 0}set owner(t){this._owner=t}get owner(){return this._owner===$?void 0:this._owner}get event(){return this._event}get isOnce(){return this._isOnce}isSameCallback(t){return this._callback===t}get callback(){return this._callback}invoke(...t){try{return this._callback(...t)}finally{if(this._isOnce)try{this._event._remove(this)}catch(t){console.warn(t)}}}listener(...t){return this.invoke(...t)}}const{isFunction:A,isNil:O}=w,{assertStringOrSymbol:z,assertFunction:W}=N;class I{static get DefaultOwner(){return $}constructor(t){z(t,"eventName"),this._name=t,this._callbacks=new Set,this._listeners=[],this._callback2Listeners=new Map,this._listener2Owner=new Map,this._owner2Listeners=new Map}get name(){return this._name}isEmpty(){return 0===this._callbacks.size}rawListeners(){return[...this._listeners]}listenerCount(t){return null==t?this._listeners.length:this._callback2Listeners.get(t)?.size??0}callbacks(){return[...this.rawListeners().map(t=>t.callback)]}emit(...t){if(0===this._listeners.length)return!1;for(const e of[...this._listeners])e.invoke(...t);return!0}emitOwner(t,...e){if(0===this._listeners.length)return!1;const r=this._owner2Listeners.get(t);if(null==r)return!1;for(const t of[...r])t.invoke(...e);return!0}hasListener(t){return!!A(t)&&this._callbacks.has(t)}hasOwner(t){return!O(t)&&this._owner2Listeners.has(t)}addListener(t,e){return this._addListener(t,e,!1,!1)}prependListener(t,e){return this._addListener(t,e,!1,!0)}addOnceListener(t,e){return this._addListener(t,e,!0,!1)}prependOnceListener(t,e){return this._addListener(t,e,!0,!0)}_addListener(t,e,r,n){if(O(t))return!1;W(t),this._callbacks.has(t)||this._callbacks.add(t),e=e??$;const i=new S(this,t,r);i.owner=e,n?this._listeners.unshift(i):this._listeners.push(i),this._listener2Owner.set(i,e);let s=this._callback2Listeners.get(t);null==s&&(s=new Set,this._callback2Listeners.set(t,s)),s.add(i);let o=this._owner2Listeners.get(e);return null==o&&(o=new Set,this._owner2Listeners.set(e,o)),o.add(i),!0}removeListener(t){if(O(t))return!1;if(!this._callbacks.has(t))return!1;this._callbacks.delete(t);const e=this._callback2Listeners.get(t);if(null==e)return!1;this._callback2Listeners.delete(t);for(const t of e){-1!==this._listeners.indexOf(t)&&this._listeners.splice(this._listeners.indexOf(t),1);const e=this._listener2Owner.get(t);if(null==e)continue;this._listener2Owner.delete(t);const r=this._owner2Listeners.get(e);null!=r&&(r.delete(t),0===r.size&&this._owner2Listeners.delete(e))}return!0}_remove(t){const e=this._listeners.indexOf(t);-1!==e&&this._listeners.splice(e,1);const{callback:r}=t,n=this._callback2Listeners.get(r);null!=n&&(n.delete(t),0===n.size&&(this._callback2Listeners.delete(r),this._callbacks.delete(r)));const i=this._listener2Owner.get(t);if(null==i)return;this._listener2Owner.delete(t);const s=this._owner2Listeners.get(i);null!=s&&(s.delete(t),0===s.size&&this._owner2Listeners.delete(i))}removeAllListeners(t){if(O(t))return this._callbacks.clear(),this._listeners.length=0,this._callback2Listeners.clear(),this._listener2Owner.clear(),this._owner2Listeners.clear(),this;const e=this._owner2Listeners.get(t);if(null==e)return this;this._owner2Listeners.delete(t);for(const t of e){-1!==this._listeners.indexOf(t)&&this._listeners.splice(this._listeners.indexOf(t),1),this._listener2Owner.delete(t);const{callback:e}=t,r=this._callback2Listeners.get(e);null!=r&&(r.delete(t),0===r.size&&(this._callback2Listeners.delete(e),this._callbacks.delete(e)))}return this}}const{isNil:T}=w,{assertString:C,assertFunction:M,assertNumber:F,assertStringOrSymbol:B,assertNotNil:P}=N,U=["on","once","addListener","prependListener","prependOnceListener","off","offAll","offOwner","removeAllListeners","removeListener","emit","emitOwner","setMaxListeners","getMaxListeners","hasOwner","listeners","listenerCount","eventNames","rawListeners"];let R=10;class j{static mixin(t){const e=new j;t.__emitter=e;for(const r of U){const n=e[r];t[r]=n.bind(e)}return t}static get defaultMaxListeners(){return R}static set defaultMaxListeners(t){F(t),R=t??10}constructor(){this._name2Event=new Map,this._maxListeners=R}addListener(t,e,r){return this.on(t,e,r)}prependListener(t,e,r){return C(t),M(e),this._checkMaxListeners(t),this._getOrCreateEvent(t).prependListener(e,r),this}prependOnceListener(t,e,r){return C(t),M(e),this._checkMaxListeners(t),this._getOrCreateEvent(t).prependOnceListener(e,r),this}emit(t,...e){const r=this._name2Event.get(t);return null!=r&&!r.isEmpty()&&(r.emit(...e),!0)}emitOwner(t,e,...r){if(null==e)throw new Error('Missing "owner"');const n=this._name2Event.get(t);return null!=n&&!n.isEmpty()&&(n.emitOwner(e,...r),!0)}eventNames(){return[...this._name2Event.keys()]}getMaxListeners(){return this._maxListeners}listenerCount(t,e){B(t,"eventName");const r=this._name2Event.get(t);return null==r||r.isEmpty()?0:r.listenerCount(e)}listeners(t){B(t,"eventName");const e=this._name2Event.get(t);return null==e||e.isEmpty()?[]:e.callbacks()}off(t,e){const r=this._name2Event.get(t);return null==r?this:(r.removeListener(e),r.isEmpty()?(this._name2Event.delete(t),this):this)}offAll(t,e){B(t,"eventName");const r=this._name2Event.get(t);return null==r?this:(r.removeAllListeners(e),r.isEmpty()?(this._name2Event.delete(t),this):this)}offOwner(t){P(t,"owner");const e=[...this._name2Event.values()];for(const r of e)r.removeAllListeners(t),r.isEmpty()&&this._name2Event.delete(r.name);return this}on(t,e,r){return C(t),M(e),this._checkMaxListeners(t),this._getOrCreateEvent(t).addListener(e,r),this}_checkMaxListeners(t){let e=0;0!==this._maxListeners&&this._maxListeners!==1/0&&(e=this.listenerCount(t))>=this._maxListeners&&console.warn(`maxlistenersexceededwarning: Possible EventEmitter memory leak detected. ${e} ${t} listeners added to [${this}]. Use emitter.setMaxListeners() to increase limit`)}once(t,e,r){return C(t),M(e),this._getOrCreateEvent(t).addOnceListener(e,r),this}rawListeners(t){return this._name2Event.get(t)?.rawListeners()||[]}removeAllListeners(t,e){return this.offAll(t,e)}removeListener(t,e){return this.off(t,e)}setMaxListeners(t){if(F(t),t<0)throw new RangeError("maxListeners must >=0");return this._maxListeners=t,this}_getOrCreateEvent(t){if(this._name2Event.has(t))return this._name2Event.get(t);const e=new I(t);return this._name2Event.set(t,e),e}hasOwner(t){if(T(t))return!1;for(const e of this._name2Event.values())if(e.hasOwner(t))return!0;return!1}}const D={Advance:"advance",Expired:"expired"};class H extends j{get Event(){return D}constructor(t){super(),this.options=t??{},this._tickInterval=this.options.tickInterval??1e3,this._tickCount=this.options.tickCount??60,this._autoStart=this.options.autoStart??!0,this._slots=Array(this._tickCount).fill(void 0).map(()=>new Map),this._cache=new Map,this._currentSlotIndex=0,this._timer=void 0,this._autoStart&&this._startAutoEvict()}get first(){return this._cache.keys().next().value}get tickInterval(){return this._tickInterval}get tickCount(){return this._tickCount}get maxTtl(){return null==this._maxTtl&&(this._maxTtl=this._tickCount*this._tickInterval),this._maxTtl}assertStarted(){if(!this.autoEvictRunning)throw new Error(`${this.constructor.name} is not started`)}get autoEvictRunning(){return null!=this._timer}get autoStart(){return this._autoStart}_startAutoEvict(){this._timer=setInterval(()=>this._advance(),this._tickInterval)}_stopAutoEvict(){this._timer&&clearInterval(this._timer),this._timer=void 0}_advance(){const t=(this._currentSlotIndex+1)%this._tickCount,e=this._slots[t],r=this._slots[this._currentSlotIndex];if(this.emit(D.Advance,this._currentSlotIndex,r,e),r.size>0){for(const t of r.keys()){const e=this._cache.get(t);this._cache.delete(t),this.emit(D.Expired,t,e.value,e.expireTimestamp)}r.clear()}this._currentSlotIndex=t}start(){this.autoEvictRunning||this._startAutoEvict()}stop(){this.autoEvictRunning&&this._stopAutoEvict()}destroy(){this._stopAutoEvict(),this.clear()}set(t,e,r){if(this.assertStarted(),o.assertNotNil(t,"key"),o.assertNotNil(e,"value"),r<=0)throw new Error(`Bad ttl "${r}", must > 0`);if(r>this.maxTtl)throw new Error(`Bad ttl "${r}", must <= maxTtl(${this.maxTtl})`);this._cache.has(t)&&this.delete(t);const n=p()+r,i=Math.floor((r-1)/this._tickInterval),s=(this._currentSlotIndex+i)%this._tickCount,a={value:e,slotIndex:s,expireTimestamp:n};return this._slots[s].set(t,a),this._cache.set(t,a),!0}delete(t){const e=this._cache.get(t);if(!e)return!1;this._cache.delete(t);const{slotIndex:r}=e;return this._slots[r].delete(t),!0}deleteFirst(){const t=this.first;return null!=t&&(this.delete(t),t)}has(t){const e=this._cache.get(t);return!!e&&(!(e.expireTimestamp<=p())||(this.delete(t),this.emit(D.Expired,t,e.value,e.expireTimestamp),!1))}clear(){this._cache.clear(),this._slots.forEach(t=>t.clear()),this._currentSlotIndex=0}get(t){const e=this._cache.get(t);if(e)return e.expireTimestamp<=p()?(this.delete(t),void this.emit("expired",t,e.value,e.expireTimestamp)):e.value}size(){const t=this._slots[this._currentSlotIndex];if(t.size>0){const e=p();for(const[r,{expireTimestamp:n}]of t.entries())if(n<=e){const e=this._cache.get(r);this.emit("expired",r,e.value,e.expireTimestamp),t.delete(r),this._cache.delete(r)}}return this._cache.size}}const J=6e4,V=36e5,G=864e5,q={HourToSecond:"hour->second",HourToMinute:"hour->minute",MinuteToSecond:"minute->second"},K={Downgrade:"downgrade",Expired:"expired"};class Q extends j{static get DowngradType(){return q}static get Event(){return K}constructor(t=!0){super(),this._autoStart=t??!0,this._secondWheel=new H({tickInterval:1e3,tickCount:60,autoStart:t}),this._minuteWheel=new H({tickInterval:J,tickCount:60,autoStart:t}),this._hourWheel=new H({tickInterval:V,tickCount:24,autoStart:t}),this._cache=new Map,this._init()}get first(){return this._cache.keys().next().value}get maxTtl(){return 86399999}get autoEvictRunning(){return this._secondWheel.autoEvictRunning||this._minuteWheel.autoEvictRunning||this._hourWheel.autoEvictRunning}get autoStart(){return this._autoStart}_init(){this._hourWheel.on("advance",(t,e,r)=>{for(const[t,{value:e,expireTimestamp:n}]of r){let r,i=n-p();i<=0?(i=1e3,r=q.HourToSecond,this._secondWheel.set(t,e,i),this.emit(K.Downgrade,t,e,r,i)):i<J?(r=q.HourToSecond,this._secondWheel.set(t,e,i),this.emit(K.Downgrade,t,e,r,i)):(r=q.HourToMinute,this._minuteWheel.set(t,e,i)),this._hourWheel.delete(t),this.emit(K.Downgrade,t,e,r,i)}}),this._minuteWheel.on("advance",(t,e,r)=>{for(const[t,{value:e,expireTimestamp:n}]of r){let r=n-p();r<=0&&(r=999),this._secondWheel.set(t,e,r),this.emit(K.Downgrade,t,e,q.MinuteToSecond,r),this._minuteWheel.delete(t)}}),this._secondWheel.on("expired",(t,e,r)=>{this._cache.delete(t),this.emit(K.Expired,t,e,r)})}start(){this._secondWheel.start(),this._minuteWheel.start(),this._hourWheel.start()}stop(){this._secondWheel.stop(),this._minuteWheel.stop(),this._hourWheel.stop()}destroy(){this._secondWheel.destroy(),this._minuteWheel.destroy(),this._hourWheel.destroy()}set(t,e,r){if(r<=0)throw new Error(`Bad ttl "${r}", must > 0`);let n;if(r<J)n=this._secondWheel;else if(r<V)n=this._minuteWheel;else{if(!(r<G))throw new Error('"ttl" Should <= Millisencod Of 24 Hours');n=this._hourWheel}return n.set(t,e,r),this._cache.set(t,n),!0}delete(t){const e=this._cache.get(t);return!!e&&(this._cache.delete(t),e.delete(t))}deleteFirst(){const t=this.first;return null!=t&&(this.delete(t),t)}has(t){const e=this._cache.get(t);return!!e&&(!!e.has(t)||e!==this._secondWheel&&(e===this._hourWheel?this._minuteWheel.has(t):e===this._minuteWheel&&this._secondWheel.has(t)))}clear(){this._secondWheel.clear(),this._minuteWheel.clear(),this._hourWheel.clear(),this._cache.clear()}get(t){const e=this._cache.get(t);if(!e)return;const r=e.get(t);return null!=r?r:e!==this._secondWheel?e===this._hourWheel?this._minuteWheel.get(t):e===this._minuteWheel?this._secondWheel.get(t):void 0:void 0}size(){return this._hourWheel.size()+this._minuteWheel.size()+this._secondWheel.size()}}const{assertNotNegative:X,assertPositive:Y}=o;class Z{constructor(){this._head=void 0,this._tail=void 0,this._size=0}get size(){return this._size}get first(){return this._head?.value}get last(){return this._tail?.value}[Symbol.iterator](){let t=this._head;return{next(){if(t){const e=t.value;return t=t.next,{value:e,done:!1}}return{value:void 0,done:!0}},[Symbol.iterator](){return this}}}addFirst(t){const e={value:t,prev:void 0,next:this._head};return this._head?this._head.prev=e:this._tail=e,this._head=e,this._size++,this}addLast(t){const e={value:t,prev:this._tail,next:void 0};return this._tail?this._tail.next=e:this._head=e,this._tail=e,this._size++,this}add(t){return this.addLast(t)}removeFirst(){if(!this._head)return;const t=this._head.value;return this._head=this._head.next,this._head?this._head.prev=void 0:this._tail=void 0,this._size--,t}removeLast(){if(!this._tail)return;const t=this._tail.value;return this._tail=this._tail.prev,this._tail?this._tail.next=void 0:this._head=void 0,this._size--,t}remove(t,e){const r=e||this._defaultComparator;let n=this._head;for(;n;){if(r(n.value,t))return this._removeNode(n),!0;n=n.next}return!1}removeAll(t,e){const r=e||this._defaultComparator;let n=0,i=this._head;for(;i;){const e=i.next;r(i.value,t)&&(this._removeNode(i),n++),i=e}return n}removeAt(t){if(X(t,"index"),t>=this._size)return;if(0===t)return this.removeFirst();if(t===this._size-1)return this.removeLast();let e=this._head;for(let r=0;r<t;r++)e=e?.next;if(e){const t=e.value;return this._removeNode(e),t}}removeFromHead(t){if(Y(t,"count"),0===this._size)return!1;if(t>=this._size)return this.clear(),!0;let e=this._head;for(let r=1;r<t;r++){if(!e)return!1;e=e.next}return!!e&&(e.next?(this._head=e.next,this._head.prev=void 0):(this._head=void 0,this._tail=void 0),this._size=this._size-t,!0)}removeFromTail(t){if(Y(t,"count"),0===this._size)return!1;if(t>=this._size)return this.clear(),!0;let e=this._tail;for(let r=1;r<t;r++){if(!e)return!1;e=e.prev}return!!e&&(e.prev?(this._tail=e.prev,this._tail.next=void 0):(this._head=void 0,this._tail=void 0),this._size=this._size-t,!0)}contains(t,e){const r=e||this._defaultComparator;let n=this._head;for(;n;){if(r(n.value,t))return!0;n=n.next}return!1}indexOf(t,e){const r=e||this._defaultComparator;let n=this._head,i=0;for(;n;){if(r(n.value,t))return i;n=n.next,i++}return-1}indexLastOf(t,e){const r=e||this._defaultComparator;let n=this._tail,i=this._size-1;for(;n;){if(r(n.value,t))return i;n=n.prev,i--}return-1}get(t){if(X(t,"index"),t>=this._size)return;let e=this._head;for(let r=0;r<t;r++)e=e?.next;return e?.value}insertAt(t,e){if(X(t,"index"),0===t)return this.addFirst(e);if(t>=this._size)return this.addLast(e);let r=this._head;for(let e=0;e<t-1;e++)r=r?.next;if(r){const t={value:e,prev:r,next:r.next};r.next&&(r.next.prev=t),r.next=t,this._size++}return this}clear(){this._head=void 0,this._tail=void 0,this._size=0}toArray(){const t=[];let e=this._head;for(;e;)t.push(e.value),e=e.next;return t}values(){return this[Symbol.iterator]()}slice(t=0,e=this.size){const r=t<0?Math.max(this.size+t,0):Math.min(t,this.size),n=e<0?Math.max(this.size+e,0):Math.min(e,this.size);return r>=n?[]:t<0?this._sliceNegative(r,n):this._slicePositive(r,n)}toString(){const t=this.slice(0,3),e=this.size>3?u.safeToString(t)+"...":u.safeToString(t);return`LinkedList[${this.size}](${e})`}_slicePositive(t,e){const r=[];let n=this._head,i=0;for(;n&&i<t;)n=n.next,i++;for(;n&&i<e;)r.push(n.value),n=n.next,i++;return r}_sliceNegative(t,e){const r=[];let n=this._tail,i=this.size-1;for(;n&&i>t;)n=n.prev,i--;for(;n&&i<e;)r.push(n.value),n=n.next,i++;return r}_removeNode(t){t.prev?t.prev.next=t.next:this._head=t.next,t.next?t.next.prev=t.prev:this._tail=t.prev,this._size--}_defaultComparator(t,e){return t===e}}const{assertPositive:tt}=o;class et extends Z{constructor(t){tt(t,"capacity"),super(),this.capacity=Math.ceil(t)}addFirst(t){return this.size>=this.capacity&&this.removeLast(),super.addFirst(t),this}addLast(t){return this.size>=this.capacity&&this.removeFirst(),super.addLast(t),this}add(t){return this.addLast(t)}insertAt(t,e){return this.size>=this.capacity&&this.removeFirst(),super.insertAt(t,e),this}get remainingCapacity(){return this.capacity-this.size}get isFull(){return this.size>=this.capacity}toString(){const t=this.slice(0,3),e=this.size>3?u.safeToString(t)+"...":u.safeToString(t);return`FixedLinkedList[${this.size}/${this.capacity}](${e})`}}var rt={CappedSet:g,TimeWheelCache:H,Hour24TimeWheelCache:Q,LinkedList:Z,FixedLinkedList:et};exports.CappedSet=g,exports.FixedLinkedList=et,exports.Hour24TimeWheelCache=Q,exports.LinkedList=Z,exports.TimeWheelCache=H,exports.default=rt;
2
2
  //# sourceMappingURL=index-min.cjs.map