Skip to content

Commit

Permalink
refactor: Simplify EventEmitter declarations (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer authored May 29, 2024
1 parent 141f82d commit 85bd8ea
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 66 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@types/lodash.kebabcase": "^4.1.9",
"@types/mocha": "^10.0.6",
"@types/n3": "^1.16.4",
"@types/node": "20.11.5",
"@types/node": "^20.12.12",
"@types/sparqljs": "^3.1.8",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
Expand Down
30 changes: 6 additions & 24 deletions src/lib/Generator.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import type {ConstructQuery, ValuePatternRow} from 'sparqljs';
import type Stage from './Stage.class.js';
import getSPARQLQuery from '../utils/getSPARQLQuery.js';
Expand All @@ -12,28 +11,13 @@ import EventEmitter from 'node:events';

const DEFAULT_BATCH_SIZE = 10;

declare interface Generator {
on(event: 'data', listener: (statement: Quad) => void): this;
on(
event: 'end',
listener: (
iterations: number,
statements: number,
processed: number
) => void
): this;
on(event: 'error', listener: (e: Error) => void): this;

emit(event: 'data', statement: Quad): boolean;
emit(
event: 'end',
iterations: number,
statements: number,
processed: number
): boolean;
emit(event: 'error', e: Error): boolean;
interface Events {
end: [iterations: number, statements: number, processed: number];
error: [e: Error];
data: [statement: Quad];
}
class Generator extends EventEmitter {

export default class Generator extends EventEmitter<Events> {
private readonly query: ConstructQuery;
private readonly engine: QueryEngine;
private iterationsProcessed = 0;
Expand Down Expand Up @@ -137,5 +121,3 @@ class Generator extends EventEmitter {
this.runBatch(this.$thisList);
}
}

export default Generator;
16 changes: 5 additions & 11 deletions src/lib/Iterator.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import EventEmitter from 'node:events';
import type {SelectQuery} from 'sparqljs';
import type Stage from './Stage.class.js';
Expand All @@ -13,17 +12,14 @@ import getEngineSource from '../utils/getEngineSource.js';
import parse from 'parse-duration';

const DEFAULT_LIMIT = 10;
declare interface Iterator {
on(event: 'data', listener: ($this: NamedNode) => void): this;
on(event: 'end', listener: (numResults: number) => void): this;
on(event: 'error', listener: (e: Error) => void): this;

emit(event: 'data', $this: NamedNode): boolean;
emit(event: 'end', numResults: number): boolean;
emit(event: 'error', e: Error): boolean;
interface Events {
data: [$this: NamedNode];
end: [numResults: number];
error: [Error];
}

class Iterator extends EventEmitter {
export default class Iterator extends EventEmitter<Events> {
private readonly query: SelectQuery;
public readonly endpoint: Endpoint;
private readonly engine: QueryEngine;
Expand Down Expand Up @@ -103,5 +99,3 @@ class Iterator extends EventEmitter {
}, this.delay);
}
}

export default Iterator;
31 changes: 7 additions & 24 deletions src/lib/Stage.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import EventEmitter from 'node:events';
import File from './File.class.js';
import {type LDWorkbenchConfiguration} from './LDWorkbenchConfiguration.js';
Expand All @@ -10,29 +9,15 @@ import path from 'node:path';
import {Writer} from 'n3';
import type {NamedNode} from '@rdfjs/types';
import type {WriteStream} from 'node:fs';
declare interface Stage {
on(event: 'generatorResult', listener: (count: number) => void): this;
on(
event: 'end',
listener: (iteratorCount: number, statements: number) => void
): this;
on(
event: 'iteratorResult',
listener: ($this: NamedNode, quadsGenerated: number) => void
): this;
on(event: 'error', listener: (e: Error) => void): this;

emit(event: 'generatorResult', count: number): boolean;
emit(event: 'end', iteratorCount: number, statements: number): boolean;
emit(
event: 'iteratorResult',
$this: NamedNode,
quadsGenerated: number
): boolean;
emit(event: 'error', e: Error): boolean;

interface Events {
generatorResult: [count: number];
end: [iteratorCount: number, statements: number];
iteratorResult: [$this: NamedNode, quadsGenerated: number];
error: [Error];
}

class Stage extends EventEmitter {
export default class Stage extends EventEmitter<Events> {
public destination: () => WriteStream;
public iterator: Iterator;
public generators: Generator[] = [];
Expand Down Expand Up @@ -140,5 +125,3 @@ class Stage extends EventEmitter {
this.iterator.run();
}
}

export default Stage;
2 changes: 1 addition & 1 deletion src/lib/tests/Iterator.class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import chaiAsPromised from 'chai-as-promised';
import type {LDWorkbenchConfiguration} from '../LDWorkbenchConfiguration.js';
import {fileURLToPath} from 'url';
import removeDirectory from '../../utils/removeDir.js';
import {NamedNode} from 'n3';
import {NamedNode} from '@rdfjs/types';
chai.use(chaiAsPromised);
const expect = chai.expect;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/tests/Stage.class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import Generator from '../Generator.class.js';
import * as chai from 'chai';
import * as path from 'path';
import chaiAsPromised from 'chai-as-promised';
import type {NamedNode} from 'n3';
import type {LDWorkbenchConfiguration} from '../LDWorkbenchConfiguration.js';
import {fileURLToPath} from 'url';
import removeDirectory from '../../utils/removeDir.js';
import {NamedNode} from '@rdfjs/types';

chai.use(chaiAsPromised);
const expect = chai.expect;
Expand Down

0 comments on commit 85bd8ea

Please sign in to comment.