@@ -2,6 +2,7 @@ import { strict } from "assert";
22
33import { injectable } from "../../ioc" ;
44import { AttributeIndex } from "./attribute-index" ;
5+ import { IndexOptions } from "./contracts" ;
56
67@injectable ( )
78export class AttributeService {
@@ -10,47 +11,65 @@ export class AttributeService {
1011 * @type {Map<string, AttributeIndex> }
1112 * @memberof AttributeService
1213 */
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 > > ( ) ;
1415
1516 /**
1617 * @param {string } name
18+ * @param {IndexOptions } options
1719 * @returns {AttributeIndex }
1820 * @memberof AttributeService
1921 */
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 ) ;
2224
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 ) ;
2428 }
2529
2630 /**
2731 * @param {string } name
32+ * @param {IndexOptions } options
2833 * @returns {boolean }
2934 * @memberof AttributeService
3035 */
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 } ` ) ;
3340
34- this . indexes . set ( name , new AttributeIndex ( ) ) ;
41+ scope . set ( name , new AttributeIndex ( ) ) ;
3542
36- return this . indexes . has ( name ) ;
43+ return scope . has ( name ) ;
3744 }
3845
3946 /**
4047 * @param {string } name
48+ * @param {IndexOptions } options
4149 * @returns {boolean }
4250 * @memberof AttributeService
4351 */
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 ) ;
4654 }
4755
4856 /**
4957 * @param {string } name
58+ * @param {IndexOptions } options
5059 * @returns {boolean }
5160 * @memberof AttributeService
5261 */
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 ) ;
5574 }
5675}
0 commit comments