Skip to content

Commit

Permalink
feat(core-kernel): scoped attribute indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Oct 18, 2019
1 parent 452f98a commit c78e561
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ let indexes: AttributeService;
beforeEach(() => (indexes = new AttributeService()));

describe("AttributeService", () => {
it("should throw if the given index does not exist in the given scope", () => {
indexes.set("block", { scope: "queued" });

expect(() => indexes.get("block")).toThrow("Tried to get an unknown index: block");
});

it("should return the given index", () => {
indexes.set("block");

Expand Down
43 changes: 31 additions & 12 deletions packages/core-kernel/src/services/attributes/attribute-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { strict } from "assert";

import { injectable } from "../../ioc";
import { AttributeIndex } from "./attribute-index";
import { IndexOptions } from "./contracts";

@injectable()
export class AttributeService {
Expand All @@ -10,47 +11,65 @@ export class AttributeService {
* @type {Map<string, AttributeIndex>}
* @memberof AttributeService
*/
private readonly indexes: Map<string, AttributeIndex> = new Map<string, AttributeIndex>();
private readonly scopes: Map<string, Map<string, AttributeIndex>> = new Map<string, Map<string, AttributeIndex>>();

/**
* @param {string} name
* @param {IndexOptions} options
* @returns {AttributeIndex}
* @memberof AttributeService
*/
public get(name: string): AttributeIndex {
strict.strictEqual(this.indexes.has(name), true, `Tried to get an unknown index: ${name}`);
public get(name: string, options: IndexOptions = { scope: "default" }): AttributeIndex {
const scope: Map<string, AttributeIndex> = this.scope(options.scope);

return this.indexes.get(name);
strict.strictEqual(scope.has(name), true, `Tried to get an unknown index: ${name}`);

return scope.get(name);
}

/**
* @param {string} name
* @param {IndexOptions} options
* @returns {boolean}
* @memberof AttributeService
*/
public set(name: string): boolean {
strict.strictEqual(this.indexes.has(name), false, `Tried to set a known index: ${name}`);
public set(name: string, options: IndexOptions = { scope: "default" }): boolean {
const scope: Map<string, AttributeIndex> = this.scope(options.scope);

strict.strictEqual(scope.has(name), false, `Tried to set a known index: ${name}`);

this.indexes.set(name, new AttributeIndex());
scope.set(name, new AttributeIndex());

return this.indexes.has(name);
return scope.has(name);
}

/**
* @param {string} name
* @param {IndexOptions} options
* @returns {boolean}
* @memberof AttributeService
*/
public forget(name: string): boolean {
return this.indexes.delete(name);
public forget(name: string, options: IndexOptions = { scope: "default" }): boolean {
return this.scope(options.scope).delete(name);
}

/**
* @param {string} name
* @param {IndexOptions} options
* @returns {boolean}
* @memberof AttributeService
*/
public has(name: string): boolean {
return this.indexes.has(name);
public has(name: string, options: IndexOptions = { scope: "default" }): boolean {
return this.scope(options.scope).has(name);
}

private scope(name: string) {
if (this.scopes.has(name)) {
return this.scopes.get(name);
}

this.scopes.set(name, new Map<string, AttributeIndex>());

return this.scopes.get(name);
}
}
3 changes: 3 additions & 0 deletions packages/core-kernel/src/services/attributes/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IndexOptions {
scope: string;
}
1 change: 1 addition & 0 deletions packages/core-kernel/src/services/attributes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./contracts";
export * from "./attribute-index";
export * from "./attribute-service";
export * from "./service-provider";

0 comments on commit c78e561

Please sign in to comment.