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

refactor(socketio): enhance context management for event listeners #2678

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
173 changes: 163 additions & 10 deletions packages/third-parties/socketio/src/class/SocketHandlersBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {PlatformTest} from "@tsed/common";
import {DIContext, getContext, PlatformTest} from "@tsed/common";
import {Store} from "@tsed/core";
import {InjectorService, ProviderType} from "@tsed/di";
import {SocketFilters} from "../interfaces/SocketFilters";
Expand Down Expand Up @@ -38,6 +38,9 @@ describe("SocketHandlersBuilder", () => {
};

const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
Expand Down Expand Up @@ -114,7 +117,8 @@ describe("SocketHandlersBuilder", () => {
injectNamespaces: [{nsp: "/nsp", propertyKey: "key"}],
handlers: {
$onConnection: {
eventName: "onConnection"
eventName: "connection",
methodClassName: "$onConnection"
} as any
}
})
Expand All @@ -126,6 +130,9 @@ describe("SocketHandlersBuilder", () => {
};

const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
Expand All @@ -139,13 +146,57 @@ describe("SocketHandlersBuilder", () => {
expect(buildHandlersStub).toBeCalledWith(socketStub, nspStub);
expect(invokeStub).toBeCalledWith(
instance,
{eventName: "onConnection"},
{eventName: "connection", methodClassName: "$onConnection"},
{
socket: socketStub,
nsp: nspStub
}
);
});

it("should call the $onConnection in the context", async () => {
let ctx!: DIContext;

const instance = {
$onConnection: jest.fn().mockImplementation(() => {
ctx = getContext();
})
};

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

const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
} as any);

await builder.onConnection(socketStub, nspStub);

expect(ctx).toMatchObject({
id: expect.any(String)
});
});
});
describe("onDisconnect()", () => {
it("should create the $onDisconnect method if is missing", async () => {
Expand All @@ -159,7 +210,8 @@ describe("SocketHandlersBuilder", () => {
injectNamespace: "nsp",
handlers: {
$onDisconnect: {
eventName: "onDisconnect"
eventName: "disconnect",
methodClassName: "$onDisconnect"
}
}
})
Expand All @@ -171,6 +223,9 @@ describe("SocketHandlersBuilder", () => {
};

const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
Expand All @@ -181,7 +236,7 @@ describe("SocketHandlersBuilder", () => {

expect(invokeStub).toBeCalledWith(
instance,
{eventName: "onDisconnect"},
{eventName: "disconnect", methodClassName: "$onDisconnect"},
{
socket: socketStub,
nsp: nspStub
Expand All @@ -200,7 +255,8 @@ describe("SocketHandlersBuilder", () => {
injectNamespace: "nsp",
handlers: {
$onDisconnect: {
eventName: "onDisconnect"
eventName: "disconnect",
methodClassName: "$onDisconnect"
}
}
})
Expand All @@ -213,6 +269,9 @@ describe("SocketHandlersBuilder", () => {
};

const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
Expand All @@ -223,14 +282,58 @@ describe("SocketHandlersBuilder", () => {

expect(invokeStub).toBeCalledWith(
instance,
{eventName: "onDisconnect"},
{eventName: "disconnect", methodClassName: "$onDisconnect"},
{
reason,
socket: socketStub,
nsp: nspStub
}
);
});

it("should call the $onDisconnect in the context", async () => {
let ctx!: DIContext;

const instance = {
$onDisconnect: jest.fn().mockImplementation(() => {
ctx = getContext();
})
};

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

const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
} as any);

await builder.onDisconnect(socketStub, nspStub);

expect(ctx).toMatchObject({
id: expect.any(String)
});
});
});

describe("buildHandlers()", () => {
Expand All @@ -248,17 +351,61 @@ describe("SocketHandlersBuilder", () => {
}
};
const socketStub = {
on: jest.fn()
on: jest.fn().mockImplementation((_, fn) => fn("arg1"))
};
const builder: any = new SocketHandlersBuilder(provider, {} as any);
const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
}
} as any);
jest.spyOn(builder, "runQueue").mockResolvedValue(undefined);

await builder.buildHandlers(socketStub, "ws");
socketStub.on.mock.calls[0][1]("arg1");

expect(socketStub.on).toBeCalledWith("eventName", expect.any(Function));
expect(builder.runQueue).toBeCalledWith(metadata.handlers.testHandler, ["arg1"], socketStub, "ws");
});

it("should call the method instance in the context", async () => {
const metadata = {
handlers: {
testHandler: {
eventName: "eventName",
methodClassName: "testHandler"
}
}
};
let ctx!: DIContext;
const instance = {
testHandler: jest.fn().mockImplementation(() => {
ctx = getContext();
})
};
const provider: any = {
store: {
get: jest.fn().mockReturnValue(metadata)
}
};
let promise!: Promise<unknown>;
const socketStub = {
on: jest.fn().mockImplementation((_, fn) => (promise = fn("arg1")))
};
const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
} as any);

await builder.buildHandlers(socketStub, "ws");
await promise;

expect(ctx).toMatchObject({
id: expect.any(String)
});
});
});
describe("invoke()", () => {
it("should call the method instance", () => {
Expand All @@ -272,6 +419,9 @@ describe("SocketHandlersBuilder", () => {
};

const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
Expand Down Expand Up @@ -299,6 +449,9 @@ describe("SocketHandlersBuilder", () => {
};

const builder: any = new SocketHandlersBuilder(provider, {
alterAsync(_event, fn, _ctx) {
return fn;
},
get() {
return instance;
}
Expand Down
32 changes: 26 additions & 6 deletions packages/third-parties/socketio/src/class/SocketHandlersBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {isFunction, Store} from "@tsed/core";
import {InjectorService, Provider} from "@tsed/di";
import {DIContext, InjectorService, Provider, runInContext} from "@tsed/di";
import {deserialize} from "@tsed/json-mapper";
import {$log} from "@tsed/logger";
import {Namespace, Socket} from "socket.io";
Expand All @@ -11,14 +11,18 @@ import {SocketProviderTypes} from "../interfaces/SocketProviderTypes";
import {SocketReturnsTypes} from "../interfaces/SocketReturnsTypes";
import {SocketProviderMetadata} from "./SocketProviderMetadata";
import {SocketSessionData} from "./SocketSessionData";
import {v4} from "uuid";

/**
* @ignore
*/
export class SocketHandlersBuilder {
private readonly socketProviderMetadata: SocketProviderMetadata;

constructor(private provider: Provider, private injector: InjectorService) {
constructor(
private readonly provider: Provider,
private readonly injector: InjectorService
) {
this.socketProviderMetadata = new SocketProviderMetadata(this.provider.store.get("socketIO"));
}

Expand Down Expand Up @@ -90,7 +94,8 @@ export class SocketHandlersBuilder {
this.buildHandlers(socket, nsp);

if (instance.$onConnection) {
await this.invoke(instance, socketProviderMetadata.$onConnection, {socket, nsp});
const ctx = this.createContext(socket, nsp);
await runInContext(ctx, () => this.invoke(instance, socketProviderMetadata.$onConnection, {socket, nsp}), this.injector);
}
}

Expand All @@ -99,7 +104,8 @@ export class SocketHandlersBuilder {
const {socketProviderMetadata} = this;

if (instance.$onDisconnect) {
await this.invoke(instance, socketProviderMetadata.$onDisconnect, {socket, nsp, reason});
const ctx = this.createContext(socket, nsp);
await runInContext(ctx, () => this.invoke(instance, socketProviderMetadata.$onDisconnect, {socket, nsp, reason}), this.injector);
}
}

Expand All @@ -110,8 +116,9 @@ export class SocketHandlersBuilder {
const {eventName} = handler;

if (eventName) {
socket.on(eventName, (...args) => {
this.runQueue(handler, args, socket, nsp);
socket.on(eventName, async (...args) => {
const ctx = this.createContext(socket, nsp);
await runInContext(ctx, () => this.runQueue(handler, args, socket, nsp), this.injector);
});
}
});
Expand Down Expand Up @@ -240,4 +247,17 @@ export class SocketHandlersBuilder {
}
});
}

private createContext(socket: Socket, nsp: Namespace): DIContext {
return new DIContext({
injector: this.injector,
id: v4().split("-").join(""),
logger: this.injector.logger,
additionalProps: {
module: "socket.io",
sid: socket.id,
namespace: nsp.name
}
});
}
}
Loading