@creejs/commons-collection 2.0.1 → 2.0.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@creejs/commons-collection",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Commons Collection",
5
5
  "keywords": [
6
6
  "commons",
@@ -13,9 +13,12 @@ export default class CappedSet {
13
13
  constructor(capacity: number);
14
14
  capacity: number;
15
15
  /**
16
+ * 1. key is the Value stored in CappedSet
17
+ * 2. value is a Node
18
+ * 3. JS Map preserve the insertion order
16
19
  * @type {Map<any, Node>}
17
20
  */
18
- _set: Map<any, Node>;
21
+ _map: Map<any, Node>;
19
22
  /**
20
23
  * @type {Node|undefined}
21
24
  */
@@ -25,7 +28,13 @@ export default class CappedSet {
25
28
  */
26
29
  _tail: Node | undefined;
27
30
  get size(): number;
31
+ /**
32
+ * get the oldest element
33
+ */
28
34
  get oldest(): any;
35
+ /**
36
+ * get the newest element
37
+ */
29
38
  get newest(): any;
30
39
  /**
31
40
  * Adds a value to the capped set. If the value already exists, it will be moved to the most recent position.
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Hour-Minute-Second-Time-Wheel Cache
3
+ * 1. TTL must be less than 24 Hours
4
+ */
5
+ export default class Hour24TimeWheelCache extends EventEmitter {
6
+ static get DowngradType(): {
7
+ HourToSecond: string;
8
+ HourToMinute: string;
9
+ MinuteToSecond: string;
10
+ };
11
+ static get Event(): {
12
+ Downgrade: string;
13
+ Expired: string;
14
+ };
15
+ /**
16
+ * Second Wheel:
17
+ * 1. 1 Tick Mark is 1 Second, is 1000 Milliseconds
18
+ * 2. 60 Slots, maximumly, SencondWheel can contain 60 Seconds
19
+ * 3. 60 Seconds should be stored in MinuteWheel
20
+ * * 01 00:00:00.XXX -> 00:00:59.XXX in Slot01 Index00
21
+ * * 02 00:00:01.XXX -> 00:00:01.XXX in Slot02 Index01
22
+ * * 60 00:00:59.XXX -> 00:00:59.XXX in Slot60 Index59
23
+ * @type {TimeWheelCache}
24
+ */
25
+ _secondWheel: TimeWheelCache;
26
+ /**
27
+ * Minute Wheel:
28
+ * 1. 1 Tick Mark is 1 Minute, is 60 * 1000 Milliseconds
29
+ * 2. 60 Slots, maximumly, MinuteWheel can contain 60 Minutes
30
+ * * 01 00:00:00 -> 00:00:59 in Slot01 Index00
31
+ * * 02 00:01:00 -> 00:01:59 in Slot02 Index01
32
+ * * 60 00:59:00 -> 00:59:59 in Slot60 Index59
33
+ * @type {TimeWheelCache}
34
+ */
35
+ _minuteWheel: TimeWheelCache;
36
+ /**
37
+ * Hour Wheel:
38
+ * 1. 1 Tick Mark is 1 Hour, is 60 * 60 * 1000 Milliseconds
39
+ * 2. 24 Slots, maximumly, HourWheel can contain 23:59:59
40
+ * * 01 00:00:00 -> 00:59:59 in Slot01 Index00
41
+ * * 02 01:00:00 -> 01:59:59 in Slot02 Index01
42
+ * * 24 23:00:00 -> 23:59:59 in Slot23 Index23
43
+ * @type {TimeWheelCache}
44
+ */
45
+ _hourWheel: TimeWheelCache;
46
+ /**
47
+ * @type {Map<any, TimeWheelCache>}
48
+ */
49
+ _cache: Map<any, TimeWheelCache>;
50
+ /**
51
+ * Max Time to Live, atom unit "millisecond"
52
+ * @returns {number}
53
+ */
54
+ get maxTtl(): number;
55
+ get autoEvictRunning(): boolean;
56
+ _init(): void;
57
+ destroy(): void;
58
+ /**
59
+ * @param {any} key
60
+ * @param {any} value
61
+ * @param {number} ttl Time To Live, unit "millisencond", ttl should < 24 Hours
62
+ * @returns {boolean}
63
+ */
64
+ set(key: any, value: any, ttl: number): boolean;
65
+ /**
66
+ * @param {any} key
67
+ * @returns {boolean}
68
+ */
69
+ delete(key: any): boolean;
70
+ /**
71
+ * Checks if the cache contains the specified key.
72
+ * @param {any} key - The key to check for existence in the cache.
73
+ * @returns {boolean} - True if the key exists in the cache, false otherwise.
74
+ */
75
+ has(key: any): boolean;
76
+ clear(): void;
77
+ /**
78
+ *
79
+ * @param {any} key
80
+ * @returns {any}
81
+ */
82
+ get(key: any): any;
83
+ size(): number;
84
+ }
85
+ export type Timestamp = number;
86
+ import { EventEmitter } from '@creejs/commons-events';
87
+ import TimeWheelCache from './time-wheel-cache.js';
88
+ export namespace DowngradType {
89
+ let HourToSecond: string;
90
+ let HourToMinute: string;
91
+ let MinuteToSecond: string;
92
+ }
93
+ export namespace Event {
94
+ let Downgrade: string;
95
+ let Expired: string;
96
+ }
package/types/index.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  declare namespace _default {
2
2
  export { CappedSet };
3
+ export { TimeWheelCache };
4
+ export { Hour24TimeWheelCache };
3
5
  }
4
6
  export default _default;
5
- export { CappedSet };
6
7
  import CappedSet from './capped-set.js';
8
+ import TimeWheelCache from './time-wheel-cache.js';
9
+ import Hour24TimeWheelCache from './hour24-time-wheel-cache.js';
10
+ export { CappedSet, TimeWheelCache, Hour24TimeWheelCache };
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Not Found Better Implementation in npmjs.com, so do it by ourselves.
3
+ *
4
+ * Key Points:
5
+ * 1. Basic Atom Unit is "tick", 1 tick = 1 tickInterval
6
+ * 3. How many milliseconds does 1 Tick represent is defined by "tickInterval"
7
+ * 4. How many Ticks does the Wheel own is defined by "tickCount"
8
+ */
9
+ export default class TimeWheelCache extends EventEmitter {
10
+ /**
11
+ * @param {TimeWheelCacheOptions} [options]
12
+ */
13
+ constructor(options?: TimeWheelCacheOptions);
14
+ get Event(): {
15
+ Advance: string;
16
+ Expired: string;
17
+ };
18
+ options: {};
19
+ /**
20
+ * How many milliseconds does one Tick represent
21
+ */
22
+ _tickInterval: number;
23
+ /**
24
+ * How many Ticks does the Wheel have
25
+ */
26
+ _tickCount: number;
27
+ /**
28
+ * Slots, one Tick owns one Slot to store key and expire timestamp
29
+ * @type {Map<any, {value: any, slotIndex: number, expireTimestamp: number}>[]}
30
+ */
31
+ _slots: Map<any, {
32
+ value: any;
33
+ slotIndex: number;
34
+ expireTimestamp: number;
35
+ }>[];
36
+ /**
37
+ * Data Cache
38
+ * @type {Map<any, {value: any, slotIndex: number, expireTimestamp: number}>}
39
+ */
40
+ _cache: Map<any, {
41
+ value: any;
42
+ slotIndex: number;
43
+ expireTimestamp: number;
44
+ }>;
45
+ _currentSlotIndex: number;
46
+ /** @type {NodeJS.Timeout|undefined} */
47
+ _timer: NodeJS.Timeout | undefined;
48
+ get tickInterval(): number;
49
+ get tickCount(): number;
50
+ /**
51
+ * Max Time to Live, atom unit "millisecond"
52
+ * @returns {number}
53
+ */
54
+ get maxTtl(): number;
55
+ _maxTtl: number | undefined;
56
+ get autoEvictRunning(): boolean;
57
+ _startAutoEvict(): void;
58
+ _stopAutoEvict(): void;
59
+ _advance(): void;
60
+ destroy(): void;
61
+ /**
62
+ * 1. "key" is not Nil
63
+ * 2. "value" is not Nil
64
+ * 3. "ttl" must > 0 && <= maxTtl, maxTtl = tickCount * tickInterval
65
+ * * > 0: CAN NOT go to past
66
+ * * < maxTtl: Wheel is Round, eg.
67
+ * * "Hour Wheel" has 24 Hour-Tick-Mark
68
+ * * CAN NOT distinguish 24 hours and 48 hours
69
+ * 4. Understand the difference between "ttl" and "timestamp"
70
+ * * TTL is Indexed from 1
71
+ * * eg. ttl = 60, should be stored in Slot[59]
72
+ * * Timestamp is Indexed from 0
73
+ * @param {any} key
74
+ * @param {any} value
75
+ * @param {number} ttl Time to Live, atom unit "millisecond"
76
+ * @returns {boolean}
77
+ */
78
+ set(key: any, value: any, ttl: number): boolean;
79
+ /**
80
+ * @param {any} key
81
+ * @returns {boolean}
82
+ */
83
+ delete(key: any): boolean;
84
+ /**
85
+ * Checks if the cache contains the specified key.
86
+ * @param {any} key - The key to check for existence in the cache.
87
+ * @returns {boolean} - True if the key exists in the cache, false otherwise.
88
+ */
89
+ has(key: any): boolean;
90
+ clear(): void;
91
+ /**
92
+ *
93
+ * @param {any} key
94
+ * @returns {any}
95
+ */
96
+ get(key: any): any;
97
+ size(): number;
98
+ }
99
+ export type Timestamp = number;
100
+ export type TimeWheelCacheOptions = {
101
+ tickInterval: number;
102
+ tickCount: number;
103
+ };
104
+ import { EventEmitter } from '@creejs/commons-events';