Skip to content

Commit

Permalink
Merge branch 'feature/0.4.0' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lisez committed May 24, 2024
2 parents 5f61a61 + 5b09fe9 commit 1fe4adc
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 45 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ npm install xevt
Then:

```typescript
import { Xemitter } from "xevt";
import { Xevt } from "xevt";
```

## Usage

### Basic usage

```typescript
const emitter = new Xemitter();
const emitter = new Xevt();

let result = 0;
emitter.on("event", () => {
Expand All @@ -39,7 +39,7 @@ emitter.emit("event");
### Async event

```typescript
const emitter = new Xemitter();
const emitter = new Xevt();

let result = 0;
emitter.onAsync("event", async () => {
Expand All @@ -50,21 +50,25 @@ await emitter.emit("event");

### Conjoined event

IMPORTANT: conjoined events are not supported any arguments in handlers.

```typescript
const emitter = new Xemitter();
const emitter = new Xevt();

let count = 0;
emitter.on(["event1", "event2"], () => {
count++;
});
emitter.emit("event1");
emitter.emit("event2");

console.log(count); // 1
```

### Mixed async/sync handlers

```typescript
const emitter = new Xemitter();
const emitter = new Xevt();
const result: number[] = [];
emitter.on("event", (data) => {
result.push(data);
Expand All @@ -83,10 +87,12 @@ emitter.onAsync(
for (let i = 0; i < 5; i++) {
emitter.emit("event", i);
}

// [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
```

```typescript
const emitter = new Xemitter();
const emitter = new Xevt();
const result: number[] = [];
emitter.conjoin(["event1", "event2"], async () => {
result.push(1);
Expand All @@ -99,4 +105,6 @@ for (let i = 0; i < 5; i++) {
emitter.emit("event1");
emitter.emit("event2");
}

// [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
```
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"@deno/dnt": "jsr:@deno/dnt@^0.41.1",
"modules/": "./modules/"
},
"exports": "./modules/xemitter.ts"
"exports": "./modules/xevt.ts"
}
4 changes: 2 additions & 2 deletions examples/node/async-event-example.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Xemitter } from "xevt";
import { Xevt } from "xevt";

const emitter = new Xemitter();
const emitter = new Xevt();
emitter.onAsync(
"event",
async (data) =>
Expand Down
4 changes: 2 additions & 2 deletions examples/node/conjoined-event-example.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Xemitter } from "xevt";
import { Xevt } from "xevt";

const emitter = new Xemitter();
const emitter = new Xevt();
emitter.on("event1", (data) => {
console.log("Event1:", data);
});
Expand Down
4 changes: 2 additions & 2 deletions examples/node/mixed-handlers-example.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Xemitter } from "xevt";
import { Xevt } from "xevt";

const emitter = new Xemitter();
const emitter = new Xevt();
emitter.on("event", (data) => {
console.log("sync hanlder:", data);
});
Expand Down
4 changes: 2 additions & 2 deletions examples/node/sync-event-example.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Xemitter } from "xevt";
import { Xevt } from "xevt";

const emitter = new Xemitter();
const emitter = new Xevt();
emitter.on("event", (data) => {
console.log("Event:", data);
});
Expand Down
2 changes: 1 addition & 1 deletion modules/xemitter.ts → modules/xevt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { EmitDone, Emitter } from "./emitter.ts";

export type XeventName = EventName | ConjoinEvents;

export class Xemitter extends CoreEmitter<XeventName>
export class Xevt extends CoreEmitter<XeventName>
implements XevtEmitter, XConjoinEmitter {
private emitter: Emitter;
private conjoinEmitter: ConjoinEmitter;
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ await emptyDir(output);
await build({
test: false,
typeCheck: "both",
entryPoints: ["./modules/xemitter.ts"],
entryPoints: ["./modules/xevt.ts"],
outDir: output,
shims: {
deno: false,
Expand Down
10 changes: 5 additions & 5 deletions tests/deno/general_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { assert, assertEquals, assertIsError } from "jsr:@std/assert";
import { describe, it } from "jsr:@std/testing/bdd";

import { Xemitter } from "modules/xemitter.ts";
import { Xevt } from "modules/xevt.ts";

it("list all events", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
assertEquals(emitter.eventNames(), []);

emitter.on("foo", () => {});
Expand All @@ -21,7 +21,7 @@ it("list all events", () => {
});

it("catch error", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let count = 0;
emitter.on("error", (error) => {
assertIsError(error);
Expand All @@ -40,7 +40,7 @@ it("catch error", () => {
});

it("remove event handlers", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let count = 0;
const handler = () => {
count += 1;
Expand All @@ -53,7 +53,7 @@ it("remove event handlers", () => {
});

it("remove conjoined event handlers", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let count = 0;
const handler = () => {
count += 1;
Expand Down
24 changes: 12 additions & 12 deletions tests/deno/multiple_events_test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { assert, assertEquals } from "jsr:@std/assert";
import { describe, it } from "jsr:@std/testing/bdd";

import { Xemitter } from "modules/xemitter.ts";
import { Xevt } from "modules/xevt.ts";

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

describe("Xemitter - multiple events", () => {
describe("Xevt - multiple events", () => {
it("should listen multiple events", () => {
const emitter = new Xemitter();
const emitter = new Xevt();

let count = 0;
emitter.on(["event1", "event2"], () => {
Expand All @@ -24,7 +24,7 @@ describe("Xemitter - multiple events", () => {
});

it("should listen multiple handlers", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
const result: number[] = [];
emitter.on(["event1", "event2"], () => {
result.push(1);
Expand All @@ -41,7 +41,7 @@ describe("Xemitter - multiple events", () => {
});

it("should listen multiple async handlers", async () => {
const emitter = new Xemitter();
const emitter = new Xevt();
const result: number[] = [];
emitter.conjoinAsync(["event1", "event2"], async () => {
result.push(1);
Expand All @@ -58,7 +58,7 @@ describe("Xemitter - multiple events", () => {
});

it("should listen multiple events with addEventListener", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let count = 0;
emitter.addEventListener(["event1", "event2"], () => {
count++;
Expand All @@ -71,7 +71,7 @@ describe("Xemitter - multiple events", () => {
});

it('should listen multiple events with "conjoin"', () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let count = 0;
emitter.conjoin(["event1", "event2"], () => {
count++;
Expand All @@ -84,7 +84,7 @@ describe("Xemitter - multiple events", () => {
});

it("should remove multiple events", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let count = 0;
const handler = () => {
count++;
Expand All @@ -102,7 +102,7 @@ describe("Xemitter - multiple events", () => {
});

it("should listen multiple events once", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let count = 0;
emitter.on(
["event1", "event2"],
Expand All @@ -119,14 +119,14 @@ describe("Xemitter - multiple events", () => {
});

it("should take every async events", async () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let result: number = 0;
emitter.conjoinAsync(["event1", "event2"], async () => {
await new Promise((resolve) =>
setTimeout(() => {
result++;
resolve(true);
}, 10),
}, 10)
);
});
emitter.emit("event1");
Expand All @@ -142,7 +142,7 @@ describe("Xemitter - multiple events", () => {
});

it("mix handlers", async () => {
const emitter = new Xemitter();
const emitter = new Xevt();
const result: number[] = [];
emitter.conjoin(["event1", "event2"], () => {
result.push(1);
Expand Down
22 changes: 11 additions & 11 deletions tests/deno/single_event_test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { assert, assertEquals } from "jsr:@std/assert";
import { describe, it } from "jsr:@std/testing/bdd";

import { Xemitter } from "modules/xemitter.ts";
import { Xevt } from "modules/xevt.ts";

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

describe("Xemitter - single event", () => {
describe("Xevt - single event", () => {
it("should listen event", () => {
const emitter = new Xemitter();
const emitter = new Xevt();

let result = 0;
emitter.on("event", () => {
Expand All @@ -21,7 +21,7 @@ describe("Xemitter - single event", () => {
});

it("should listen event multiple times", () => {
const emitter = new Xemitter();
const emitter = new Xevt();

let result = 0;
emitter.on("event", () => {
Expand All @@ -33,7 +33,7 @@ describe("Xemitter - single event", () => {
});

it('should listen event with "addEventListener"', () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let result = 0;
emitter.addEventListener("event", () => {
result++;
Expand All @@ -43,7 +43,7 @@ describe("Xemitter - single event", () => {
});

it("should remove event", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let result = 0;
const handler = () => {
result++;
Expand All @@ -57,7 +57,7 @@ describe("Xemitter - single event", () => {
});

it("should listen event once", () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let count = 0;
emitter.on(
"event",
Expand All @@ -74,7 +74,7 @@ describe("Xemitter - single event", () => {
});

it('should listen event with "addEventListener"', () => {
const emitter = new Xemitter();
const emitter = new Xevt();
let result = 0;
emitter.addEventListener("event", () => {
result++;
Expand All @@ -84,14 +84,14 @@ describe("Xemitter - single event", () => {
});

it("should listen every async events", async () => {
const emitter = new Xemitter();
const emitter = new Xevt();
const result: number[] = [];
emitter.onAsync("event", async (arg) => {
await new Promise((resolve) =>
setTimeout(() => {
result.push(arg);
resolve(true);
}, 10),
}, 10)
);
});
emitter.emit("event", 1);
Expand All @@ -107,7 +107,7 @@ describe("Xemitter - single event", () => {
});

it("mix handlers", async () => {
const emitter = new Xemitter();
const emitter = new Xevt();
const result: number[] = [];
emitter.on("event", (data) => {
result.push(data);
Expand Down

0 comments on commit 1fe4adc

Please sign in to comment.