From c78e56116b514454bec3a7c1a95a31b26442b6c2 Mon Sep 17 00:00:00 2001 From: Brian Faust Date: Fri, 18 Oct 2019 06:44:20 +0300 Subject: [PATCH] feat(core-kernel): scoped attribute indexes --- .../attributes/attribute-service.test.ts | 6 +++ .../services/attributes/attribute-service.ts | 43 +++++++++++++------ .../src/services/attributes/contracts.ts | 3 ++ .../src/services/attributes/index.ts | 1 + 4 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 packages/core-kernel/src/services/attributes/contracts.ts diff --git a/__tests__/unit/core-kernel/services/attributes/attribute-service.test.ts b/__tests__/unit/core-kernel/services/attributes/attribute-service.test.ts index de4bfe96ec..532264a102 100644 --- a/__tests__/unit/core-kernel/services/attributes/attribute-service.test.ts +++ b/__tests__/unit/core-kernel/services/attributes/attribute-service.test.ts @@ -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"); diff --git a/packages/core-kernel/src/services/attributes/attribute-service.ts b/packages/core-kernel/src/services/attributes/attribute-service.ts index 88835c46a2..c2e87f25af 100644 --- a/packages/core-kernel/src/services/attributes/attribute-service.ts +++ b/packages/core-kernel/src/services/attributes/attribute-service.ts @@ -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 { @@ -10,47 +11,65 @@ export class AttributeService { * @type {Map} * @memberof AttributeService */ - private readonly indexes: Map = new Map(); + private readonly scopes: Map> = new Map>(); /** * @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 = 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 = 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()); + + return this.scopes.get(name); } } diff --git a/packages/core-kernel/src/services/attributes/contracts.ts b/packages/core-kernel/src/services/attributes/contracts.ts new file mode 100644 index 0000000000..d76f08b1c9 --- /dev/null +++ b/packages/core-kernel/src/services/attributes/contracts.ts @@ -0,0 +1,3 @@ +export interface IndexOptions { + scope: string; +} diff --git a/packages/core-kernel/src/services/attributes/index.ts b/packages/core-kernel/src/services/attributes/index.ts index 57f982471a..418dda50e0 100644 --- a/packages/core-kernel/src/services/attributes/index.ts +++ b/packages/core-kernel/src/services/attributes/index.ts @@ -1,3 +1,4 @@ +export * from "./contracts"; export * from "./attribute-index"; export * from "./attribute-service"; export * from "./service-provider";