@domain.js/main 0.3.27 → 0.4.0

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,2 +1,2 @@
1
- import { CnfDef, DepsDef, PubSubDef, Cache } from "./Define";
1
+ import { Cache, CnfDef, DepsDef, PubSubDef } from "./Define";
2
2
  export declare const After: (lru: Pick<Cache, "del">, cnf: CnfDef, deps: Pick<DepsDef, "logger">, pubsub?: PubSubDef | undefined) => void;
@@ -18,6 +18,7 @@ const After = (lru, cnf, deps, pubsub) => {
18
18
  });
19
19
  const del = lru.del.bind(lru);
20
20
  lru.del = (key) => {
21
+ del(key);
21
22
  pub.publish(delSignalChannel, key);
22
23
  };
23
24
  sub.on("message", async (channel, key) => {
@@ -8,7 +8,7 @@ interface Deps {
8
8
  redis: Pick<Redis, "hget" | "hset" | "hdel" | "hincrby">;
9
9
  }
10
10
  export declare function Main(cnf: Cnf, deps: Deps): {
11
- get: (key: string) => Promise<number>;
11
+ get: (key: string) => Promise<string | null>;
12
12
  set: (key: string, value: string) => Promise<number>;
13
13
  del: (key: string) => Promise<number>;
14
14
  incr: (key: string, step?: number) => Promise<number>;
@@ -5,8 +5,7 @@ function Main(cnf, deps) {
5
5
  const { hash: { key: REDIS_KEY }, } = cnf;
6
6
  const { redis } = deps;
7
7
  const get = async (key) => {
8
- const num = await redis.hget(REDIS_KEY, key);
9
- return Number(num) | 0;
8
+ return redis.hget(REDIS_KEY, key);
10
9
  };
11
10
  const set = (key, value) => redis.hset(REDIS_KEY, key, value);
12
11
  const del = (key) => redis.hdel(REDIS_KEY, key);
@@ -9,6 +9,10 @@ export declare type Client = Socket<DefaultEventsMap, DefaultEventsMap, DefaultE
9
9
  profile?: ReturnType<typeof makeProfile>;
10
10
  inited?: boolean;
11
11
  roomId?: string;
12
+ methods?: Record<string, Function>;
13
+ operator?: any;
14
+ /** 退出房间回调函数 */
15
+ quit?: Function;
12
16
  };
13
17
  declare const makeProfile: (client: Client, type: string | undefined, auth: string | Signature, extra?: Profile["extra"]) => Profile;
14
18
  export declare function BridgeSocket(io: Server, domain: Domain): void;
@@ -105,10 +105,7 @@ function BridgeSocket(io, domain) {
105
105
  }
106
106
  catch (e) {
107
107
  client.inited = false;
108
- if (e instanceof MyError) {
109
- client.emit("internalError", e.message, e.code || "unknown");
110
- return;
111
- }
108
+ client.emit("internalError", e.code || "unknown", e.message, e.data);
112
109
  console.error(e);
113
110
  }
114
111
  });
@@ -116,44 +113,39 @@ function BridgeSocket(io, domain) {
116
113
  try {
117
114
  if (!client.profile || !client.inited)
118
115
  return;
119
- const res = await entrance({ ...client.profile, roomId }, client);
116
+ const ret = await entrance({ ...client.profile, roomId }, client);
120
117
  client.profile.roomId = roomId;
121
118
  client.roomId = roomId;
122
- client.emit("entranced", res);
119
+ Object.assign(client, ret);
120
+ client.emit("entranced", { ...ret, methods: Object.keys(ret.methods) });
123
121
  }
124
122
  catch (e) {
125
- if (e instanceof MyError) {
126
- client.emit("internalError", e.message, e.code || "unknown");
127
- return;
128
- }
123
+ client.emit("internalError", e.code || "unknown", e.message, e.data);
129
124
  console.error(e);
130
125
  }
131
126
  });
132
127
  client.use(async ([name, params, responseId], next) => {
133
128
  if (name === "init" || name === "entrance")
134
129
  return next();
135
- const { method } = domain[name];
130
+ if (!client.methods)
131
+ throw new MyError("没有允许执行的方法", "请先进入房间");
132
+ const method = client.methods[name];
136
133
  try {
137
134
  if (!method)
138
135
  throw new MyError("notFound", "不存在该领域方法");
139
136
  if (!client.profile)
140
137
  throw new MyError("noAuth", "请先执行 init");
141
- const res = await method({ ...client.profile, method: name }, params);
138
+ const res = await method(...params);
142
139
  if (responseId) {
143
140
  client.emit("response", responseId, res);
144
141
  }
145
142
  }
146
143
  catch (e) {
147
- if (e instanceof Error) {
148
- if (responseId) {
149
- client.emit("responseError", responseId, e.code, e.message, e.data);
150
- }
151
- else {
152
- client.emit(`${name}Error`, e.code, e.message, e.data);
153
- }
144
+ if (responseId) {
145
+ client.emit("responseError", responseId, e.code, e.message, e.data);
154
146
  }
155
147
  else {
156
- console.error(e);
148
+ client.emit(`${name}Error`, e.code, e.message, e.data);
157
149
  }
158
150
  }
159
151
  return next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.3.27",
3
+ "version": "0.4.0",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {