ts-game-decorators 1.0.21 → 1.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
- import { Bucket, Collection } from 'couchbase';
1
+ import { Bucket, Collection, Scope } from 'couchbase';
2
2
  export declare function connectToCouchbase(): Promise<Bucket>;
3
3
  export declare function queryData(query: string): Promise<any[]>;
4
4
  export declare function getCollection(name: string, scopeName?: string): Collection;
5
+ export declare function getScope(name: string): Scope;
@@ -6,12 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.connectToCouchbase = connectToCouchbase;
7
7
  exports.queryData = queryData;
8
8
  exports.getCollection = getCollection;
9
+ exports.getScope = getScope;
9
10
  const couchbase_1 = require("couchbase");
10
11
  const dotenv_1 = __importDefault(require("dotenv"));
11
12
  dotenv_1.default.config();
12
13
  let cluster;
13
14
  let bucket;
14
15
  let dataCollection;
16
+ let scopes;
15
17
  async function connectToCouchbase() {
16
18
  try {
17
19
  // Kết nối với Couchbase Cluster
@@ -58,3 +60,17 @@ function getCollection(name, scopeName = '_default') {
58
60
  }
59
61
  return collection;
60
62
  }
63
+ function getScope(name) {
64
+ if (!scopes) {
65
+ scopes = new Map();
66
+ }
67
+ let scope = scopes.get(name);
68
+ if (!scope) {
69
+ scope = bucket.scope(name);
70
+ if (!scope)
71
+ throw new Error('Scope not found');
72
+ scopes.set(name, scope);
73
+ return scope;
74
+ }
75
+ return scope;
76
+ }
@@ -54,18 +54,21 @@ function OnError() {
54
54
  function registerSocketServices(io, serviceClasses, authSocketMiddleware) {
55
55
  // Import internal middleware as fallback
56
56
  const { authSocketToken } = require('../middleware/auth');
57
+ // Create singleton instances ONCE for all connections
58
+ const serviceInstances = serviceClasses.map(ServiceClass => {
59
+ if (!Reflect.getMetadata(SOCKET_SERVICE_META, ServiceClass))
60
+ return null;
61
+ // Nếu class nhận io ở constructor thì truyền vào
62
+ try {
63
+ return new ServiceClass(io);
64
+ }
65
+ catch (_a) {
66
+ return new ServiceClass();
67
+ }
68
+ }).filter(Boolean); // Remove null values
57
69
  io.on('connection', (socket) => {
58
- serviceClasses.forEach(ServiceClass => {
59
- if (!Reflect.getMetadata(SOCKET_SERVICE_META, ServiceClass))
60
- return;
61
- // Nếu class nhận io ở constructor thì truyền vào
62
- let instance;
63
- try {
64
- instance = new ServiceClass(io);
65
- }
66
- catch (_a) {
67
- instance = new ServiceClass();
68
- }
70
+ serviceInstances.forEach((instance) => {
71
+ const ServiceClass = instance.constructor;
69
72
  const events = Reflect.getMetadata(SOCKET_EVENTS_META, ServiceClass) || [];
70
73
  const classAuthen = Reflect.getMetadata(SOCKET_AUTHEN_META, ServiceClass);
71
74
  const methodAuthenArr = Reflect.getMetadata(SOCKET_AUTHEN_META, ServiceClass) || [];
package/dist/index.d.ts CHANGED
@@ -12,3 +12,4 @@ export * from './types/map';
12
12
  export * from './types/index';
13
13
  export { Request, Response, NextFunction, Express, Router } from 'express';
14
14
  export { Server } from 'socket.io';
15
+ export { Collection, Scope } from 'couchbase';
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Server = exports.Router = void 0;
17
+ exports.Scope = exports.Collection = exports.Server = exports.Router = void 0;
18
18
  // Decorators
19
19
  __exportStar(require("./decorators/autoRouter"), exports);
20
20
  __exportStar(require("./decorators/socketDecorators"), exports);
@@ -38,3 +38,6 @@ var express_1 = require("express");
38
38
  Object.defineProperty(exports, "Router", { enumerable: true, get: function () { return express_1.Router; } });
39
39
  var socket_io_1 = require("socket.io");
40
40
  Object.defineProperty(exports, "Server", { enumerable: true, get: function () { return socket_io_1.Server; } });
41
+ var couchbase_1 = require("couchbase");
42
+ Object.defineProperty(exports, "Collection", { enumerable: true, get: function () { return couchbase_1.Collection; } });
43
+ Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return couchbase_1.Scope; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-game-decorators",
3
- "version": "1.0.21",
3
+ "version": "1.0.23",
4
4
  "description": "Express & Socket.IO decorators for auto routing and event handling. using for backend game development.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",