1
1
"use strict" ;
2
2
3
- module . exports = class SortableSet extends Set {
3
+ class SortableSet extends Set {
4
4
5
5
constructor ( initialIterable , defaultSort ) {
6
6
super ( initialIterable ) ;
7
7
this . _sortFn = defaultSort ;
8
8
this . _lastActiveSortFn = null ;
9
- this . _cache = null ;
10
- this . _cacheOrderIndependent = null ;
9
+ this . _cache = undefined ;
10
+ this . _cacheOrderIndependent = undefined ;
11
11
}
12
12
13
13
/**
@@ -16,21 +16,21 @@ module.exports = class SortableSet extends Set {
16
16
*/
17
17
add ( value ) {
18
18
this . _lastActiveSortFn = null ;
19
- this . _cache = null ;
20
- this . _cacheOrderIndependent = null ;
19
+ this . _invalidateCache ( ) ;
20
+ this . _invalidateOrderedCache ( ) ;
21
21
super . add ( value ) ;
22
22
return this ;
23
23
}
24
24
25
25
delete ( value ) {
26
- this . _cache = null ;
27
- this . _cacheOrderIndependent = null ;
26
+ this . _invalidateCache ( ) ;
27
+ this . _invalidateOrderedCache ( ) ;
28
28
return super . delete ( value ) ;
29
29
}
30
30
31
31
clear ( ) {
32
- this . _cache = null ;
33
- this . _cacheOrderIndependent = null ;
32
+ this . _invalidateCache ( ) ;
33
+ this . _invalidateOrderedCache ( ) ;
34
34
return super . clear ( ) ;
35
35
}
36
36
@@ -50,7 +50,7 @@ module.exports = class SortableSet extends Set {
50
50
super . add ( sortedArray [ i ] ) ;
51
51
}
52
52
this . _lastActiveSortFn = sortFn ;
53
- this . _cache = null ;
53
+ this . _invalidateCache ( ) ;
54
54
}
55
55
56
56
/**
@@ -65,7 +65,7 @@ module.exports = class SortableSet extends Set {
65
65
* @returns {any } - returns result of fn(this), cached until set changes
66
66
*/
67
67
getFromCache ( fn ) {
68
- if ( this . _cache === null ) {
68
+ if ( this . _cache === undefined ) {
69
69
this . _cache = new Map ( ) ;
70
70
} else {
71
71
const data = this . _cache . get ( fn ) ;
@@ -77,12 +77,13 @@ module.exports = class SortableSet extends Set {
77
77
this . _cache . set ( fn , newData ) ;
78
78
return newData ;
79
79
}
80
+
80
81
/**
81
82
* @param {Function } fn - function to calculate value
82
83
* @returns {any } - returns result of fn(this), cached until set changes
83
84
*/
84
85
getFromUnorderedCache ( fn ) {
85
- if ( this . _cacheOrderIndependent === null ) {
86
+ if ( this . _cacheOrderIndependent === undefined ) {
86
87
this . _cacheOrderIndependent = new Map ( ) ;
87
88
} else {
88
89
const data = this . _cacheOrderIndependent . get ( fn ) ;
@@ -94,4 +95,16 @@ module.exports = class SortableSet extends Set {
94
95
this . _cacheOrderIndependent . set ( fn , newData ) ;
95
96
return newData ;
96
97
}
97
- } ;
98
+
99
+ _invalidateCache ( ) {
100
+ if ( this . _cache !== undefined )
101
+ this . _cache . clear ( ) ;
102
+ }
103
+
104
+ _invalidateOrderedCache ( ) {
105
+ if ( this . _cacheOrderIndependent !== undefined )
106
+ this . _cacheOrderIndependent . clear ( ) ;
107
+ }
108
+ }
109
+
110
+ module . exports = SortableSet ;
0 commit comments