feat: first release

This commit is contained in:
2024-06-30 14:06:20 +02:00
commit e286e3092c
7 changed files with 160 additions and 0 deletions

34
.github/workflows/publish.yml vendored Normal file
View File

@@ -0,0 +1,34 @@
name: Publish
on:
workflow_dispatch:
push:
branches:
- main
paths:
- "deno.json"
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Deno
uses: maximousblk/setup-deno@v2 # Installs latest version
- run: deno task test
publish:
runs-on: ubuntu-latest
needs: test
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
- name: Publish package
run: npx jsr publish

20
.github/workflows/test.yml vendored Normal file
View File

@@ -0,0 +1,20 @@
name: Test
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Deno
uses: maximousblk/setup-deno@v2 # Installs latest version
- run: deno task test

13
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,13 @@
{
"deno.enable": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.ts$",
"cmd": "deno lint ${file} --fix"
}
]
}
}

8
README.md Normal file
View File

@@ -0,0 +1,8 @@
<p align="center">
<img src="https://user-images.githubusercontent.com/1998130/229430454-ca0f2811-d874-4314-b13d-c558de8eec7e.svg" />
</p>
# Event Emitter
Introduces a `subscribe` extension on the latest popular [eventemitter3](https://github.com/primus/eventemitter3)
solution.

26
deno.json Normal file
View File

@@ -0,0 +1,26 @@
{
"name": "@valkyr/event-emitter",
"version": "1.0.0",
"exports": "./event-emitter.ts",
"imports": {
"eventemitter3": "npm:eventemitter3@5.0.1"
},
"exclude": [
".vscode"
],
"lint": {
"rules": {
"exclude": [
"no-explicit-any",
"require-await"
]
}
},
"fmt": {
"lineWidth": 120
},
"tasks": {
"test": "deno test --allow-all --unstable-ffi",
"lint": "deno lint --fix"
}
}

20
deno.lock generated Normal file
View File

@@ -0,0 +1,20 @@
{
"version": "3",
"packages": {
"specifiers": {
"npm:eventemitter3@5.0.1": "npm:eventemitter3@5.0.1"
},
"npm": {
"eventemitter3@5.0.1": {
"integrity": "sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==",
"dependencies": {}
}
}
},
"remote": {},
"workspace": {
"dependencies": [
"npm:eventemitter3@5.0.1"
]
}
}

39
event-emitter.ts Normal file
View File

@@ -0,0 +1,39 @@
import { EventEmitter as BaseEventEmitter } from "eventemitter3";
export class EventEmitter<EventTypes extends ValidEventTypes = string | symbol> extends BaseEventEmitter<EventTypes> {
subscribe<T extends EventNames<EventTypes>>(
event: T,
fn: EventListener<EventTypes, T>,
destroy?: () => void,
): {
unsubscribe: () => void;
} {
this.addListener(event, fn);
return {
unsubscribe: () => {
this.removeListener(event, fn);
if (destroy) {
destroy();
}
},
};
}
}
/*
|--------------------------------------------------------------------------------
| Types
|--------------------------------------------------------------------------------
*/
type EventListener<T extends ValidEventTypes, K extends EventNames<T>> = T extends string | symbol
? (...args: any[]) => void
: (...args: ArgumentMap<Exclude<T, string | symbol>>[Extract<K, keyof T>]) => void;
type EventNames<T extends ValidEventTypes> = T extends string | symbol ? T : keyof T;
type ValidEventTypes = string | symbol | Record<string, unknown>;
type ArgumentMap<T extends Record<string, unknown>> = {
[K in keyof T]: T[K] extends (...args: any[]) => void ? Parameters<T[K]> : T[K] extends any[] ? T[K] : any[];
};