Skip to content

Commit b9c7806

Browse files
authored
feature: Migrated to vite (#9)
1 parent 32639b9 commit b9c7806

File tree

8 files changed

+31
-39
lines changed

8 files changed

+31
-39
lines changed

.eslintrc.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@
3232
],
3333
"no-nested-ternary": "warn",
3434
"@typescript-eslint/no-shadow": "off",
35-
"@typescript-eslint/naming-convention": [
36-
"error",
37-
{
38-
"selector": "enum",
39-
"format": ["UPPER_CASE"]
40-
}
41-
],
4235
"no-useless-catch": "error",
4336
"no-underscore-dangle": "off",
4437
"@typescript-eslint/no-invalid-void-type": "off"

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ $ npm install awesome-pubsub-js
1313
```
1414

1515
### yarn
16+
1617
```shell
1718
$ yarn add awesome-pubsub-js
1819
```
@@ -26,7 +27,7 @@ Do you want to know more? Go to the [Documentation](#API) section
2627
```js
2728
import PubSub from "awesome-pubsub-js";
2829

29-
const pubSub = PubSub();
30+
const pubSub = new PubSub();
3031

3132
pubSub.subscribe("event.example", (data) => {
3233
// data = { name: "John", email: "john@gmail.com" }
@@ -37,20 +38,19 @@ pubSub.publish("event.example", { name: "John", email: "john@gmail.com" });
3738
```
3839

3940
### API
41+
4042
**List of all available methods:**
4143

4244
Each of the following methods takes one or two arguments
4345

4446
- Subscribe
4547

46-
47-
| Method name | Payload | Return value |
48-
| ------ | ------ | ------ |
49-
| subscribe | ```pubSub.subscribe("eventName", (data) => {});```| HashKey (string) - is needed for use in the 'unsubscribe' method |
50-
| unsubscribe | ```pubSub.unsubscribe('hashKey')``` <br /> hashKey (string) is always returned from the subscribe method | true - when the event has been successfully unsubscribed <br /> false - when the event does not exists |
51-
| publish | ```pubSub.publish('eventName', any)``` <br /> any = literally anything you want to pass :) <br /> If you don't pass anything, the default value will be undefined | true - when the event has published successfully <br /> false - when the event has not been published (e.g. due to the lack of a registered subscriber) |
52-
| getAllSubscribers | - | returns the current subscription list |
53-
48+
| Method name | Payload | Return value |
49+
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
50+
| subscribe | `pubSub.subscribe("eventName", (data) => {});` | HashKey (string) - is needed for use in the 'unsubscribe' method |
51+
| unsubscribe | `pubSub.unsubscribe('hashKey')` <br /> hashKey (string) is always returned from the subscribe method | true - when the event has been successfully unsubscribed <br /> false - when the event does not exists |
52+
| publish | `pubSub.publish('eventName', any)` <br /> any = literally anything you want to pass :) <br /> If you don't pass anything, the default value will be undefined | true - when the event has published successfully <br /> false - when the event has not been published (e.g. due to the lack of a registered subscriber) |
53+
| getAllSubscribers | - | returns the current subscription list |
5454

5555
### Roadmap:
5656

@@ -59,4 +59,3 @@ Each of the following methods takes one or two arguments
5959
- [x] getAllSubscribers method
6060
- [ ] Wildcard support
6161
- [ ] Logger
62-

__tests__/pubSub.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
/* eslint-disable @typescript-eslint/ban-ts-comment */
2-
import PubSub, { PubSubInterface } from "../lib";
1+
import PubSub from "../lib";
32

43
// eslint-disable-next-line @typescript-eslint/no-empty-function
54
const noop = (): void => {};
65

76
describe("PubSub -> Default Instance", () => {
8-
let pubSub: PubSubInterface;
7+
let pubSub: PubSub;
98

109
beforeEach(() => {
11-
pubSub = PubSub();
10+
pubSub = new PubSub();
1211
});
1312

1413
it("Should create an instance of pubsub with an empty subscription list", () => {
@@ -17,10 +16,10 @@ describe("PubSub -> Default Instance", () => {
1716
});
1817

1918
describe("PubSub -> Subscribe Method", () => {
20-
let pubSub: PubSubInterface;
19+
let pubSub: PubSub;
2120

2221
beforeEach(() => {
23-
pubSub = PubSub();
22+
pubSub = new PubSub();
2423
});
2524

2625
it("Should add event to the subscriptions list, with generated hash key and forwarded callback ", () => {
@@ -82,10 +81,10 @@ describe("PubSub -> Subscribe Method", () => {
8281
});
8382

8483
describe("PubSub -> Unsubscribe Method", () => {
85-
let pubSub: PubSubInterface;
84+
let pubSub: PubSub;
8685

8786
beforeEach(() => {
88-
pubSub = PubSub();
87+
pubSub = new PubSub();
8988
});
9089

9190
it("Should remove event from the subscribers array", () => {
@@ -186,14 +185,14 @@ describe("PubSub -> Unsubscribe Method", () => {
186185
});
187186

188187
describe("PubSub -> Publish method", () => {
189-
let pubSub: PubSubInterface;
188+
let pubSub: PubSub;
190189
const data = {
191190
name: "John",
192191
age: 50,
193192
};
194193

195194
beforeEach(() => {
196-
pubSub = PubSub();
195+
pubSub = new PubSub();
197196
});
198197

199198
it("Should publish event with payload", () => {

__tests__/pubSub.typings.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import PubSub from "../lib";
23

34
enum Event {
@@ -8,7 +9,7 @@ interface Data {
89
name: string;
910
}
1011

11-
const pubSub = PubSub();
12+
const pubSub = new PubSub();
1213

1314
// publish method
1415
pubSub.publish<Event, Data>(Event.Random, { name: "John" });
@@ -19,13 +20,9 @@ pubSub.publish("event.without.types", []);
1920

2021
// subscribe method
2122
pubSub.subscribe<Event, Data>(Event.Random, (data) => {
22-
data.name;
23+
console.log(data.name);
2324
});
2425

2526
pubSub.subscribe<Event>(Event.Random, () => {
2627
// empty payload
2728
});
28-
29-
pubSub.subscribe<Event.Random, Data>(Event.Random, (data) => {
30-
data.name;
31-
});

lib/pubSub.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type Callback<D = unknown> = (data?: D) => void;
55
interface Subscribers {
66
eventName: string;
77
hashKey: HashKey;
8+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
89
callback: Callback<any>;
910
}
1011

@@ -15,9 +16,9 @@ export class PubSub {
1516
this.subscribers = [];
1617
}
1718

18-
subscribe = <E extends string, D = (data?: unknown) => void>(
19+
subscribe = <E extends string, D = undefined>(
1920
event: E,
20-
callback: Callback<D>
21+
callback: (data: D extends undefined ? void : D) => void
2122
): HashKey => {
2223
const hashKey = randomHashKeyGenerator();
2324

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "awesome-pubsub-js",
3-
"version": "1.2.1",
3+
"version": "2.0.0",
4+
"type": "module",
45
"description": "JavaScript implementation of the Publish/Subscribe pattern with TypeScript support.",
56
"main": "dist/index.cjs.js",
67
"types": "dist/index.d.ts",

tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,5 @@
2525
"noUnusedParameters": true,
2626
"noFallthroughCasesInSwitch": true,
2727
"noImplicitAny": true
28-
},
29-
"include": ["./lib/**/*"],
30-
"exclude": ["./__tests__", "./webpack.config.ts"]
28+
}
3129
}

vite.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { defineConfig } from "vite";
2+
import { fileURLToPath } from "url";
23
import path from "path";
34
import dts from "vite-plugin-dts";
45

6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = path.dirname(__filename);
8+
59
export default defineConfig({
610
build: {
711
lib: {

0 commit comments

Comments
 (0)