tmc.js 0.3.5 → 0.3.6

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.
package/README.md CHANGED
@@ -43,14 +43,27 @@ new Tmc('your_app_key', 'your_app_secret')
43
43
  | options.onCloseReconnection | `number` | 当消费端断开连接,重试连接间隔(默认`3000`毫秒) |
44
44
  | options.autoParseContentJson | `boolean` | 自动解析推送消息`$.content.content`字段为对象(默认`true`) |
45
45
  | options.autoReplyConfirmation | `boolean` | 以推送的`$.content.id`字段自动`Confirm`消息(默认`true`) |
46
+ | options.autoGroupedEmitting | `boolean` | 以`/^(([^_]+)_[^_]+)_.+/`规则切分`$.content.topic`主题,开关消费端多维监听功能(默认`true`) |
46
47
 
47
48
  **`tmc.on(topic: string, listener: (this: Tmc, message: Message) => void) => Tmc`**
48
49
 
49
50
  注册 `topic` 消息通知处理函数,默认已内置 [消息](./types/message.in.d.ts) 说明。
51
+ 自`v0.3.6`起,默认开启消费端多维监听功能,`topic`字符串也可以是`BU`及`BU_G`单位,例如:
52
+
53
+ ```js
54
+ .on('taobao', console.info)
55
+ .on('alibaba_einvoice', console.info)
56
+ ```
50
57
 
51
58
  **`tmc[<topic>](fn: (this: Tmc, message: Message) => void) => Tmc`**
52
59
 
53
60
  直接以 `topic` 为键值,注册消息通知处理函数。
61
+ 自`v0.3.6`起,默认开启消费端多维监听功能,`topic`字符串也可以是`BU`及`BU_G`单位,例如:
62
+
63
+ ```js
64
+ .taobao(console.info)
65
+ .alibaba_einvoice(console.info)
66
+ ```
54
67
 
55
68
  **`tmc.reconnect(ms: number) => Tmc`**
56
69
 
package/lib/consumer.js CHANGED
@@ -46,6 +46,8 @@ class TaoMessageConsumer extends EventEmitter {
46
46
  onCloseReconnection: 3e3,
47
47
  autoParseContentJson: true,
48
48
  autoReplyConfirmation: true,
49
+ /** @since v0.3.6 */
50
+ autoGroupedEmitting: true,
49
51
  };
50
52
 
51
53
  [kWebSocket];
@@ -129,7 +131,7 @@ class TaoMessageConsumer extends EventEmitter {
129
131
  }
130
132
 
131
133
  onping(data) {
132
- logger.onping('The channel is onping with data [%s].', stringify(data));
134
+ logger.onping('The channel is onping with data [%s].', data);
133
135
  this[kWebSocket]?.pong(data, true);
134
136
  }
135
137
 
@@ -138,8 +140,8 @@ class TaoMessageConsumer extends EventEmitter {
138
140
  this.reconnect(this[kOptions].onErrorReconnection);
139
141
  }
140
142
 
141
- onclose(...arg) {
142
- logger.onclose('The channel is onclose(%s), let us reconnect.', stringify(arg));
143
+ onclose(code, reason) {
144
+ logger.onclose('The channel is onclose(%d: %s), let us reconnect.', code, reason);
143
145
  this.reconnect(this[kOptions].onCloseReconnection);
144
146
  }
145
147
 
@@ -148,7 +150,13 @@ class TaoMessageConsumer extends EventEmitter {
148
150
  }
149
151
 
150
152
  [`process${SEND}`](msg) {
151
- if (msg && msg[CONTENT] && msg[CONTENT][TOPIC]) { this.emit(msg[CONTENT][TOPIC], msg); }
153
+ if (msg && msg[CONTENT] && msg[CONTENT][TOPIC]) {
154
+ [msg[CONTENT][TOPIC]].concat(
155
+ this[kOptions].autoGroupedEmitting
156
+ ? msg[CONTENT][TOPIC].split(/^(([^_]+)_[^_]+)_.+/).slice(1, -1)
157
+ : [],
158
+ ).forEach((t) => this.emit(t, msg));
159
+ }
152
160
 
153
161
  if (this[kOptions].autoReplyConfirmation && msg && msg[CONTENT] && msg[CONTENT][ID]) {
154
162
  const reply = new Message(SEND, Confirm).with(TOKEN, this[kToken]).with(ID, msg[CONTENT][ID]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tmc.js",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "Events driven and chained Taobao Message Channel(TMC) for NodeJS",
5
5
  "author": "James ZHANG",
6
6
  "license": "MIT",