@kaciras/utilities
Advanced tools
Comparing version
@@ -43,3 +43,3 @@ const htmlEscapes = { | ||
* | ||
* This function is only available on browser. | ||
* This function is only available in browser. | ||
* | ||
@@ -46,0 +46,0 @@ * <h1>Alternatives</h1> |
@@ -47,3 +47,3 @@ const htmlEscapes: Record<string, string> = { | ||
* | ||
* This function is only available on browser. | ||
* This function is only available in browser. | ||
* | ||
@@ -50,0 +50,0 @@ * <h1>Alternatives</h1> |
@@ -0,1 +1,5 @@ | ||
/** | ||
* The SingleEventEmitter calls all listeners synchronously in the | ||
* order in which they were registered. | ||
*/ | ||
export class SingleEventEmitter { | ||
@@ -6,2 +10,13 @@ handlers = []; | ||
} | ||
/** | ||
* Removes the specified listener from the listener array. | ||
* | ||
* Once an event is emitted, all listeners attached to it | ||
* at the time of emitting are called in order. | ||
* This implies that any removeListener() or removeAllListeners() | ||
* calls after emitting and before the last listener | ||
* finishes execution will not remove them from emit() in progress. | ||
* | ||
* @param handler The listener to remove | ||
*/ | ||
removeListener(handler) { | ||
@@ -12,3 +27,3 @@ const { handlers } = this; | ||
removeAllListeners() { | ||
this.handlers.length = 0; | ||
this.handlers = []; | ||
} | ||
@@ -15,0 +30,0 @@ once(handler) { |
@@ -1,12 +0,27 @@ | ||
type Handler = (...args: any[]) => void; | ||
type Handler<T extends any[]> = (...args: T) => any; | ||
export class SingleEventEmitter<T extends Handler = any> { | ||
/** | ||
* The SingleEventEmitter calls all listeners synchronously in the | ||
* order in which they were registered. | ||
*/ | ||
export class SingleEventEmitter<T extends any[] = any[]> { | ||
private handlers: T[] = []; | ||
private handlers: Array<Handler<T>> = []; | ||
addListener(handler: T) { | ||
addListener(handler: Handler<T>) { | ||
this.handlers.push(handler); | ||
} | ||
removeListener(handler: T) { | ||
/** | ||
* Removes the specified listener from the listener array. | ||
* | ||
* Once an event is emitted, all listeners attached to it | ||
* at the time of emitting are called in order. | ||
* This implies that any removeListener() or removeAllListeners() | ||
* calls after emitting and before the last listener | ||
* finishes execution will not remove them from emit() in progress. | ||
* | ||
* @param handler The listener to remove | ||
*/ | ||
removeListener(handler: Handler<T>) { | ||
const { handlers } = this; | ||
@@ -17,14 +32,14 @@ this.handlers = handlers.filter(h => h !== handler); | ||
removeAllListeners() { | ||
this.handlers.length = 0; | ||
this.handlers = []; | ||
} | ||
once(handler: T) { | ||
const wrapper = (...args: unknown[]) => { | ||
once(handler: Handler<T>) { | ||
const wrapper = (...args: T) => { | ||
handler(...args); | ||
this.removeListener(wrapper as T); | ||
this.removeListener(wrapper as Handler<T>); | ||
}; | ||
this.addListener(wrapper as T); | ||
this.addListener(wrapper as Handler<T>); | ||
} | ||
dispatchEvent(...args: Parameters<T>) { | ||
dispatchEvent(...args: T) { | ||
for (const handler of this.handlers) handler(...args); | ||
@@ -41,3 +56,3 @@ } | ||
interface Default extends EventsMap { | ||
[event: string]: (...args: any) => void; | ||
[event: string]: (...args: any) => any; | ||
} | ||
@@ -44,0 +59,0 @@ |
@@ -8,2 +8,3 @@ export * from "./codec.js"; | ||
export * from "./interactive.js"; | ||
export { default as TTLCache } from "./TTLCache.js"; | ||
//# sourceMappingURL=index.js.map |
@@ -8,1 +8,3 @@ export * from "./codec.js"; | ||
export * from "./interactive.js"; | ||
export { default as TTLCache } from "./TTLCache.js"; |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* This function is only available on browser. | ||
* This function is only available in browser. | ||
* | ||
@@ -29,3 +29,3 @@ * @param blob The blob object to download. | ||
* | ||
* This function is only available on browser. | ||
* This function is only available in browser. | ||
* | ||
@@ -32,0 +32,0 @@ * <h1>Limitation</h1> |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* This function is only available on browser. | ||
* This function is only available in browser. | ||
* | ||
@@ -30,3 +30,3 @@ * @param blob The blob object to download. | ||
* | ||
* This function is only available on browser. | ||
* This function is only available in browser. | ||
* | ||
@@ -33,0 +33,0 @@ * <h1>Limitation</h1> |
{ | ||
"name": "@kaciras/utilities", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
@@ -14,2 +14,5 @@ "description": "A set of common JS functions for node and browser.", | ||
], | ||
"exports": { | ||
"./*": "./lib/*.js" | ||
}, | ||
"type": "module", | ||
@@ -22,12 +25,12 @@ "types": "lib/index.ts", | ||
"devDependencies": { | ||
"@jest/globals": "^27.5.1", | ||
"@kaciras/eslint-config-core": "^2.3.0", | ||
"@kaciras/eslint-config-jest": "^2.3.0", | ||
"@kaciras/eslint-config-typescript": "^2.3.0", | ||
"@swc/core": "^1.2.154", | ||
"@swc/jest": "^0.2.20", | ||
"@types/node": "^17.0.21", | ||
"eslint": "^8.11.0", | ||
"jest": "^27.5.1", | ||
"typescript": "^4.6.2" | ||
"@jest/globals": "^28.1.0", | ||
"@kaciras/eslint-config-core": "^2.4.0", | ||
"@kaciras/eslint-config-jest": "^2.4.0", | ||
"@kaciras/eslint-config-typescript": "^2.4.0", | ||
"@swc/core": "^1.2.189", | ||
"@swc/jest": "^0.2.21", | ||
"@types/node": "^17.0.35", | ||
"eslint": "^8.16.0", | ||
"jest": "^28.1.0", | ||
"typescript": "^4.6.4" | ||
}, | ||
@@ -38,4 +41,3 @@ "scripts": { | ||
"compile": "tsc" | ||
}, | ||
"readme": "# Utilities\n\n[](https://github.com/Kaciras/utilities/actions/workflows/test.yml)\n[](https://codecov.io/gh/Kaciras/utilities)\n\nA set of common JS functions for node and browser.\n\n## Install\n\nThis package is pure ESM, It cannot be `require()`'d from CommonJS.\n\n```\npnpm i @kaciras/utilities\n```\n\n## Environment detection\n\n@kaciras/utilities use `typeof window === \"undefined\"` to detect whether it is running in the browser or Node, \n" | ||
} | ||
} |
# Utilities | ||
 | ||
[](https://github.com/Kaciras/utilities/actions/workflows/test.yml) | ||
@@ -4,0 +5,0 @@ [](https://codecov.io/gh/Kaciras/utilities) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39144
27.28%30
11.11%846
35.79%20
5.26%