Skip to content

Commit c78e561

Browse files
committed
feat(core-kernel): scoped attribute indexes
1 parent 452f98a commit c78e561

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

__tests__/unit/core-kernel/services/attributes/attribute-service.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ let indexes: AttributeService;
88
beforeEach(() => (indexes = new AttributeService()));
99

1010
describe("AttributeService", () => {
11+
it("should throw if the given index does not exist in the given scope", () => {
12+
indexes.set("block", { scope: "queued" });
13+
14+
expect(() => indexes.get("block")).toThrow("Tried to get an unknown index: block");
15+
});
16+
1117
it("should return the given index", () => {
1218
indexes.set("block");
1319

packages/core-kernel/src/services/attributes/attribute-service.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { strict } from "assert";
22

33
import { injectable } from "../../ioc";
44
import { AttributeIndex } from "./attribute-index";
5+
import { IndexOptions } from "./contracts";
56

67
@injectable()
78
export 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
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface IndexOptions {
2+
scope: string;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./contracts";
12
export * from "./attribute-index";
23
export * from "./attribute-service";
34
export * from "./service-provider";

0 commit comments

Comments
 (0)