Skip to content

Commit 6f7e42a

Browse files
committed
sendWithCondition
1 parent e0aa1c4 commit 6f7e42a

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

js/network/onewaydata/changelog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 6.2.0
2+
3+
* add .sendWithCondition(messageObject, condition)
4+
15
# 6.1.0
26

37
* data must be a string
@@ -10,7 +14,7 @@
1014
* Completed Readme
1115
* Better handling of badly formatted requests
1216
* Set reconnection time to 10000ms
13-
* Add possibility to change reconnection time with reconnectionTimeh
17+
* Add possibility to change reconnection time with reconnectionTime
1418
* Removed a weird condition
1519
* Simplify code with optional chaining
1620
* Fix reconnection

js/network/onewaydata/examples/client.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h2>Message sent with event type football/goal</h2>
2828
const li = document.createElement(`li`);
2929
li.textContent = `goal for ${event.data}`;
3030
goals.appendChild(li);
31-
});
31+
});
3232
</script>
3333
</body>
3434
</html>

js/network/onewaydata/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ eventStream.on(RECONNECT, ({lastId, response}) => {
3939
sendOne(response, `here is what you missed since last disconnection`);
4040
}
4141
});
42+
43+
// eventStream.off(RECONNECT); // unsubscribe
44+
// eventStream.close(); // closes all eventStream
45+
// eventStream.setAndSendId(); // shorthand to send id only and saving lastEventId
46+
// same as send but takes a filter function
47+
eventStream.sendWithCondition({
48+
data: `latitude434521, longitude`,
49+
event: "fire"},
50+
function (request, response) {
51+
return request.isFirefighter; // only send to isFirefighter === true
52+
});
4253
```
4354

4455
### with express/polka

js/network/onewaydata/source/onewaydata.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const createEventStream = (options) => {
7676
const eventStream = Emitter({ lastEventId: 0 });
7777

7878
let responses = [];
79+
let requests = [];
7980
let onRequest;
8081

8182
const requestHandler = (request, response) => {
@@ -94,6 +95,7 @@ const createEventStream = (options) => {
9495
[`Content-Type`]: MIME,
9596
});
9697
responses.push(response);
98+
requests.push(request);
9799

98100
const lastId = request.headers[LAST_ID];
99101
if (lastId) {
@@ -110,6 +112,7 @@ const createEventStream = (options) => {
110112
const index = responses.indexOf(response);
111113
if (index !== -1) {
112114
responses.splice(index, 1);
115+
requests.push(index, 1);
113116
eventStream.emit(DISCONNECT, { response });
114117
}
115118
});
@@ -124,7 +127,9 @@ const createEventStream = (options) => {
124127
responses.forEach(response => {
125128
response.end();
126129
});
127-
responses = [];
130+
responses = undefined;
131+
requests = undefined;
132+
eventStream.off()
128133
};
129134

130135
const setAndSendId = (id) => {
@@ -142,6 +147,15 @@ const createEventStream = (options) => {
142147
});
143148
};
144149

150+
const sendWithCondition = (messageObject, condition) => {
151+
const message = formatEvent(messageObject);
152+
responses.forEach((response, i) => {
153+
if (condition(requests[i], response)) {
154+
response.write(message);
155+
}
156+
});
157+
};
158+
145159
if (asMiddleWare) {
146160
eventStream.middleWare = (request, response, next) => {
147161
const handled = requestHandler(request, response);
@@ -152,14 +166,15 @@ const createEventStream = (options) => {
152166
} else {
153167
onRequest = (request, response) => {
154168
if (condition(request)) {
155-
requestHandler(request, response, responses);
169+
requestHandler(request, response);
156170
}
157171
};
158172
server.on(`request`, onRequest);
159173
}
160174

161175
return Object.assign(eventStream, {
162176
send,
177+
sendWithCondition,
163178
setAndSendId,
164179
close,
165180
});

0 commit comments

Comments
 (0)