diff --git a/package.json b/package.json index e0d7391..3efa1ec 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,7 @@ "ts-jest": "^24.3.0", "tslint": "^5.20.1", "typedoc": "^0.16.2", - "typescript": "^3.9.2" + "typescript": "^4.0.5" }, "babel": { "presets": [ diff --git a/src/store/BasicStore.ts b/src/store/BasicStore.ts index 4e7fc26..341f90a 100644 --- a/src/store/BasicStore.ts +++ b/src/store/BasicStore.ts @@ -19,6 +19,9 @@ export default class BasicStore implements LowLevelStore { public readonly dataCallbacks: Array<(quad: Quad) => void>; public readonly removeCallback: ((quad: Quad) => void) | undefined; + /** Returns the number of quads in the store. */ + public length: number = 0; + constructor(opts: Partial = {}) { this.dataCallbacks = []; this.quads = opts.quads || []; @@ -26,6 +29,15 @@ export default class BasicStore implements LowLevelStore { this.rdfArrayRemove = this.rdfFactory.supports?.[Feature.identity] ? this.identityRemove : this.searchRemove; + + Object.defineProperty(this, "length", { + get(): number { + return this.quads.length; + }, + set(value: number): void { + this.quads.length = value; + }, + }); } /** Add a quad to the store. */ @@ -78,11 +90,6 @@ export default class BasicStore implements LowLevelStore { this.dataCallbacks.push(callback); } - /** Returns the number of quads in the store. */ - public get length(): number { - return this.quads.length; - } - /** Remove a quad from the store */ public remove(st: Quad): this { const sts = this.match( diff --git a/src/store/RDFIndex.ts b/src/store/RDFIndex.ts index 20512be..d5673ee 100644 --- a/src/store/RDFIndex.ts +++ b/src/store/RDFIndex.ts @@ -20,6 +20,9 @@ export type PropertyActionCallback = (quad: Quad) => void; /** Query and modify an array of quads. */ export default class RDFIndex extends Equatable(Indexable(BasicStore)) implements LowLevelStore { + /** Returns the number of quads in the store. */ + public length: number = 0; + private readonly propertyActions: PropertyActionCallback[][] = []; /** @@ -29,7 +32,17 @@ export default class RDFIndex extends Equatable(Indexable(BasicStore)) implement */ constructor(opts: Partial = {}) { super(opts); + this.addDataCallback(this.processPropertyAction.bind(this)); + + Object.defineProperty(this, "length", { + get(): number { + return this.quads.length; + }, + set(value: number): void { + this.quads.length = value; + }, + }); } public any( @@ -64,20 +77,6 @@ export default class RDFIndex extends Equatable(Indexable(BasicStore)) implement return this.match(subject, predicate, object, graph, true)?.[0]; } - /** - * Returns the number of quads contained in this IndexedFormula. - * (Getter proxy to this.quads). - * Usage: - * ``` - * const kb = rdf.graph() - * kb.length // -> 0 - * ``` - * @returns {Number} - */ - public get length(): number { - return this.quads.length; - } - public holds(s: SomeNode, p: NamedNode, o: SomeTerm, g: SomeNode): boolean { return this.match(s, p, o, g, true)?.[0] !== undefined; } diff --git a/yarn.lock b/yarn.lock index 4818ad9..392944e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6550,10 +6550,10 @@ typescript@3.7.x: resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae" integrity sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw== -typescript@^3.9.2: - version "3.9.2" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.2.tgz#64e9c8e9be6ea583c54607677dd4680a1cf35db9" - integrity sha512-q2ktq4n/uLuNNShyayit+DTobV2ApPEo/6so68JaD5ojvc/6GClBipedB9zNWYxRSAlZXAe405Rlijzl6qDiSw== +typescript@^4.0.5: + version "4.0.5" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389" + integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ== uglify-js@^3.1.4: version "3.8.0"