11"use strict" ;
2- //TODO: Make this a generic type
3- //https://github.com/Microsoft/TypeScript/issues/23385
4- //https://github.com/Microsoft/TypeScript/issues/23384
2+
3+ /**
4+ * A subset of Set that offers sorting functionality
5+ * @template T item type in set
6+ */
57class SortableSet extends Set {
8+ // eslint-disable-next-line valid-jsdoc
9+ /**
10+ * Create a new sortable set
11+ * @param {Array<T>= } initialIterable The initial iterable value
12+ * @param {SortFunction= } defaultSort Default sorting function
13+ * @typedef {(a: T, b: T) => number } SortFunction
14+ */
615 constructor ( initialIterable , defaultSort ) {
716 super ( initialIterable ) ;
17+ /** @private @type {(a: T, b: T) => number }} */
818 this . _sortFn = defaultSort ;
19+ /** @private @type {(a: T, b: T) => number } | null} */
920 this . _lastActiveSortFn = null ;
21+ /** @private @type {Map<Function, T> | undefined } */
1022 this . _cache = undefined ;
23+ /** @private @type {Map<Function, T> | undefined } */
1124 this . _cacheOrderIndependent = undefined ;
1225 }
1326
1427 /**
15- * @param {TODO } value - value to add to set
16- * @returns {this } - returns itself
28+ * @param {T } value value to add to set
29+ * @returns {this } returns itself
1730 */
1831 add ( value ) {
1932 this . _lastActiveSortFn = null ;
@@ -23,19 +36,31 @@ class SortableSet extends Set {
2336 return this ;
2437 }
2538
39+ /**
40+ * @param {T } value value to delete
41+ * @returns {boolean } true if value existed in set, false otherwise
42+ */
2643 delete ( value ) {
2744 this . _invalidateCache ( ) ;
2845 this . _invalidateOrderedCache ( ) ;
2946 return super . delete ( value ) ;
3047 }
3148
49+ /**
50+ * @returns {void }
51+ */
3252 clear ( ) {
3353 this . _invalidateCache ( ) ;
3454 this . _invalidateOrderedCache ( ) ;
3555 return super . clear ( ) ;
3656 }
3757
38- sortWith ( /** @type {(a: TODO, b: TODO) => number } */ sortFn ) {
58+ /**
59+ * Sort with a comparer function
60+ * @param {SortFunction } sortFn Sorting comparer function
61+ * @returns {void }
62+ */
63+ sortWith ( sortFn ) {
3964 if ( this . size <= 1 || sortFn === this . _lastActiveSortFn ) {
4065 // already sorted - nothing to do
4166 return ;
@@ -50,16 +75,14 @@ class SortableSet extends Set {
5075 this . _invalidateCache ( ) ;
5176 }
5277
53- /**
54- * @returns {void }
55- */
5678 sort ( ) {
5779 this . sortWith ( this . _sortFn ) ;
5880 }
5981
82+ // eslint-disable-next-line valid-jsdoc
6083 /**
61- * @param {Function } fn - function to calculate value
62- * @returns {TODO } - returns result of fn(this), cached until set changes
84+ * @param {(instance: SortableSet<T>) => TODO } fn function to calculate value
85+ * @returns {TODO } returns result of fn(this), cached until set changes
6386 */
6487 getFromCache ( fn ) {
6588 if ( this . _cache === undefined ) {
@@ -76,8 +99,8 @@ class SortableSet extends Set {
7699 }
77100
78101 /**
79- * @param {Function } fn - function to calculate value
80- * @returns {TODO } - returns result of fn(this), cached until set changes
102+ * @param {Function } fn function to calculate value
103+ * @returns {any } returns result of fn(this), cached until set changes
81104 */
82105 getFromUnorderedCache ( fn ) {
83106 if ( this . _cacheOrderIndependent === undefined ) {
@@ -93,12 +116,20 @@ class SortableSet extends Set {
93116 return newData ;
94117 }
95118
119+ /**
120+ * @private
121+ * @returns {void }
122+ */
96123 _invalidateCache ( ) {
97124 if ( this . _cache !== undefined ) {
98125 this . _cache . clear ( ) ;
99126 }
100127 }
101128
129+ /**
130+ * @private
131+ * @returns {void }
132+ */
102133 _invalidateOrderedCache ( ) {
103134 if ( this . _cacheOrderIndependent !== undefined ) {
104135 this . _cacheOrderIndependent . clear ( ) ;
0 commit comments