Skip to content

Commit

Permalink
4 support debug mode (#7)
Browse files Browse the repository at this point in the history
* feat: add debug option

* chore: update readme
  • Loading branch information
lisez authored May 25, 2024
1 parent 4f76227 commit f1c67eb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ for (let i = 0; i < 5; i++) {
// [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
```

### Debug mode

```typescript
const emitter = new Xevt();
emitter.debug = true;
```

Then you can see the debug information in the console.

## Similar projects

- [mitt](https://github.com/developit/mitt)
Expand Down
9 changes: 5 additions & 4 deletions modules/conjoin_emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ import type {
import { CoreEmitter } from "./core_emitter.ts";
import { Emitter } from "./emitter.ts";

export class ConjoinEmitter
extends CoreEmitter<ConjoinEvents>
implements XConjoinEmitter
{
export class ConjoinEmitter extends CoreEmitter<ConjoinEvents>
implements XConjoinEmitter {
private nameIndex: Map<EventName, number> = new Map();
private conjoinedNames: Map<EventName, ConjoinEvents> = new Map();
private indexCounter = 0;
private waitingQueue: PendingConjoinEvent[] = [];
private idleQueue: PendingConjoinEvent[] = [];
private errorEmitter = new Emitter();
private prevEvents?: Promise<any>;
debug = false;

private internalConjoinOn(signature: EventHandlerSignature<ConjoinEvents>) {
if (signature.name.length < 2) {
Expand Down Expand Up @@ -139,6 +138,7 @@ export class ConjoinEmitter
}

emit(event: EventName): any {
if (this.debug) this.logger.debug("emit", event);
if (!this.nameIndex.has(event)) return;

let executing: EventName[] = [];
Expand All @@ -157,6 +157,7 @@ export class ConjoinEmitter
}));

if (executing.length) {
if (this.debug) this.logger.debug("conjoined", executing);
if (this.prevEvents) {
this.prevEvents = this.prevEvents.then(() => this.exec(0, executing));
} else {
Expand Down
7 changes: 7 additions & 0 deletions modules/core_emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import type {
XCoreEmitter,
} from "./types.ts";

import { Logger } from "./logger.ts";

export abstract class CoreEmitter<T> implements XCoreEmitter<T> {
protected handlers: RegisteredHandlers;
abstract debug: boolean;

logger = new Logger();

constructor(handlers?: RegisteredHandlers) {
this.handlers = handlers || new Map();
Expand Down Expand Up @@ -49,6 +54,8 @@ export abstract class CoreEmitter<T> implements XCoreEmitter<T> {
throw new Error("Async handler must be a promise or thenable");
}

if (this.debug) this.logger.debug("on", name, signature);

const handlers = this.handlers.get(name);
if (handlers) {
handlers.push(signature);
Expand Down
5 changes: 4 additions & 1 deletion modules/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const EmitDone = Symbol("emit_done");

export class Emitter extends CoreEmitter<EventName> implements XevtEmitter {
private prevEvents?: Promise<any>;
debug = false;

on(event: EventName, handler: EventHandler, options?: Partial<EventOptions>) {
const signature = {
Expand Down Expand Up @@ -49,6 +50,8 @@ export class Emitter extends CoreEmitter<EventName> implements XevtEmitter {
}

emit(event: EventName, ...args: any[]): any {
if (this.debug) this.logger.debug("emit", event, args);

const handlers = this.handlers.get(event)?.slice() || [];
handlers
.filter((e) => e.options?.once)
Expand All @@ -59,7 +62,7 @@ export class Emitter extends CoreEmitter<EventName> implements XevtEmitter {
try {
if (this.prevEvents) {
this.prevEvents = this.prevEvents.then(() =>
this.internalExec(0, handlers, ...args),
this.internalExec(0, handlers, ...args)
);
} else {
this.prevEvents = this.internalExec(0, handlers, ...args);
Expand Down
8 changes: 8 additions & 0 deletions modules/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class Logger implements Pick<Console, "debug"> {
private ns = "[xevt]";

debug(...data: any[]): void {
console.debug(this.ns, ...data);
}
}

2 changes: 2 additions & 0 deletions modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export type EventUnregister<T> = Record<

export type XCoreEmitter<T> =
& {
debug: boolean;
logger: Pick<Console, "debug">;
eventNames(): EventName[];
emit(event: EventName, ...args: any[]): void;
error(handler: ErrorHandler): void;
Expand Down
11 changes: 11 additions & 0 deletions modules/xevt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Xevt extends CoreEmitter<XeventName>
implements XevtEmitter, XConjoinEmitter {
private emitter: Emitter;
private conjoinEmitter: ConjoinEmitter;
private _debug = false;

constructor(map?: RegisteredHandlers) {
super(map);
Expand All @@ -28,6 +29,16 @@ export class Xevt extends CoreEmitter<XeventName>
this.conjoinEmitter = new ConjoinEmitter(this.handlers);
}

set debug(debug: boolean) {
this.emitter.debug = debug;
this.conjoinEmitter.debug = debug;
this._debug = debug;
}

get debug() {
return this._debug;
}

on(
event: XeventName,
handler: EventHandler,
Expand Down

0 comments on commit f1c67eb

Please sign in to comment.