Skip to content

Commit a961147

Browse files
committed
saving
1 parent e7cc602 commit a961147

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface ProjectConfigManager {
2+
onReady(): Promise<void>;
3+
getConfig(): ProjectConfig | null;
4+
onUpdate(listener: (config: ProjectConfig) => void): () => void;
5+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
type Consumer<T> = (arg: T) => void;
2+
3+
type Disposer = () => void;
4+
5+
interface Listeners<T> {
6+
[index: string]: {
7+
// index is event name
8+
[index: string]: Listener; // index is listener id
9+
};
10+
}
11+
12+
export class EventEmitter<T> {
13+
private listeners: Listeners = {};
14+
15+
private listenerId = 1;
16+
17+
on<E extends keyof T>(eventName: E, listener: Consumer<T[E]>): Disposer {
18+
if (!this.listeners[eventName]) {
19+
this.listeners[eventName] = {};
20+
}
21+
const currentListenerId = String(this.listenerId);
22+
this.listenerId++;
23+
this.listeners[eventName][currentListenerId] = listener;
24+
return (): void => {
25+
if (this.listeners[eventName]) {
26+
delete this.listeners[eventName][currentListenerId];
27+
}
28+
};
29+
}
30+
31+
emit(eventName: string, arg?: DatafileUpdate): void {
32+
const listeners = this.listeners[eventName];
33+
if (listeners) {
34+
Object.keys(listeners).forEach(listenerId => {
35+
const listener = listeners[listenerId];
36+
listener(arg);
37+
});
38+
}
39+
}
40+
41+
removeAllListeners(): void {
42+
this.listeners = {};
43+
}
44+
}

0 commit comments

Comments
 (0)