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

fix(socketio): injecting a session into $onDisconnect #2384

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe("SocketHandlersBuilder", () => {
});
});
describe("onConnection()", () => {
it("should build handler and invoke onConnection instance method", () => {
it("should build handler and invoke onConnection instance method", async () => {
const instance = {
$onConnection: jest.fn()
};
Expand Down Expand Up @@ -140,7 +140,7 @@ describe("SocketHandlersBuilder", () => {
const buildHandlersStub = jest.spyOn(builder, "buildHandlers").mockReturnValue(undefined);
const createSessionStub = jest.spyOn(builder, "createSession").mockReturnValue(undefined);

builder.onConnection(socketStub, nspStub);
await builder.onConnection(socketStub, nspStub);

expect(buildHandlersStub).toBeCalledWith(socketStub, nspStub);
expect(createSessionStub).toBeCalledWith(socketStub);
Expand All @@ -155,7 +155,7 @@ describe("SocketHandlersBuilder", () => {
});
});
describe("onDisconnect()", () => {
it("should call the createSession method and create the $onDisconnect method if is missing", () => {
it("should call the createSession method and create the $onDisconnect method if is missing", async () => {
const instance = {
$onDisconnect: jest.fn()
};
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("SocketHandlersBuilder", () => {
const invokeStub = jest.spyOn(builder, "invoke").mockReturnValue(undefined);
const destroySessionStub = jest.spyOn(builder, "destroySession").mockReturnValue(undefined);

builder.onDisconnect(socketStub, nspStub);
await builder.onDisconnect(socketStub, nspStub);

expect(destroySessionStub).toBeCalledWith(socketStub);
expect(invokeStub).toBeCalledWith(
Expand All @@ -198,7 +198,7 @@ describe("SocketHandlersBuilder", () => {
);
});

it("should pass the disconnection reason", () => {
it("should pass the disconnection reason", async () => {
const instance = {
$onDisconnect: jest.fn()
};
Expand Down Expand Up @@ -229,7 +229,7 @@ describe("SocketHandlersBuilder", () => {
const invokeStub = jest.spyOn(builder, "invoke").mockReturnValue(undefined);
jest.spyOn(builder, "destroySession").mockReturnValue(undefined);

builder.onDisconnect(socketStub, nspStub, reason);
await builder.onDisconnect(socketStub, nspStub, reason);

expect(invokeStub).toBeCalledWith(
instance,
Expand All @@ -241,6 +241,41 @@ describe("SocketHandlersBuilder", () => {
}
);
});

it("should destroy the session only when $onDisconnect is completed invocation", async () => {
const instance = {
$onDisconnect: jest.fn()
};

const provider: any = {
store: {
get: jest.fn().mockReturnValue({
injectNamespace: "nsp",
handlers: {
$onDisconnect: {
eventName: "onDisconnect"
}
}
})
}
};
const nspStub: any = {nsp: "nsp"};
const socketStub: any = {
on: jest.fn()
};

const builder: any = new SocketHandlersBuilder(provider, {
get() {
return instance;
}
} as any);
const invokeStub = jest.spyOn(builder, "invoke").mockReturnValue(undefined);
const destroySessionStub = jest.spyOn(builder, "destroySession").mockReturnValue(undefined);

await builder.onDisconnect(socketStub, nspStub);

expect(destroySessionStub.mock.invocationCallOrder[0]).toBeGreaterThan(invokeStub.mock.invocationCallOrder[0]);
});
});
describe("createSession()", () => {
it("should create session for the socket", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,24 @@ export class SocketHandlersBuilder {
* @param {Socket} socket
* @param {Namespace} nsp
*/
public onConnection(socket: Socket, nsp: Namespace) {
public async onConnection(socket: Socket, nsp: Namespace) {
const {socketProviderMetadata} = this;
const instance = this.injector.get(this.provider.token);

this.buildHandlers(socket, nsp);
this.createSession(socket);

if (instance.$onConnection) {
this.invoke(instance, socketProviderMetadata.$onConnection, {socket, nsp});
await this.invoke(instance, socketProviderMetadata.$onConnection, {socket, nsp});
}
}

public onDisconnect(socket: Socket, nsp: Namespace, reason?: string) {
public async onDisconnect(socket: Socket, nsp: Namespace, reason?: string) {
const instance = this.injector.get(this.provider.token);
const {socketProviderMetadata} = this;

if (instance.$onDisconnect) {
this.invoke(instance, socketProviderMetadata.$onDisconnect, {socket, nsp, reason});
await this.invoke(instance, socketProviderMetadata.$onDisconnect, {socket, nsp, reason});
}

this.destroySession(socket);
Expand Down Expand Up @@ -136,7 +136,6 @@ export class SocketHandlersBuilder {
}

private runQueue(handlerMetadata: SocketHandlerMetadata, args: any[], socket: Socket, nsp: Namespace) {
let promise: any = Promise.resolve(args);
const instance = this.injector.get(this.provider.token);
const {useBefore, useAfter} = this.socketProviderMetadata;
const scope = {
Expand All @@ -146,7 +145,7 @@ export class SocketHandlersBuilder {
eventName: handlerMetadata.eventName
};

promise = promise.then(() => this.deserialize(handlerMetadata, scope));
let promise = Promise.resolve().then(() => this.deserialize(handlerMetadata, scope));

if (useBefore) {
useBefore.forEach((target: any) => (promise = this.bindMiddleware(target, scope, promise)));
Expand Down