@@ -2,6 +2,7 @@ import { strict } from "assert";
2
2
3
3
import { injectable } from "../../ioc" ;
4
4
import { AttributeIndex } from "./attribute-index" ;
5
+ import { IndexOptions } from "./contracts" ;
5
6
6
7
@injectable ( )
7
8
export class AttributeService {
@@ -10,47 +11,65 @@ export class AttributeService {
10
11
* @type {Map<string, AttributeIndex> }
11
12
* @memberof AttributeService
12
13
*/
13
- private readonly indexes : Map < string , AttributeIndex > = new Map < string , AttributeIndex > ( ) ;
14
+ private readonly scopes : Map < string , Map < string , AttributeIndex > > = new Map < string , Map < string , AttributeIndex > > ( ) ;
14
15
15
16
/**
16
17
* @param {string } name
18
+ * @param {IndexOptions } options
17
19
* @returns {AttributeIndex }
18
20
* @memberof AttributeService
19
21
*/
20
- public get ( name : string ) : AttributeIndex {
21
- strict . strictEqual ( this . indexes . has ( name ) , true , `Tried to get an unknown index: ${ name } ` ) ;
22
+ public get ( name : string , options : IndexOptions = { scope : "default" } ) : AttributeIndex {
23
+ const scope : Map < string , AttributeIndex > = this . scope ( options . scope ) ;
22
24
23
- return this . indexes . get ( name ) ;
25
+ strict . strictEqual ( scope . has ( name ) , true , `Tried to get an unknown index: ${ name } ` ) ;
26
+
27
+ return scope . get ( name ) ;
24
28
}
25
29
26
30
/**
27
31
* @param {string } name
32
+ * @param {IndexOptions } options
28
33
* @returns {boolean }
29
34
* @memberof AttributeService
30
35
*/
31
- public set ( name : string ) : boolean {
32
- strict . strictEqual ( this . indexes . has ( name ) , false , `Tried to set a known index: ${ name } ` ) ;
36
+ public set ( name : string , options : IndexOptions = { scope : "default" } ) : boolean {
37
+ const scope : Map < string , AttributeIndex > = this . scope ( options . scope ) ;
38
+
39
+ strict . strictEqual ( scope . has ( name ) , false , `Tried to set a known index: ${ name } ` ) ;
33
40
34
- this . indexes . set ( name , new AttributeIndex ( ) ) ;
41
+ scope . set ( name , new AttributeIndex ( ) ) ;
35
42
36
- return this . indexes . has ( name ) ;
43
+ return scope . has ( name ) ;
37
44
}
38
45
39
46
/**
40
47
* @param {string } name
48
+ * @param {IndexOptions } options
41
49
* @returns {boolean }
42
50
* @memberof AttributeService
43
51
*/
44
- public forget ( name : string ) : boolean {
45
- return this . indexes . delete ( name ) ;
52
+ public forget ( name : string , options : IndexOptions = { scope : "default" } ) : boolean {
53
+ return this . scope ( options . scope ) . delete ( name ) ;
46
54
}
47
55
48
56
/**
49
57
* @param {string } name
58
+ * @param {IndexOptions } options
50
59
* @returns {boolean }
51
60
* @memberof AttributeService
52
61
*/
53
- public has ( name : string ) : boolean {
54
- return this . indexes . has ( name ) ;
62
+ public has ( name : string , options : IndexOptions = { scope : "default" } ) : boolean {
63
+ return this . scope ( options . scope ) . has ( name ) ;
64
+ }
65
+
66
+ private scope ( name : string ) {
67
+ if ( this . scopes . has ( name ) ) {
68
+ return this . scopes . get ( name ) ;
69
+ }
70
+
71
+ this . scopes . set ( name , new Map < string , AttributeIndex > ( ) ) ;
72
+
73
+ return this . scopes . get ( name ) ;
55
74
}
56
75
}
0 commit comments