-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Hub.ts
235 lines (206 loc) · 5.33 KB
/
Hub.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ConsoleLogger as Logger } from './Logger';
const logger = new Logger('Hub');
const AMPLIFY_SYMBOL = (
typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
? Symbol.for('amplify_default')
: '@@amplify_default'
) as Symbol;
interface IPattern {
pattern: RegExp;
callback: HubCallback;
}
interface IListener {
name: string;
callback: HubCallback;
}
export type HubCapsule = {
channel: string;
payload: HubPayload;
source: string;
patternInfo?: string[];
};
export type HubPayload = {
event: string;
data?: any;
message?: string;
};
export type HubCallback = (capsule: HubCapsule) => void;
export type LegacyCallback = { onHubCapsule: HubCallback };
function isLegacyCallback(callback: any): callback is LegacyCallback {
return (<LegacyCallback>callback).onHubCapsule !== undefined;
}
export class HubClass {
name: string;
private listeners: IListener[] = [];
private patterns: IPattern[] = [];
protectedChannels = [
'core',
'auth',
'api',
'analytics',
'interactions',
'pubsub',
'storage',
'ui',
'xr',
];
constructor(name: string) {
this.name = name;
}
/**
* Used internally to remove a Hub listener.
*
* @remarks
* This private method is for internal use only. Instead of calling Hub.remove, call the result of Hub.listen.
*/
private _remove(channel: string | RegExp, listener: HubCallback) {
if (channel instanceof RegExp) {
const pattern = this.patterns.find(
({ pattern }) => pattern.source === channel.source
);
if (!pattern) {
logger.warn(`No listeners for ${channel}`);
return;
}
this.patterns = [...this.patterns.filter(x => x !== pattern)];
} else {
const holder = this.listeners[channel];
if (!holder) {
logger.warn(`No listeners for ${channel}`);
return;
}
this.listeners[channel] = [
...holder.filter(({ callback }) => callback !== listener),
];
}
}
/**
* @deprecated Instead of calling Hub.remove, call the result of Hub.listen.
*/
remove(channel: string | RegExp, listener: HubCallback) {
this._remove(channel, listener);
}
/**
* Used to send a Hub event.
*
* @param channel - The channel on which the event will be broadcast
* @param payload - The HubPayload
* @param source - The source of the event; defaults to ''
* @param ampSymbol - Symbol used to determine if the event is dispatched internally on a protected channel
*
*/
dispatch(
channel: string,
payload: HubPayload,
source: string = '',
ampSymbol?: Symbol
) {
if (this.protectedChannels.indexOf(channel) > -1) {
const hasAccess = ampSymbol === AMPLIFY_SYMBOL;
if (!hasAccess) {
logger.warn(
`WARNING: ${channel} is protected and dispatching on it can have unintended consequences`
);
}
}
const capsule: HubCapsule = {
channel,
payload: { ...payload },
source,
patternInfo: [],
};
try {
this._toListeners(capsule);
} catch (e) {
logger.error(e);
}
}
/**
* Used to listen for Hub events.
*
* @param channel - The channel on which to listen
* @param callback - The callback to execute when an event is received on the specified channel
* @param listenerName - The name of the listener; defaults to 'noname'
* @returns A function which can be called to cancel the listener.
*
*/
listen(
channel: string | RegExp,
callback?: HubCallback | LegacyCallback,
listenerName = 'noname'
) {
let cb: HubCallback;
// Check for legacy onHubCapsule callback for backwards compatability
if (isLegacyCallback(callback)) {
logger.warn(
`WARNING onHubCapsule is Deprecated. Please pass in a callback.`
);
cb = callback.onHubCapsule.bind(callback);
} else if (typeof callback !== 'function') {
throw new Error('No callback supplied to Hub');
} else {
cb = callback;
}
if (channel instanceof RegExp) {
this.patterns.push({
pattern: channel,
callback: cb,
});
} else {
let holder = this.listeners[channel];
if (!holder) {
holder = [];
this.listeners[channel] = holder;
}
holder.push({
name: listenerName,
callback: cb,
});
}
return () => {
this._remove(channel, cb);
};
}
private _toListeners(capsule: HubCapsule) {
const { channel, payload } = capsule;
const holder = this.listeners[channel];
if (holder) {
holder.forEach(listener => {
logger.debug(`Dispatching to ${channel} with `, payload);
try {
listener.callback(capsule);
} catch (e) {
logger.error(e);
}
});
}
if (this.patterns.length > 0) {
if (!payload.message) {
logger.warn(`Cannot perform pattern matching without a message key`);
return;
}
const payloadStr = payload.message;
this.patterns.forEach(pattern => {
const match = payloadStr.match(pattern.pattern);
if (match) {
const [, ...groups] = match;
const dispatchingCapsule: HubCapsule = {
...capsule,
patternInfo: groups,
};
try {
pattern.callback(dispatchingCapsule);
} catch (e) {
logger.error(e);
}
}
});
}
}
}
/*We export a __default__ instance of HubClass to use it as a
pseudo Singleton for the main messaging bus, however you can still create
your own instance of HubClass() for a separate "private bus" of events.*/
export const Hub = new HubClass('__default__');