Skip to content

Commit

Permalink
chore: update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
itswillta committed Aug 25, 2024
1 parent e9fb5b6 commit d58b54e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ npm install native-pubsub
First, you need to create an event bus:

```typescript
import { createEventBus } from 'native-pubsub';
import { EventBus } from 'native-pubsub';

const eventBus = createEventBus();
const eventBus = new EventBus();

// or create an event bus with a typed data structure:

const typedEventBus = createEventBus<{ id: string, name: string }>();
const typedEventBus = new EventBus<{ id: string, name: string }>();
```

Event buses are unique, and it's recommended that you only publish (and subscribe to) one kind of events for each event bus.
Expand Down
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default antfu(
'antfu/consistent-list-newline': 'off',
'semi': 'error',
'style/semi': 'off',
'test/prefer-lowercase-title': 'off',
'test/consistent-test-it': 'off',
},
},
{
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"name": "native-pubsub",
"type": "module",
"version": "0.0.7",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/itswillta/native-pubsub.git"
},
"exports": {
".": {
"import": "./dist/native-pubsub.js",
Expand All @@ -14,10 +19,7 @@
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/itswillta/native-pubsub.git"
},

"scripts": {
"dev": "vite",
"build": "tsc && vite build",
Expand All @@ -43,8 +45,5 @@
"vite": "^5.2.11",
"vite-plugin-dts": "^3.9.1",
"vitest": "^1.6.0"
},
"lint-staged": {
"*.ts": "pnpm run lint"
}
}
32 changes: 16 additions & 16 deletions test/lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';

import { EventBus } from '../src';

Expand All @@ -7,56 +7,56 @@ describe('Given EventBus class', () => {
test('Then an event can be subscribed to and published synchronously', () => {
const eventBus = new EventBus<number>();
let receivedData: number | undefined;

const unsubscribe = eventBus.subscribe((data) => {
receivedData = data;
});

eventBus.publishSync(42);

expect(receivedData).toEqual(42);

unsubscribe();
});

test('Then an event can be subscribed to and published asynchronously', async () => {
const eventBus = new EventBus<string>();
let receivedData: string | undefined;

const unsubscribe = eventBus.subscribe((data) => {
receivedData = data;
});

eventBus.publish('Hello, world!');

expect(receivedData).toBeUndefined();

await Promise.resolve();

expect(receivedData).toEqual('Hello, world!');

unsubscribe();
});

test('Then publishing and subscribing to multiple events are allowed', () => {
const eventBus = new EventBus<boolean>();
let receivedData1: boolean | undefined;
let receivedData2: boolean | undefined;

const unsubscribe1 = eventBus.subscribe((data) => {
receivedData1 = data;
}, 'event1');

const unsubscribe2 = eventBus.subscribe((data) => {
receivedData2 = data;
}, 'event2');

eventBus.publishSync(true, 'event1');
eventBus.publishSync(false, 'event2');

expect(receivedData1).toEqual(true);
expect(receivedData2).toEqual(false);

unsubscribe1();
unsubscribe2();
});
Expand Down

0 comments on commit d58b54e

Please sign in to comment.