Skip to content

Commit 32c34a9

Browse files
committed
wip symfony style event dispatcher system
1 parent e52c23a commit 32c34a9

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

src/Type/Definition/Event.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { EventIdentifier } from './EventIdentifier.js';
2+
3+
interface EventInterface {
4+
getIdentifier(): EventIdentifier;
5+
6+
isPropagationStopped(): boolean;
7+
stopPropagation(): this;
8+
9+
getContextValue(key: string): unknown;
10+
setContextValue(key: string, value: unknown): this;
11+
hasContextValue(key: string): boolean;
12+
clearContextValue(key: string): this;
13+
getContext(): Readonly<Record<string, unknown>>;
14+
setContext(context: Record<string, unknown>): this;
15+
clearContext(): this;
16+
}
17+
18+
class Event implements EventInterface {
19+
private readonly identifier: EventIdentifier;
20+
private stopped: boolean = false;
21+
private context: Record<string, unknown>;
22+
23+
constructor(identifier: EventIdentifier, context: Record<string, unknown> = {}) {
24+
this.identifier = identifier;
25+
this.context = context;
26+
}
27+
28+
getIdentifier(): EventIdentifier {
29+
return this.identifier;
30+
}
31+
32+
isPropagationStopped(): boolean {
33+
return this.stopped;
34+
}
35+
36+
stopPropagation(): this {
37+
this.stopped = true;
38+
return this;
39+
}
40+
41+
getContextValue(key: string): unknown {
42+
return this.context[key];
43+
}
44+
45+
setContextValue(key: string, value: unknown): this {
46+
this.context[key] = value;
47+
return this;
48+
}
49+
50+
hasContextValue(key: string): boolean {
51+
return Object.prototype.hasOwnProperty.call(this.context, key);
52+
}
53+
54+
clearContextValue(key: string): this {
55+
delete this.context[key];
56+
return this;
57+
}
58+
59+
getContext(): Readonly<Record<string, unknown>> {
60+
return { ...this.context };
61+
}
62+
63+
setContext(context: Record<string, unknown>): this {
64+
this.context = context;
65+
return this;
66+
}
67+
68+
clearContext(): this {
69+
this.context = {};
70+
return this;
71+
}
72+
}
73+
74+
export { EventInterface, Event };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {EventInterface} from "./Event";
2+
3+
// todo: implement similar logic as in https://github.com/simshaun/ts-event-dispatcher/blob/master/src/index.ts
4+
interface EventDispatcher {
5+
dispatchEvent(event: EventInterface): this;
6+
}
7+
8+
export {EventDispatcher};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Branded } from './Branded.js';
2+
import { ParseError } from '../../Error/index.js';
3+
4+
/**
5+
* Type safe variant of string containing event identifier.
6+
*/
7+
type EventIdentifier = Branded<string, 'eventIdentifier'>;
8+
9+
const eventIdentifierRegex = /^([a-z][a-z0-9-]*)(\.([a-z][a-z0-9-]*))*$/;
10+
11+
function validateEventIdentifierFromString(eventIdentifier: string): EventIdentifier {
12+
if (!eventIdentifierRegex.test(eventIdentifier)) {
13+
throw new ParseError('Passed variable is not a valid event identifier.');
14+
}
15+
return eventIdentifier as EventIdentifier;
16+
}
17+
18+
export { EventIdentifier, validateEventIdentifierFromString, eventIdentifierRegex };

src/Type/Definition/EventListener.ts

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Branded } from './Branded.js';
2+
import { ParseError } from '../../Error/index.js';
3+
4+
/**
5+
* Type safe variant of string containing event identifier.
6+
*/
7+
type EventListenerIdentifier = Branded<string, 'eventListenerIdentifier'>;
8+
9+
const eventListenerIdentifierRegex =
10+
/^(?:([a-z][a-z0-9-]*)(\.([a-z][a-z0-9-]*))*|\*|([a-z][a-z0-9-]*)(\.([a-z][a-z0-9-]*))*(\.\*))$/;
11+
12+
function validateEventListenerIdentifierFromString(eventListenerIdentifier: string): EventListenerIdentifier {
13+
if (!eventListenerIdentifierRegex.test(eventListenerIdentifier)) {
14+
throw new ParseError('Passed variable is not a valid event listener identifier.');
15+
}
16+
return eventListenerIdentifier as EventListenerIdentifier;
17+
}
18+
19+
export { EventListenerIdentifier, validateEventListenerIdentifierFromString, eventListenerIdentifierRegex };

0 commit comments

Comments
 (0)