diff --git a/packages/subscribable-dataset/src/SubscribableDataset.ts b/packages/subscribable-dataset/src/SubscribableDataset.ts index b2d7db1..c2f99dd 100644 --- a/packages/subscribable-dataset/src/SubscribableDataset.ts +++ b/packages/subscribable-dataset/src/SubscribableDataset.ts @@ -8,35 +8,28 @@ import type { ObjectNode, GraphNode, } from "@ldo/rdf-utils"; -import type { - Dataset, - BaseQuad, - Stream, - Term, - DatasetFactory, -} from "@rdfjs/types"; +import type { Dataset, BaseQuad, Term, DatasetFactory } from "@rdfjs/types"; import type { nodeEventListener, ISubscribableDataset, ITransactionDataset, ITransactionDatasetFactory, } from "./types"; +import { ExtendedDataset } from "@ldo/dataset"; /** * A wrapper for a dataset that allows subscriptions to be made on nodes to * be triggered whenever a quad containing that added or removed. */ export class SubscribableDataset + extends ExtendedDataset implements ISubscribableDataset { /** - * The underlying dataset factory - */ - protected datasetFactory: DatasetFactory; - /** - * The underlying dataset + * DatasetFactory for creating new datasets */ - protected dataset: Dataset; + protected datasetFactory: DatasetFactory; + /** * The underlying event emitter */ @@ -60,10 +53,10 @@ export class SubscribableDataset transactionDatasetFactory: ITransactionDatasetFactory, initialDataset?: Dataset, ) { - this.datasetFactory = datasetFactory; + super(initialDataset || datasetFactory.dataset(), datasetFactory); this.transactionDatasetFactory = transactionDatasetFactory; - this.dataset = initialDataset || this.datasetFactory.dataset(); this.eventEmitter = new EventEmitter(); + this.datasetFactory = datasetFactory; } /** @@ -72,6 +65,18 @@ export class SubscribableDataset * ================================================================== */ + /** + * A helper method that mimics what the super of addAll would be + */ + private superAddAll( + quads: Dataset | InAndOutQuad[], + ): this { + for (const quad of quads) { + super.add(quad); + } + return this; + } + /** * Imports the quads into this dataset. * This method differs from Dataset.union in that it adds all quads to the current instance, rather than combining quads and the current instance to create a new instance. @@ -81,7 +86,7 @@ export class SubscribableDataset public addAll( quads: Dataset | InAndOutQuad[], ): this { - this.dataset.addAll(quads); + this.superAddAll(quads); this.triggerSubscriptionForQuads({ added: this.datasetFactory.dataset(quads), }); @@ -94,26 +99,17 @@ export class SubscribableDataset */ public bulk(changed: DatasetChanges): this { if (changed.added) { - this.dataset.addAll(changed.added); + this.superAddAll(changed.added); } if (changed.removed) { changed.removed.forEach((quad) => { - this.dataset.delete(quad); + super.delete(quad); }); } this.triggerSubscriptionForQuads(changed); return this; } - /** - * Returns true if the current instance is a superset of the given dataset; differently put: if the given dataset is a subset of, is contained in the current dataset. - * Blank Nodes will be normalized. - * @param other - */ - public contains(other: Dataset): boolean { - return this.dataset.contains(other); - } - /** * This method removes the quads in the current instance that match the given arguments. The logic described in Quad Matching is applied for each quad in this dataset to select the quads which will be deleted. * @param subject @@ -128,192 +124,14 @@ export class SubscribableDataset object?: Term, graph?: Term, ): this { - const matching = this.dataset.match(subject, predicate, object, graph); + const matching = super.match(subject, predicate, object, graph); for (const quad of matching) { - this.dataset.delete(quad); + super.delete(quad); } this.triggerSubscriptionForQuads({ removed: matching }); return this; } - /** - * Returns a new dataset that contains alls quads from the current dataset, not included in the given dataset. - * @param other - */ - public difference( - other: Dataset, - ): Dataset { - return this.dataset.difference(other); - } - - /** - * Returns true if the current instance contains the same graph structure as the given dataset. - * @param other - */ - public equals(other: Dataset): boolean { - return this.dataset.equals(other); - } - - /** - * Universal quantification method, tests whether every quad in the dataset passes the test implemented by the provided iteratee. - * This method immediately returns boolean false once a quad that does not pass the test is found. - * This method always returns boolean true on an empty dataset. - * Note: This method is aligned with Array.prototype.every() in ECMAScript-262. - * @param iteratee - */ - public every( - iteratee: (quad: InAndOutQuad, dataset: this) => boolean, - ): boolean { - return this.dataset.every((quad) => iteratee(quad, this)); - } - - /** - * Creates a new dataset with all the quads that pass the test implemented by the provided iteratee. - * Note: This method is aligned with Array.prototype.filter() in ECMAScript-262. - * @param iteratee - */ - public filter( - iteratee: (quad: InAndOutQuad, dataset: this) => boolean, - ): Dataset { - return this.dataset.filter((quad) => iteratee(quad, this)); - } - - /** - * Executes the provided iteratee once on each quad in the dataset. - * Note: This method is aligned with Array.prototype.forEach() in ECMAScript-262. - * @param iteratee - */ - public forEach(iteratee: (quad: InAndOutQuad, dataset: this) => void): void { - return this.dataset.forEach((quad) => iteratee(quad, this)); - } - - /** - * Imports all quads from the given stream into the dataset. - * The stream events end and error are wrapped in a Promise. - * @param stream - */ - public async import(stream: Stream): Promise { - await this.dataset.import(stream); - return this; - } - - /** - * Returns a new dataset containing alls quads from the current dataset that are also included in the given dataset. - * @param other - */ - // Typescript disabled because rdf-js has incorrect typings - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - public intersection( - other: Dataset, - ): Dataset { - return this.dataset.intersection(other); - } - - /** - * Returns a new dataset containing all quads returned by applying iteratee to each quad in the current dataset. - * @param iteratee - */ - public map( - iteratee: (quad: InAndOutQuad, dataset: this) => InAndOutQuad, - ): Dataset { - return this.dataset.map((quad) => iteratee(quad, this)); - } - - /** - * This method calls the iteratee on each quad of the DatasetCore. The first time the iteratee is called, the accumulator value is the initialValue or, if not given, equals to the first quad of the Dataset. The return value of the iteratee is used as accumulator value for the next calls. - * This method returns the return value of the last iteratee call. - * Note: This method is aligned with Array.prototype.reduce() in ECMAScript-262. - * @param iteratee - * @param initialValue - */ - public reduce( - iteratee: (accumulator: A, quad: InAndOutQuad, dataset: this) => A, - initialValue?: A, - ): A { - return this.dataset.reduce( - (acc, quad) => iteratee(acc, quad, this), - initialValue, - ); - } - - /** - * Existential quantification method, tests whether some quads in the dataset pass the test implemented by the provided iteratee. - * Note: This method is aligned with Array.prototype.some() in ECMAScript-262. - * @param iteratee - * @returns boolean true once a quad that passes the test is found. - */ - public some( - iteratee: (quad: InAndOutQuad, dataset: this) => boolean, - ): boolean { - return this.dataset.some((quad) => iteratee(quad, this)); - } - - /** - * Returns the set of quads within the dataset as a host language native sequence, for example an Array in ECMAScript-262. - * Note: Since a DatasetCore is an unordered set, the order of the quads within the returned sequence is arbitrary. - */ - public toArray(): InAndOutQuad[] { - console.log("Calling toArray"); - return this.dataset.toArray(); - } - - /** - * Returns an N-Quads string representation of the dataset, preprocessed with RDF Dataset Normalization algorithm. - */ - public toCanonical(): string { - return this.dataset.toCanonical(); - } - - /** - * Returns a stream that contains all quads of the dataset. - */ - public toStream(): Stream { - return this.dataset.toStream(); - } - - /** - * Returns an N-Quads string representation of the dataset. - * No prior normalization is required, therefore the results for the same quads may vary depending on the Dataset implementation. - */ - public toString(): string { - return this.dataset.toString(); - } - - /** - * Returns a new Dataset that is a concatenation of this dataset and the quads given as an argument. - * @param other - */ - public union( - quads: Dataset, - ): Dataset { - return this.dataset.union(quads); - } - - /** - * This method returns a new dataset that is comprised of all quads in the current instance matching the given arguments. The logic described in Quad Matching is applied for each quad in this dataset to check if it should be included in the output dataset. - * @param subject - * @param predicate - * @param object - * @param graph - * @returns a Dataset with matching triples - */ - public match( - subject?: Term | null, - predicate?: Term | null, - object?: Term | null, - graph?: Term | null, - ): Dataset { - return this.dataset.match(subject, predicate, object, graph); - } - - /** - * A non-negative integer that specifies the number of quads in the set. - */ - public get size(): number { - return this.dataset.size; - } - /** * Adds the specified quad to the dataset. * Existing quads, as defined in Quad.equals, will be ignored. @@ -321,7 +139,7 @@ export class SubscribableDataset * @returns the dataset instance it was called on. */ public add(quad: InAndOutQuad): this { - this.dataset.add(quad); + super.add(quad); this.triggerSubscriptionForQuads({ added: this.datasetFactory.dataset([quad]), }); @@ -334,28 +152,13 @@ export class SubscribableDataset * @param quad */ public delete(quad: InAndOutQuad): this { - this.dataset.delete(quad); + super.delete(quad); this.triggerSubscriptionForQuads({ removed: this.datasetFactory.dataset([quad]), }); return this; } - /** - * Determines whether a dataset includes a certain quad, returning true or false as appropriate. - * @param quad - */ - public has(quad: InAndOutQuad): boolean { - return this.dataset.has(quad); - } - - /** - * Returns an iterator - */ - public [Symbol.iterator](): Iterator { - return this.dataset[Symbol.iterator](); - } - /** * ================================================================== * EVENTEMITTER METHODS diff --git a/packages/subscribable-dataset/src/TransactionDataset.ts b/packages/subscribable-dataset/src/TransactionDataset.ts index 875ad6f..4b92fff 100644 --- a/packages/subscribable-dataset/src/TransactionDataset.ts +++ b/packages/subscribable-dataset/src/TransactionDataset.ts @@ -178,7 +178,6 @@ export class TransactionDataset * Returns an iterator */ public [Symbol.iterator](): Iterator { - console.log("Getting Iterator"); const addedIterator = (this.datasetChanges.added || [])[Symbol.iterator](); let addedNext = addedIterator.next(); const parentIterator = this.parentDataset[Symbol.iterator](); @@ -251,19 +250,7 @@ export class TransactionDataset * Helper method to update the parent dataset or any other provided dataset */ private updateParentDataset(datasetChanges: DatasetChanges) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - if ((this.parentDataset as any).bulk) { - this.parentDataset.bulk(datasetChanges); - } else { - if (datasetChanges.added) { - this.parentDataset.addAll(datasetChanges.added); - } - if (datasetChanges.removed) { - datasetChanges.removed.forEach((curQuad) => { - this.parentDataset.delete(curQuad); - }); - } - } + this.parentDataset.bulk(datasetChanges); } /**