File tree Expand file tree Collapse file tree 2 files changed +13
-16
lines changed Expand file tree Collapse file tree 2 files changed +13
-16
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
+ const { Cache } = require ( 'hexo-util' ) ;
4
+
3
5
class Locals {
4
6
constructor ( ) {
5
- this . cache = { } ;
7
+ this . cache = new Cache ( ) ;
6
8
this . getters = { } ;
7
9
}
8
10
9
11
get ( name ) {
10
12
if ( typeof name !== 'string' ) throw new TypeError ( 'name must be a string!' ) ;
11
13
12
- let cache = this . cache [ name ] ;
13
-
14
- if ( cache == null ) {
14
+ return this . cache . apply ( name , ( ) => {
15
15
const getter = this . getters [ name ] ;
16
16
if ( ! getter ) return ;
17
17
18
- cache = getter ( ) ;
19
- this . cache [ name ] = cache ;
20
- }
21
-
22
- return cache ;
18
+ return getter ( ) ;
19
+ } ) ;
23
20
}
24
21
25
22
set ( name , value ) {
@@ -29,7 +26,7 @@ class Locals {
29
26
const getter = typeof value === 'function' ? value : ( ) => value ;
30
27
31
28
this . getters [ name ] = getter ;
32
- this . cache [ name ] = null ;
29
+ this . cache . del ( name ) ;
33
30
34
31
return this ;
35
32
}
@@ -38,13 +35,13 @@ class Locals {
38
35
if ( typeof name !== 'string' ) throw new TypeError ( 'name must be a string!' ) ;
39
36
40
37
this . getters [ name ] = null ;
41
- this . cache [ name ] = null ;
38
+ this . cache . del ( name ) ;
42
39
43
40
return this ;
44
41
}
45
42
46
43
invalidate ( ) {
47
- this . cache = { } ;
44
+ this . cache . flush ( ) ;
48
45
49
46
return this ;
50
47
}
Original file line number Diff line number Diff line change @@ -24,10 +24,10 @@ describe('Locals', () => {
24
24
locals . set ( 'foo' , ( ) => 'foo' ) ;
25
25
26
26
// cache should be clear after new data is set
27
- should . not . exist ( locals . cache . foo ) ;
27
+ should . not . exist ( locals . cache . cache . foo ) ;
28
28
locals . get ( 'foo' ) . should . eql ( 'foo' ) ;
29
29
// cache should be saved once it's get
30
- locals . cache . foo . should . eql ( 'foo' ) ;
30
+ locals . cache . cache . foo . should . eql ( 'foo' ) ;
31
31
} ) ;
32
32
33
33
it ( 'set() - not function' , ( ) => {
@@ -69,7 +69,7 @@ describe('Locals', () => {
69
69
locals . remove ( 'foo' ) ;
70
70
71
71
should . not . exist ( locals . getters . foo ) ;
72
- should . not . exist ( locals . cache . foo ) ;
72
+ should . not . exist ( locals . cache . cache . foo ) ;
73
73
} ) ;
74
74
75
75
it ( 'remove() - name must be a string' , ( ) => {
@@ -100,6 +100,6 @@ describe('Locals', () => {
100
100
locals . get ( 'foo' ) ;
101
101
locals . invalidate ( ) ;
102
102
103
- should . not . exist ( locals . cache . foo ) ;
103
+ should . not . exist ( locals . cache . cache . foo ) ;
104
104
} ) ;
105
105
} ) ;
You can’t perform that action at this time.
0 commit comments