From 85bd8ead8506dc8f5de89cb4bb3c0314518fcba5 Mon Sep 17 00:00:00 2001 From: David de Boer Date: Wed, 29 May 2024 10:29:03 +0200 Subject: [PATCH] refactor: Simplify EventEmitter declarations (#65) --- package-lock.json | 8 +++---- package.json | 2 +- src/lib/Generator.class.ts | 30 ++++++--------------------- src/lib/Iterator.class.ts | 16 +++++--------- src/lib/Stage.class.ts | 31 +++++++--------------------- src/lib/tests/Iterator.class.test.ts | 2 +- src/lib/tests/Stage.class.test.ts | 2 +- 7 files changed, 25 insertions(+), 66 deletions(-) diff --git a/package-lock.json b/package-lock.json index caa04ec..a50988b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -41,7 +41,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", @@ -5616,9 +5616,9 @@ } }, "node_modules/@types/node": { - "version": "20.11.5", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.5.tgz", - "integrity": "sha512-g557vgQjUUfN76MZAN/dt1z3dzcUsimuysco0KeluHgrPdJXkP/XdAURgyO2W9fZWHRtRBiVKzKn8vyOAwlG+w==", + "version": "20.12.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.12.tgz", + "integrity": "sha512-eWLDGF/FOSPtAvEqeRAQ4C8LSA7M1I7i0ky1I8U7kD1J5ITyW3AsRhQrKVoWf5pFKZ2kILsEGJhsI9r93PYnOw==", "dependencies": { "undici-types": "~5.26.4" } diff --git a/package.json b/package.json index 7382bb7..11c9e98 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/Generator.class.ts b/src/lib/Generator.class.ts index 6af4aa0..e43e770 100644 --- a/src/lib/Generator.class.ts +++ b/src/lib/Generator.class.ts @@ -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'; @@ -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 { private readonly query: ConstructQuery; private readonly engine: QueryEngine; private iterationsProcessed = 0; @@ -137,5 +121,3 @@ class Generator extends EventEmitter { this.runBatch(this.$thisList); } } - -export default Generator; diff --git a/src/lib/Iterator.class.ts b/src/lib/Iterator.class.ts index e6720d8..b85b014 100644 --- a/src/lib/Iterator.class.ts +++ b/src/lib/Iterator.class.ts @@ -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'; @@ -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 { private readonly query: SelectQuery; public readonly endpoint: Endpoint; private readonly engine: QueryEngine; @@ -103,5 +99,3 @@ class Iterator extends EventEmitter { }, this.delay); } } - -export default Iterator; diff --git a/src/lib/Stage.class.ts b/src/lib/Stage.class.ts index d91712c..5185cf1 100644 --- a/src/lib/Stage.class.ts +++ b/src/lib/Stage.class.ts @@ -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'; @@ -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 { public destination: () => WriteStream; public iterator: Iterator; public generators: Generator[] = []; @@ -140,5 +125,3 @@ class Stage extends EventEmitter { this.iterator.run(); } } - -export default Stage; diff --git a/src/lib/tests/Iterator.class.test.ts b/src/lib/tests/Iterator.class.test.ts index 8613736..b0e3c39 100644 --- a/src/lib/tests/Iterator.class.test.ts +++ b/src/lib/tests/Iterator.class.test.ts @@ -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; diff --git a/src/lib/tests/Stage.class.test.ts b/src/lib/tests/Stage.class.test.ts index faaffd1..02e8b38 100644 --- a/src/lib/tests/Stage.class.test.ts +++ b/src/lib/tests/Stage.class.test.ts @@ -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;