@ldo/subscribable-dataset 0.0.1-alpha.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/ProxyTransactionalDataset.d.ts +27 -0
  2. package/dist/ProxyTransactionalDataset.js +138 -0
  3. package/dist/ProxyTransactionalDataset.js.map +1 -0
  4. package/dist/WrapperSubscribableDataset.d.ts +53 -0
  5. package/dist/WrapperSubscribableDataset.js +236 -0
  6. package/dist/WrapperSubscribableDataset.js.map +1 -0
  7. package/dist/WrapperSubscribableDatasetFactory.d.ts +7 -0
  8. package/dist/WrapperSubscribableDatasetFactory.js +16 -0
  9. package/dist/WrapperSubscribableDatasetFactory.js.map +1 -0
  10. package/dist/createWrapperSubscribableDataset.d.ts +5 -0
  11. package/dist/createWrapperSubscribableDataset.js +23 -0
  12. package/dist/createWrapperSubscribableDataset.js.map +1 -0
  13. package/dist/createWrapperSubscribableDatasetFromSerializedInput.d.ts +4 -0
  14. package/dist/createWrapperSubscribableDatasetFromSerializedInput.js +10 -0
  15. package/dist/createWrapperSubscribableDatasetFromSerializedInput.js.map +1 -0
  16. package/dist/index.d.ts +7 -0
  17. package/dist/index.js +34 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/mergeDatasetChanges.d.ts +3 -0
  20. package/dist/mergeDatasetChanges.js +39 -0
  21. package/dist/mergeDatasetChanges.js.map +1 -0
  22. package/dist/types.d.ts +30 -0
  23. package/dist/types.js +3 -0
  24. package/dist/types.js.map +1 -0
  25. package/package.json +47 -0
  26. package/src/ProxyTransactionalDataset.ts +317 -0
  27. package/src/WrapperSubscribableDataset.ts +616 -0
  28. package/src/WrapperSubscribableDatasetFactory.ts +27 -0
  29. package/src/createWrapperSubscribableDataset.ts +30 -0
  30. package/src/createWrapperSubscribableDatasetFromSerializedInput.ts +28 -0
  31. package/src/index.ts +10 -0
  32. package/src/mergeDatasetChanges.ts +53 -0
  33. package/src/types.ts +144 -0
@@ -0,0 +1,616 @@
1
+ import { EventEmitter } from "events";
2
+ import { quadMatchToString, stringToQuadMatch } from "@ldo/rdf-utils";
3
+ import type {
4
+ DatasetChanges,
5
+ QuadMatch,
6
+ SubjectNode,
7
+ PredicateNode,
8
+ ObjectNode,
9
+ GraphNode,
10
+ } from "@ldo/rdf-utils";
11
+ import type {
12
+ Dataset,
13
+ BaseQuad,
14
+ Stream,
15
+ Term,
16
+ DatasetFactory,
17
+ } from "@rdfjs/types";
18
+ import type {
19
+ nodeEventListener,
20
+ SubscribableDataset,
21
+ TransactionalDataset,
22
+ } from "./types";
23
+ import ProxyTransactionalDataset from "./ProxyTransactionalDataset";
24
+
25
+ /**
26
+ * A wrapper for a dataset that allows subscriptions to be made on nodes to
27
+ * be triggered whenever a quad containing that added or removed.
28
+ */
29
+ export default class WrapperSubscribableDataset<
30
+ InAndOutQuad extends BaseQuad = BaseQuad,
31
+ > implements SubscribableDataset<InAndOutQuad>
32
+ {
33
+ /**
34
+ * The underlying dataset factory
35
+ */
36
+ private datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>;
37
+ /**
38
+ * The underlying dataset
39
+ */
40
+ private dataset: Dataset<InAndOutQuad, InAndOutQuad>;
41
+ /**
42
+ * The underlying event emitter
43
+ */
44
+ private eventEmitter: EventEmitter;
45
+ /**
46
+ * Helps find all the events for a given listener
47
+ */
48
+ private listenerHashMap: Map<nodeEventListener<InAndOutQuad>, Set<string>> =
49
+ new Map();
50
+
51
+ /**
52
+ *
53
+ * @param datasetFactory
54
+ * @param initialDataset
55
+ */
56
+ constructor(
57
+ datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>,
58
+ initialDataset?: Dataset<InAndOutQuad, InAndOutQuad>,
59
+ ) {
60
+ this.datasetFactory = datasetFactory;
61
+ this.dataset = initialDataset || this.datasetFactory.dataset();
62
+ this.eventEmitter = new EventEmitter();
63
+ }
64
+
65
+ /**
66
+ * ==================================================================
67
+ * DATASET METHODS
68
+ * ==================================================================
69
+ */
70
+
71
+ /**
72
+ * Imports the quads into this dataset.
73
+ * 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.
74
+ * @param quads
75
+ * @returns the dataset instance it was called on.
76
+ */
77
+ public addAll(
78
+ quads: Dataset<InAndOutQuad, InAndOutQuad> | InAndOutQuad[],
79
+ ): this {
80
+ this.dataset.addAll(quads);
81
+ this.triggerSubscriptionForQuads({
82
+ added: this.datasetFactory.dataset(quads),
83
+ });
84
+ return this;
85
+ }
86
+
87
+ /**
88
+ * Bulk add and remove triples
89
+ * @param changed
90
+ */
91
+ public bulk(changed: DatasetChanges<InAndOutQuad>): this {
92
+ if (changed.added) {
93
+ this.dataset.addAll(changed.added);
94
+ }
95
+ if (changed.removed) {
96
+ changed.removed.forEach((quad) => {
97
+ this.dataset.delete(quad);
98
+ });
99
+ }
100
+ this.triggerSubscriptionForQuads(changed);
101
+ return this;
102
+ }
103
+
104
+ /**
105
+ * 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.
106
+ * Blank Nodes will be normalized.
107
+ * @param other
108
+ */
109
+ public contains(other: Dataset<InAndOutQuad, InAndOutQuad>): boolean {
110
+ return this.dataset.contains(other);
111
+ }
112
+
113
+ /**
114
+ * 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.
115
+ * @param subject
116
+ * @param predicate
117
+ * @param object
118
+ * @param graph
119
+ * @returns the dataset instance it was called on.
120
+ */
121
+ public deleteMatches(
122
+ subject?: Term,
123
+ predicate?: Term,
124
+ object?: Term,
125
+ graph?: Term,
126
+ ): this {
127
+ const matching = this.dataset.match(subject, predicate, object, graph);
128
+ for (const quad of matching) {
129
+ this.dataset.delete(quad);
130
+ }
131
+ this.triggerSubscriptionForQuads({ removed: matching });
132
+ return this;
133
+ }
134
+
135
+ /**
136
+ * Returns a new dataset that contains alls quads from the current dataset, not included in the given dataset.
137
+ * @param other
138
+ */
139
+ public difference(
140
+ other: Dataset<InAndOutQuad, InAndOutQuad>,
141
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
142
+ return this.dataset.difference(other);
143
+ }
144
+
145
+ /**
146
+ * Returns true if the current instance contains the same graph structure as the given dataset.
147
+ * @param other
148
+ */
149
+ public equals(other: Dataset<InAndOutQuad, InAndOutQuad>): boolean {
150
+ return this.dataset.equals(other);
151
+ }
152
+
153
+ /**
154
+ * Universal quantification method, tests whether every quad in the dataset passes the test implemented by the provided iteratee.
155
+ * This method immediately returns boolean false once a quad that does not pass the test is found.
156
+ * This method always returns boolean true on an empty dataset.
157
+ * Note: This method is aligned with Array.prototype.every() in ECMAScript-262.
158
+ * @param iteratee
159
+ */
160
+ public every(
161
+ iteratee: (quad: InAndOutQuad, dataset: this) => boolean,
162
+ ): boolean {
163
+ return this.dataset.every((quad) => iteratee(quad, this));
164
+ }
165
+
166
+ /**
167
+ * Creates a new dataset with all the quads that pass the test implemented by the provided iteratee.
168
+ * Note: This method is aligned with Array.prototype.filter() in ECMAScript-262.
169
+ * @param iteratee
170
+ */
171
+ public filter(
172
+ iteratee: (quad: InAndOutQuad, dataset: this) => boolean,
173
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
174
+ return this.dataset.filter((quad) => iteratee(quad, this));
175
+ }
176
+
177
+ /**
178
+ * Executes the provided iteratee once on each quad in the dataset.
179
+ * Note: This method is aligned with Array.prototype.forEach() in ECMAScript-262.
180
+ * @param iteratee
181
+ */
182
+ public forEach(iteratee: (quad: InAndOutQuad, dataset: this) => void): void {
183
+ return this.dataset.forEach((quad) => iteratee(quad, this));
184
+ }
185
+
186
+ /**
187
+ * Imports all quads from the given stream into the dataset.
188
+ * The stream events end and error are wrapped in a Promise.
189
+ * @param stream
190
+ */
191
+ public async import(stream: Stream<InAndOutQuad>): Promise<this> {
192
+ await this.dataset.import(stream);
193
+ return this;
194
+ }
195
+
196
+ /**
197
+ * Returns a new dataset containing alls quads from the current dataset that are also included in the given dataset.
198
+ * @param other
199
+ */
200
+ // Typescript disabled because rdf-js has incorrect typings
201
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
202
+ // @ts-ignore
203
+ public intersection(
204
+ other: Dataset<InAndOutQuad, InAndOutQuad>,
205
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
206
+ return this.dataset.intersection(other);
207
+ }
208
+
209
+ /**
210
+ * Returns a new dataset containing all quads returned by applying iteratee to each quad in the current dataset.
211
+ * @param iteratee
212
+ */
213
+ public map(
214
+ iteratee: (quad: InAndOutQuad, dataset: this) => InAndOutQuad,
215
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
216
+ return this.dataset.map((quad) => iteratee(quad, this));
217
+ }
218
+
219
+ /**
220
+ * 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.
221
+ * This method returns the return value of the last iteratee call.
222
+ * Note: This method is aligned with Array.prototype.reduce() in ECMAScript-262.
223
+ * @param iteratee
224
+ * @param initialValue
225
+ */
226
+ public reduce<A = unknown>(
227
+ iteratee: (accumulator: A, quad: InAndOutQuad, dataset: this) => A,
228
+ initialValue?: A,
229
+ ): A {
230
+ return this.dataset.reduce(
231
+ (acc, quad) => iteratee(acc, quad, this),
232
+ initialValue,
233
+ );
234
+ }
235
+
236
+ /**
237
+ * Existential quantification method, tests whether some quads in the dataset pass the test implemented by the provided iteratee.
238
+ * Note: This method is aligned with Array.prototype.some() in ECMAScript-262.
239
+ * @param iteratee
240
+ * @returns boolean true once a quad that passes the test is found.
241
+ */
242
+ public some(
243
+ iteratee: (quad: InAndOutQuad, dataset: this) => boolean,
244
+ ): boolean {
245
+ return this.dataset.some((quad) => iteratee(quad, this));
246
+ }
247
+
248
+ /**
249
+ * Returns the set of quads within the dataset as a host language native sequence, for example an Array in ECMAScript-262.
250
+ * Note: Since a DatasetCore is an unordered set, the order of the quads within the returned sequence is arbitrary.
251
+ */
252
+ public toArray(): InAndOutQuad[] {
253
+ return this.dataset.toArray();
254
+ }
255
+
256
+ /**
257
+ * Returns an N-Quads string representation of the dataset, preprocessed with RDF Dataset Normalization algorithm.
258
+ */
259
+ public toCanonical(): string {
260
+ return this.dataset.toCanonical();
261
+ }
262
+
263
+ /**
264
+ * Returns a stream that contains all quads of the dataset.
265
+ */
266
+ public toStream(): Stream<InAndOutQuad> {
267
+ return this.dataset.toStream();
268
+ }
269
+
270
+ /**
271
+ * Returns an N-Quads string representation of the dataset.
272
+ * No prior normalization is required, therefore the results for the same quads may vary depending on the Dataset implementation.
273
+ */
274
+ public toString(): string {
275
+ return this.dataset.toString();
276
+ }
277
+
278
+ /**
279
+ * Returns a new Dataset that is a concatenation of this dataset and the quads given as an argument.
280
+ * @param other
281
+ */
282
+ public union(
283
+ quads: Dataset<InAndOutQuad, InAndOutQuad>,
284
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
285
+ return this.dataset.union(quads);
286
+ }
287
+
288
+ /**
289
+ * 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.
290
+ * @param subject
291
+ * @param predicate
292
+ * @param object
293
+ * @param graph
294
+ * @returns a Dataset with matching triples
295
+ */
296
+ public match(
297
+ subject?: Term | null,
298
+ predicate?: Term | null,
299
+ object?: Term | null,
300
+ graph?: Term | null,
301
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
302
+ return this.dataset.match(subject, predicate, object, graph);
303
+ }
304
+
305
+ /**
306
+ * A non-negative integer that specifies the number of quads in the set.
307
+ */
308
+ public get size(): number {
309
+ return this.dataset.size;
310
+ }
311
+
312
+ /**
313
+ * Adds the specified quad to the dataset.
314
+ * Existing quads, as defined in Quad.equals, will be ignored.
315
+ * @param quad
316
+ * @returns the dataset instance it was called on.
317
+ */
318
+ public add(quad: InAndOutQuad): this {
319
+ this.dataset.add(quad);
320
+ this.triggerSubscriptionForQuads({
321
+ added: this.datasetFactory.dataset([quad]),
322
+ });
323
+ return this;
324
+ }
325
+
326
+ /**
327
+ * Removes the specified quad from the dataset.
328
+ * This method returns the dataset instance it was called on.
329
+ * @param quad
330
+ */
331
+ public delete(quad: InAndOutQuad): this {
332
+ this.dataset.delete(quad);
333
+ this.triggerSubscriptionForQuads({
334
+ removed: this.datasetFactory.dataset([quad]),
335
+ });
336
+ return this;
337
+ }
338
+
339
+ /**
340
+ * Determines whether a dataset includes a certain quad, returning true or false as appropriate.
341
+ * @param quad
342
+ */
343
+ public has(quad: InAndOutQuad): boolean {
344
+ return this.dataset.has(quad);
345
+ }
346
+
347
+ /**
348
+ * Returns an iterator
349
+ */
350
+ public [Symbol.iterator](): Iterator<InAndOutQuad, unknown, undefined> {
351
+ return this.dataset[Symbol.iterator]();
352
+ }
353
+
354
+ /**
355
+ * ==================================================================
356
+ * EVENTEMITTER METHODS
357
+ * ==================================================================
358
+ */
359
+
360
+ /**
361
+ * Triggers all subscriptions based on an updated quads
362
+ * @param changed The changed triples of the transaction
363
+ */
364
+ private triggerSubscriptionForQuads(
365
+ changed: DatasetChanges<InAndOutQuad>,
366
+ ): void {
367
+ // A mapping of serialized QuadMatches to the changed quads
368
+ const matchingDatasetChanges: Record<
369
+ string,
370
+ DatasetChanges<InAndOutQuad>
371
+ > = {};
372
+
373
+ // Population MatchingDatasetChanges
374
+ const populateMatchingDatasetChanges = (
375
+ changeType: "added" | "removed",
376
+ ) => {
377
+ const changedQuads = changed[changeType];
378
+ changedQuads?.forEach((changedQuad) => {
379
+ // Cast the input because RDFJS types assume RDF 1.2 where a Subject can
380
+ // be a Quad
381
+ const quad = changedQuad as {
382
+ subject: SubjectNode;
383
+ predicate: PredicateNode;
384
+ object: ObjectNode;
385
+ graph: GraphNode;
386
+ };
387
+ // All possible matches that could match with this triple
388
+ const quadMatches: QuadMatch[] = [
389
+ [null, null, null, null],
390
+ [quad.subject, null, null, null],
391
+ [quad.subject, quad.predicate, null, null],
392
+ [quad.subject, null, quad.object, null],
393
+ [null, quad.predicate, null, null],
394
+ [null, quad.predicate, quad.object, null],
395
+ [null, null, quad.object, null],
396
+ [quad.subject, quad.predicate, quad.object, null],
397
+ [null, null, null, quad.graph],
398
+ [quad.subject, null, null, quad.graph],
399
+ [quad.subject, quad.predicate, null, quad.graph],
400
+ [quad.subject, null, quad.object, quad.graph],
401
+ [null, quad.predicate, null, quad.graph],
402
+ [null, quad.predicate, quad.object, quad.graph],
403
+ [null, null, quad.object, quad.graph],
404
+ [quad.subject, quad.predicate, quad.object, quad.graph],
405
+ ];
406
+ quadMatches.forEach((quadMatch) => {
407
+ const eventName = quadMatchToString(quadMatch);
408
+ // Only add to the map if there's actually a listener for this
409
+ if (this.eventEmitter.listenerCount(eventName) > 0) {
410
+ // Set matchingDatasetChanges to include data to emit
411
+ if (!matchingDatasetChanges[eventName]) {
412
+ matchingDatasetChanges[eventName] = {};
413
+ }
414
+ if (!matchingDatasetChanges[eventName][changeType]) {
415
+ matchingDatasetChanges[eventName][changeType] =
416
+ this.datasetFactory.dataset();
417
+ }
418
+ matchingDatasetChanges[eventName][changeType]?.add(changedQuad);
419
+ }
420
+ });
421
+ });
422
+ };
423
+ populateMatchingDatasetChanges("added");
424
+ populateMatchingDatasetChanges("removed");
425
+
426
+ // Alert all listeners
427
+ Object.entries(matchingDatasetChanges).forEach(
428
+ ([quadMatchString, changes]) => {
429
+ this.eventEmitter.emit(quadMatchString, changes);
430
+ },
431
+ );
432
+ }
433
+
434
+ /**
435
+ * Alias for emitter.on(eventName, listener).
436
+ * @param eventName
437
+ * @param listener
438
+ * @returns
439
+ */
440
+ public addListener(
441
+ eventName: QuadMatch,
442
+ listener: nodeEventListener<InAndOutQuad>,
443
+ ): this {
444
+ return this.on(eventName, listener);
445
+ }
446
+
447
+ /**
448
+ * Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.
449
+ * @param eventName
450
+ * @param dataset
451
+ * @param datasetChanges
452
+ * @returns true if the event had listeners, false otherwise.
453
+ */
454
+ public emit(
455
+ eventName: QuadMatch,
456
+ changes: DatasetChanges<InAndOutQuad>,
457
+ ): boolean {
458
+ return this.eventEmitter.emit(quadMatchToString(eventName), changes);
459
+ }
460
+
461
+ /**
462
+ * Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.
463
+ */
464
+ public eventNames(): QuadMatch[] {
465
+ return this.eventEmitter
466
+ .eventNames()
467
+ .map((eventName) => stringToQuadMatch(eventName as string));
468
+ }
469
+
470
+ /**
471
+ * Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to events.defaultMaxListeners.
472
+ */
473
+ public getMaxListeners(): number {
474
+ return this.eventEmitter.getMaxListeners();
475
+ }
476
+
477
+ /**
478
+ * Returns the number of listeners listening to the event named eventName.
479
+ */
480
+ public listenerCount(eventName: QuadMatch): number {
481
+ return this.eventEmitter.listenerCount(quadMatchToString(eventName));
482
+ }
483
+
484
+ /**
485
+ * Returns a copy of the array of listeners for the event named eventName.
486
+ */
487
+ public listeners(eventName: QuadMatch): nodeEventListener<InAndOutQuad>[] {
488
+ return this.eventEmitter.listeners(
489
+ quadMatchToString(eventName),
490
+ ) as nodeEventListener<InAndOutQuad>[];
491
+ }
492
+
493
+ /**
494
+ * Alias for emitter.removeListener()
495
+ */
496
+ public off(
497
+ eventName: QuadMatch,
498
+ listener: nodeEventListener<InAndOutQuad>,
499
+ ): void {
500
+ this.removeListener(eventName, listener);
501
+ }
502
+
503
+ /**
504
+ * Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
505
+ */
506
+ public on(
507
+ eventName: QuadMatch,
508
+ listener: nodeEventListener<InAndOutQuad>,
509
+ ): this {
510
+ const eventString = quadMatchToString(eventName);
511
+ if (!this.listenerHashMap.has(listener)) {
512
+ this.listenerHashMap.set(listener, new Set());
513
+ }
514
+ this.listenerHashMap.get(listener)?.add(eventString);
515
+ this.eventEmitter.on(eventString, listener);
516
+ return this;
517
+ }
518
+
519
+ /**
520
+ * Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.
521
+ */
522
+ public once(
523
+ eventName: QuadMatch,
524
+ listener: nodeEventListener<InAndOutQuad>,
525
+ ): this {
526
+ this.eventEmitter.once(quadMatchToString(eventName), listener);
527
+ return this;
528
+ }
529
+
530
+ /**
531
+ * Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
532
+ */
533
+ public prependListener(
534
+ eventName: QuadMatch,
535
+ listener: nodeEventListener<InAndOutQuad>,
536
+ ): this {
537
+ this.eventEmitter.prependListener(quadMatchToString(eventName), listener);
538
+ return this;
539
+ }
540
+
541
+ /**
542
+ * Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.
543
+ */
544
+ public prependOnceListener(
545
+ eventName: QuadMatch,
546
+ listener: nodeEventListener<InAndOutQuad>,
547
+ ): this {
548
+ this.eventEmitter.prependOnceListener(
549
+ quadMatchToString(eventName),
550
+ listener,
551
+ );
552
+ return this;
553
+ }
554
+
555
+ /**
556
+ * Removes all listeners, or those of the specified eventName.
557
+ */
558
+ public removeAllListeners(eventName: QuadMatch): this {
559
+ this.eventEmitter.removeAllListeners(quadMatchToString(eventName));
560
+ return this;
561
+ }
562
+
563
+ /**
564
+ * Removes the specified listener from the listener array for the event named eventName.
565
+ */
566
+ public removeListener(
567
+ eventName: QuadMatch,
568
+ listener: nodeEventListener<InAndOutQuad>,
569
+ ): this {
570
+ this.eventEmitter.removeListener(quadMatchToString(eventName), listener);
571
+ return this;
572
+ }
573
+
574
+ /**
575
+ * Removes the specified listener from the listener array for the event named eventName.
576
+ */
577
+ removeListenerFromAllEvents(listener: nodeEventListener<InAndOutQuad>): this {
578
+ const eventStringSet = this.listenerHashMap.get(listener);
579
+ if (eventStringSet) {
580
+ eventStringSet.forEach((eventString) => {
581
+ this.eventEmitter.off(eventString, listener);
582
+ });
583
+ }
584
+ return this;
585
+ }
586
+
587
+ /**
588
+ * By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.
589
+ */
590
+ public setMaxListeners(n: number): this {
591
+ this.eventEmitter.setMaxListeners(n);
592
+ return this;
593
+ }
594
+
595
+ /**
596
+ * Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).
597
+ */
598
+ public rawListeners(eventName: QuadMatch): nodeEventListener<InAndOutQuad>[] {
599
+ return this.eventEmitter.rawListeners(
600
+ quadMatchToString(eventName),
601
+ ) as nodeEventListener[];
602
+ }
603
+
604
+ /**
605
+ * ==================================================================
606
+ * TRANSACTION METHODS
607
+ * ==================================================================
608
+ */
609
+
610
+ /**
611
+ * Returns a transactional dataset that will update this dataset when its transaction is committed.
612
+ */
613
+ public startTransaction(): TransactionalDataset<InAndOutQuad> {
614
+ return new ProxyTransactionalDataset(this, this.datasetFactory);
615
+ }
616
+ }
@@ -0,0 +1,27 @@
1
+ import type { DatasetFactory, BaseQuad, Dataset } from "@rdfjs/types";
2
+ import WrapperSubscribableDataset from "./WrapperSubscribableDataset";
3
+
4
+ /**
5
+ * A DatasetFactory that returns a WrapperSubscribableDataset given a generic DatasetFactory.
6
+ */
7
+ export default class WrapperSubscribableDatasetFactory<
8
+ InAndOutQuad extends BaseQuad = BaseQuad,
9
+ > implements DatasetFactory<InAndOutQuad, InAndOutQuad>
10
+ {
11
+ private datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>;
12
+ constructor(datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>) {
13
+ this.datasetFactory = datasetFactory;
14
+ }
15
+
16
+ dataset(
17
+ quads?: Dataset<InAndOutQuad, InAndOutQuad> | InAndOutQuad[],
18
+ ): WrapperSubscribableDataset<InAndOutQuad> {
19
+ // Typings are wrong
20
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
21
+ // @ts-ignore
22
+ return new WrapperSubscribableDataset(
23
+ this.datasetFactory,
24
+ quads ? this.datasetFactory.dataset(quads) : undefined,
25
+ );
26
+ }
27
+ }
@@ -0,0 +1,30 @@
1
+ import type { Dataset, DatasetFactory, Quad } from "@rdfjs/types";
2
+ import type { WrapperSubscribableDataset } from ".";
3
+ import { createDataset } from "@ldo/dataset";
4
+ import WrapperSubscribableDatasetFactory from "./WrapperSubscribableDatasetFactory";
5
+
6
+ /**
7
+ * Creates a dataset factory that generates a SubscribableDataset
8
+ * @returns DatasetFactory for SubscribableDataset
9
+ */
10
+ export function createWrapperSubscribableDatasetFactory(): WrapperSubscribableDatasetFactory<Quad> {
11
+ const datasetFactory: DatasetFactory<Quad> = {
12
+ dataset: (quads?: Dataset<Quad> | Quad[]): Dataset<Quad> => {
13
+ return createDataset(quads);
14
+ },
15
+ };
16
+ return new WrapperSubscribableDatasetFactory(datasetFactory);
17
+ }
18
+
19
+ /**
20
+ * Creates a SubscribableDataset
21
+ * @param quads: A dataset or array of Quads to initialize the dataset.
22
+ * @returns Dataset
23
+ */
24
+ export default function createWrapperSubscribableDataset(
25
+ quads?: Dataset<Quad> | Quad[],
26
+ ): WrapperSubscribableDataset<Quad> {
27
+ const wrapperSubscribableDatasetFactory =
28
+ createWrapperSubscribableDatasetFactory();
29
+ return wrapperSubscribableDatasetFactory.dataset(quads);
30
+ }
@@ -0,0 +1,28 @@
1
+ import type { Quad } from "@rdfjs/types";
2
+ import type { ParserOptions } from "@ldo/rdf-utils";
3
+ import { createDatasetFromSerializedInput } from "@ldo/dataset";
4
+ import { createWrapperSubscribableDatasetFactory } from "./createWrapperSubscribableDataset";
5
+ import type WrapperSubscribableDataset from "./WrapperSubscribableDataset";
6
+
7
+ /**
8
+ * Creates a SubscribableDataset with a string input that could be JSON-LD, Turtle, N-Triples, TriG, RDF*, or N3.
9
+ * @param data A string representation of RDF Data in JSON-LD, Turtle, N-Triples, TriG, RDF*, or N3.
10
+ * @param options Parser options: {
11
+ * format?: string;
12
+ * factory?: RDF.DataFactory;
13
+ * baseIRI?: string;
14
+ * blankNodePrefix?: string;
15
+ * }
16
+ * @returns A dataset
17
+ */
18
+ export default async function createWrapperSubscribableDatasetDataserFromSerializedInput(
19
+ data: string,
20
+ options?: ParserOptions,
21
+ ): Promise<WrapperSubscribableDataset<Quad>> {
22
+ const datasetFactory = createWrapperSubscribableDatasetFactory();
23
+ return createDatasetFromSerializedInput<WrapperSubscribableDataset<Quad>>(
24
+ datasetFactory,
25
+ data,
26
+ options,
27
+ );
28
+ }