-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevent-adapter.js
177 lines (162 loc) · 4.28 KB
/
event-adapter.js
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
"use strict";
/**
* @typedef {import('../domain').Model} Model
* @typedef {string} serviceName
*
* @typedef {Object} EventMessage
* @property {serviceName} eventSource
* @property {serviceName|"broadcast"} eventTarget
* @property {"command"|"commandResponse"|"notification"|"import"} eventType
* @property {string} eventName
* @property {string} eventTime
* @property {string} eventUuid
* @property {NotificationEvent|ImportEvent|CommandEvent} eventData
*
* @typedef {object} ImportEvent
* @property {"service"|"model"|"adapter"} type
* @property {string} url
* @property {string} path
* @property {string} importRemote
*
* @typedef {object} NotificationEvent
* @property {string|} message
* @property {"utf8"|Uint32Array} encoding
*
* @typedef {Object} CommandEvent
* @property {string} commandName
* @property {string} commandResp
* @property {*} commandArgs
*/
/**
* @typedef {{
* filter:function(message):Promise<void>,
* unsubscribe:function()
* }} Subscription
* @typedef {string|RegExp} topic
* @callback eventHandler
* @param {string} eventData
* @typedef {eventHandler} notifyType
* @typedef {{
* listen:function(topic, x),
* notify:notifyType
* }} EventService
* @callback adapterFactory
* @param {EventService} service
* @returns {function(topic, eventHandler)}
*/
import { Event } from "../services/event-service";
/**
* @type {Map<any,Map<string,*>>}
*/
const subscriptions = new Map();
/**
* Test the filter.
* @param {string} message
* @returns {function(string|RegExp):boolean} did the filter match?
*/
function filterMatches(message) {
return function (filter) {
const regex = new RegExp(filter);
const result = regex.test(message);
if (result)
console.debug({
func: filterMatches.name,
filter,
result,
message: message.substring(0, 100).concat("..."),
});
return result;
};
}
/**
* @typedef {string} message
* @typedef {string|RegExp} topic
* @param {{
* id:string,
* callback:function(message,Subscription),
* topic:topic,
* filter:string|RegExp,
* once:boolean,
* model:import("../domain").Model
* }} options
*/
const Subscription = function ({ id, callback, topic, filters, once, model }) {
return {
/**
* unsubscribe from topic
*/
unsubscribe() {
subscriptions.get(topic).delete(id);
},
getId() {
return id;
},
getModel() {
return model;
},
getSubscriptions() {
return [...subscriptions.entries()];
},
/**
* Filter message and invoke callback
* @param {string} message
*/
async filter(message) {
if (filters) {
// Every filter must match.
if (filters.every(filterMatches(message))) {
if (once) {
// Only looking for 1 msg, got it.
this.unsubscribe();
}
await callback({ message, subscription: this });
return;
}
// no match
return;
}
// no filters defined, just invoke the callback.
await callback({ message, subscription: this });
},
};
};
/**
* Listen for external events with default event service if none specified.
* @type {adapterFactory}
* @param {import('../services/event-service').Event} [service] - has default service
*/
export function listen(service = Event) {
return async function (options) {
const {
model,
args: [arg],
} = options;
const subscription = Subscription({ model, ...arg });
if (subscriptions.has(arg.topic)) {
subscriptions.get(arg.topic).set(arg.id, subscription);
return subscription;
}
subscriptions.set(arg.topic, new Map().set(arg.id, subscription));
if (!service.listening) {
service.listen(/Channel/, async function ({ topic, message }) {
if (subscriptions.has(topic)) {
subscriptions.get(topic).forEach(async subscription => {
await subscription.filter(message);
});
}
});
}
return subscription;
};
}
/**
* @type {adapterFactory}
* @returns {function(topic, eventData)}
*/
export function notify(service = Event) {
return async function ({ model, args: [topic, message] }) {
console.debug("sending...", { topic, message: JSON.parse(message) });
await service.notify(topic, message);
return model;
};
}