Skip to content

Commit 2a93f06

Browse files
committed
Implemented draining and orderly close to polling.
1 parent 5e9e167 commit 2a93f06

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

lib/transports/polling.js

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,14 @@ Polling.prototype.onPollRequest = function (req, res) {
9090
req.cleanup = cleanup;
9191
req.on('close', onClose);
9292

93-
this.buffer = false;
94-
this.flush();
93+
this.writable = true;
94+
this.emit('drain');
95+
96+
// if we're still writable but had a pending close, trigger an empty send
97+
if (this.writable && this.shouldClose) {
98+
debug('triggering empty send to append close packet');
99+
this.send([]);
100+
}
95101
}
96102
};
97103

@@ -162,41 +168,22 @@ Polling.prototype.onData = function (data) {
162168
};
163169

164170
/**
165-
* Flushes buffers.
166-
*
167-
* @api private
168-
*/
169-
170-
Polling.prototype.flush = function () {
171-
if (this.writeBuffer) {
172-
this.write(parser.encodePayload(this.writeBuffer));
173-
this.writeBuffer = null;
174-
this.emit('drain');
175-
}
176-
};
177-
178-
/**
179-
* Writes a packet.
171+
* Writes a packet payload.
180172
*
181173
* @param {Object} packet
182-
* @param {Function} called when the packet is dispatched
183174
* @api private
184175
*/
185176

186-
Polling.prototype.send = function (packet, fn) {
187-
if (this.buffer) {
188-
if (!this.writeBuffer) {
189-
this.writeBuffer = [];
190-
}
191-
192-
debug('poll buffering packet: type %s | data %s', packet.type, packet.data);
193-
this.writeBuffer.push(packet);
194-
if (fn) this.once('drain', fn);
195-
} else {
196-
debug('poll writing packet: type %s | data %s', packet.type, packet.data);
197-
this.write(parser.encodePayload([packet]));
198-
if (fn) fn();
177+
Polling.prototype.send = function (packets) {
178+
debug('polling writing packets', JSON.stringify(packets));
179+
if (this.shouldClose) {
180+
debug('appending close packet to payload');
181+
packets.push({ type: 'close' });
182+
this.shouldClose();
183+
this.shouldClose = null;
199184
}
185+
186+
this.write(parser.encodePayload(packets));
200187
};
201188

202189
/**
@@ -209,7 +196,7 @@ Polling.prototype.send = function (packet, fn) {
209196
Polling.prototype.write = function (data) {
210197
this.doWrite(data);
211198
this.req.cleanup();
212-
this.buffer = true;
199+
this.writable = false;
213200
};
214201

215202
/**
@@ -225,5 +212,11 @@ Polling.prototype.doClose = function (fn) {
225212
this.dataReq.abort();
226213
}
227214

228-
this.send({ type: 'close' }, fn);
215+
if (this.writable) {
216+
debug('polling transport is writable - closing right away');
217+
this.send([{ type: 'close' }]);
218+
} else {
219+
debug('polling transport not writable - marking that we should close');
220+
this.shouldClose = fn;
221+
}
229222
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy