This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
119 lines (94 loc) · 3.08 KB
/
index.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Group, Participant } from '../../common/types/participant.types';
import { Logger, Observer } from '../../common/utils';
import config from '../../services/config';
import { EventBus } from '../../services/event-bus';
import { AblyRealtimeService } from '../../services/realtime';
import { DefaultAttachComponentOptions } from './types';
export abstract class BaseComponent {
private observers: Record<string, Observer> = {};
protected localParticipant: Participant;
protected group: Group;
protected realtime: AblyRealtimeService;
protected eventBus: EventBus;
public abstract name: string;
protected abstract logger: Logger;
protected isAttached = false;
/**
* @function attach
* @description attach component
* @returns {void}
*/
public attach = (params: DefaultAttachComponentOptions): void => {
if (Object.values(params).includes(null) || Object.values(params).includes(undefined)) {
const message = `${this.name} @ attach - params are required`;
this.logger.log(message);
throw new Error(message);
}
const { realtime, localParticipant, group, config: globalConfig, eventBus } = params;
config.setConfig(globalConfig);
this.realtime = realtime;
this.localParticipant = localParticipant;
this.group = group;
this.eventBus = eventBus;
this.isAttached = true;
this.logger.log('attached');
this.start();
};
/*
* @function detach
* @description detach component
* @returns {void}
* */
public detach = (): void => {
if (!this.isAttached) {
this.logger.log(`${this.name} @ detach - component is not attached}`);
return;
}
this.logger.log('detached');
this.destroy();
this.realtime = undefined;
this.localParticipant = undefined;
this.isAttached = false;
Object.keys(this.observers).forEach((type) => this.unsubscribe(type));
};
/**
* @function subscribe
* @description Subscribe to an event
* @param type - event type
* @param listener - event callback
* @returns {void}
*/
public subscribe = (type: string, listener: Function): void => {
this.logger.log(`subscribed to ${type} event`);
if (!this.observers[type]) {
this.observers[type] = new Observer({ logger: this.logger });
}
this.observers[type].subscribe(listener);
};
/**
* @function unsubscribe
* @description Unsubscribe from an event
* @param type - event type
* @returns {void}
*/
public unsubscribe = (type: string): void => {
this.logger.log(`unsubscribed from ${type} event`);
if (!this.observers[type]) return;
this.observers[type].reset();
delete this.observers[type];
};
/**
* @function publish
* @description Publish an event to client
* @param type - event type
* @param data - event data
* @returns {void}
*/
protected publish = (type: string, data?: unknown): void => {
const hasListenerRegistered = type in this.observers;
if (!hasListenerRegistered) return;
this.observers[type].publish(data);
};
protected abstract destroy(): void;
protected abstract start(): void;
}