Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options for controlling buffer usage #1414

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ export interface ManagerOptions extends EngineOptions {
* the parser to use. Defaults to an instance of the Parser that ships with socket.io.
*/
parser: any;

/**
* Should we buffer receive events if socket is not connected
* @default true
*/
enableReceiveBuffer: boolean;

/**
* Should we buffer send events if socket is not connected
* @default true
*/
enableSendBuffer: boolean;
}

export class Manager extends Emitter {
Expand All @@ -278,6 +290,14 @@ export class Manager extends Emitter {
* @private
*/
_reconnecting: boolean;
/**
* @private
*/
_enableReceiveBuffer: boolean;
/**
* @private
*/
_enableSendBuffer: boolean;

private readonly uri: string;
private readonly opts: object;
Expand Down Expand Up @@ -333,6 +353,8 @@ export class Manager extends Emitter {
this.encoder = new _parser.Encoder();
this.decoder = new _parser.Decoder();
this._autoConnect = opts.autoConnect !== false;
this._enableReceiveBuffer = opts.enableReceiveBuffer !== false;
this._enableSendBuffer = opts.enableSendBuffer !== false;
if (this._autoConnect) this.open();
}

Expand Down
4 changes: 2 additions & 2 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class Socket extends Emitter {
debug("discard packet as the transport is not currently writable");
} else if (this.connected) {
this.packet(packet);
} else {
} else if (this.io._enableSendBuffer) {
this.sendBuffer.push(packet);
}

Expand Down Expand Up @@ -273,7 +273,7 @@ export class Socket extends Emitter {
args.push(this.ack(packet.id));
}

if (this.connected) {
if (this.connected || !this.io._enableReceiveBuffer) {
this.emitEvent(args);
} else {
this.receiveBuffer.push(args);
Expand Down