Skip to content

Commit

Permalink
feat: event-notifier created
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Krasnov committed Feb 24, 2024
1 parent f90522d commit 3929ff3
Show file tree
Hide file tree
Showing 24 changed files with 4,965 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
18 changes: 18 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.lint.json"
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended-type-checked",
"prettier"
],
"plugins": ["simple-import-sort", "@typescript-eslint"],
"env": { "browser": true },
"rules": {
"simple-import-sort/imports": "warn",
"simple-import-sort/exports": "warn"
}
}
26 changes: 26 additions & 0 deletions .github/actions/install/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Install pnpm
description: Install dependencies and pnpm cache

runs:
using: composite
steps:
- uses: pnpm/action-setup@v2
with:
standalone: true
run_install: false

- name: Cache node_modules
uses: actions/cache@v3
id: node-modules
with:
path: node_modules
key: node-modules-${{ hashFiles('pnpm-lock.yaml') }}

- name: Set publishing config
run: pnpm config set '//npm.pkg.github.com/:_authToken' "${NODE_AUTH_TOKEN}"
shell: bash

- name: Install dependencies
if: steps.node-modules.cache-hit != 'true'
run: pnpm install --frozen-lockfile
shell: bash
16 changes: 16 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish
on:
workflow_dispatch:
release:
types: [published]

env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/install
- run: pnpm run publish
18 changes: 18 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Validate

on:
pull_request:
workflow_dispatch:

env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/install
- run: pnpm lint
- run: pnpm test
- run: pnpm build
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
dist/
.DS_Store
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm -- commitlint --edit $1
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pnpm lint
pnpm test
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use-node-version=20.11.1
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
dist/
pnpm-lock.yaml
74 changes: 72 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
# event-notifier
typed event emitter
# event-notifier pack

### **typed event emitter**

### to install, use the following (or similar package managers)

```sh
npm install event-notifier
```

### after that you can use

```ts
type Data = {
balance: { value: number };
test: undefined;
};

const eventNotifier = new EventNotifier<Data>();

eventNotifier.on('balance', ({ value }) => {
// your code
});

eventNotifier.notify({ type: 'test' });

eventNotifier.notify({ type: 'balance', value: 42 });
```

### data object

```ts
type Data = {
balance: { value: number };
test: undefined;
};

const eventNotifier = new EventNotifier<Data>();

eventNotifier.on('test', (data) => {
// Object.entries(data).length === 0;
});

eventNotifier.on('balance', (data) => {
// Object.entries(data).length === 1;
// 'value' in data === true;
// typeof data.value === 'number'
});
```

### erroneous statements

```ts
type Data = {
balance: { value: number };
test: undefined;
};

const eventNotifier = new EventNotifier<Data>();

// An argument of type ""asd" cannot be assigned to a parameter of type "keyof Data".ts(2345)
eventNotifier.on('asd', () => {});

// The "value" property is missing in the type "{ type: "balance"; }" and is required in the type "{ value: number; }".ts(2345)
eventNotifier.notify({ type: 'balance' });

// An object literal can only use unique properties. "data" does not exist in type "{ type: "test"; }.ts(2353)
eventNotifier.notify({ type: 'test', data: 42 });

// Type ""asd"" cannot be assigned to type "keyof Data".ts(2322)
eventNotifier.notify({ type: 'asd' });
```
5 changes: 5 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = {
extends: ['@commitlint/config-conventional'],
};

export default config;
57 changes: 57 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "event-notifier",
"version": "0.0.1",
"description": "typed event emitter",
"main": "dist",
"type": "module",
"files": [
"/dist"
],
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
},
"scripts": {
"build": "vite build",
"dev": "vite build --watch",
"lint": "eslint .",
"test": "pnpm test:unit & pnpm test:type",
"test:unit": "vitest --run",
"test:bench": "vitest bench --run",
"test:type": "vitest --typecheck --run",
"test:browser": "vitest --browser=chrome",
"prepare": "husky",
"prepublish": "pnpm run build"
},
"devDependencies": {
"@commitlint/cli": "^18.6.1",
"@commitlint/config-conventional": "^18.6.2",
"@types/node": "^20.11.20",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"@vitest/browser": "^1.3.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-simple-import-sort": "^12.0.0",
"husky": "^9.0.11",
"prettier": "^3.2.5",
"typescript": "^5.3.3",
"vite": "^5.1.4",
"vite-plugin-dts": "^3.7.3",
"vitest": "^1.3.1",
"webdriverio": "^8.32.3"
},
"keywords": [
"event notifier",
"event emitter"
],
"author": "Krasnov Sergei",
"license": "ISC",
"engines": {
"node": ">=20.0.0",
"pnpm": ">=8.0.0"
},
"packageManager": "[email protected]",
"homepage": "https://github.com/ksv90/event-notifier"
}
Loading

0 comments on commit 3929ff3

Please sign in to comment.