-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-den-pub-sub.ts
More file actions
63 lines (56 loc) · 2.02 KB
/
data-den-pub-sub.ts
File metadata and controls
63 lines (56 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { DataDenPublishedEvent } from './data-den-published-event';
import { DataDenEvent } from './data-den-event';
type DataDenEventCallback = (event: DataDenEvent) => void;
export class DataDenPubSub {
#listeners: { [key: string]: DataDenEventCallback[] } = {
'command:pagination:load-first-page:start': [],
'command:pagination:load-prev-page:start': [],
'command:pagination:load-next-page:start': [],
'command:pagination:load-last-page:start': [],
'info:pagination:data-change:done': [],
'info:pagination:info-change:done': [],
'info:pagination:page-size-change:done': [],
'info:dragging:columns-reorder:done': [],
'command:sorting:start': [],
'info:filtering:get-active-quick-filter:done': [],
'info:filtering:header-filter-changed': [],
'info:filtering:active-filters-changed': [],
'info:filtering:quick-filter-changed': [],
'info:filtering:active-quick-filter-changed': [],
'info:resizing:mousedown': [],
'info:resizing:start': [],
'info:resizing:done': [],
'command:fetch:start': [],
'command:fetch:sort-start': [],
'info:fetch:done': [],
'command:pin-column:start': [],
'command:rerendering:done': [],
'command:group-column:start': [],
'command:ungroup-column:start': [],
'command:group:update': []
};
publish(name: string, data: DataDenPublishedEvent) {
if (!this.#listeners[name]) {
return;
}
const event = new DataDenEvent(name, data, data.context);
this.#listeners[name].forEach((callback) => {
callback(event);
});
}
subscribe(name: string, callback: DataDenEventCallback) {
if (!this.#listeners[name]) {
throw new Error(`Could not subscribe: Unsupported event: ${name}`);
}
this.#listeners[name].push(callback);
return () => {
this.#unsubscribe(name, callback);
};
}
#unsubscribe(name: string, callback: DataDenEventCallback) {
if (!this.#listeners[name]) {
return;
}
this.#listeners[name] = this.#listeners[name].filter((cb) => cb !== callback);
}
}