@jayfong/x-server 1.35.3 → 1.35.5

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.
@@ -53,19 +53,15 @@ function defineSliceTask(options) {
53
53
  const key = (addOptions == null ? void 0 : addOptions.key) || '';
54
54
  const duration = (addOptions == null ? void 0 : addOptions.duration) != null ? (0, _date.ms)(addOptions.duration) : typeof options.duration === 'function' ? (0, _date.ms)(options.duration(key)) : options.duration && (0, _date.ms)(options.duration);
55
55
  const threshold = (addOptions == null ? void 0 : addOptions.threshold) || typeof options.threshold && (typeof options.threshold === 'function' ? options.threshold(key) : options.threshold);
56
+ const thresholdTimeout = (addOptions == null ? void 0 : addOptions.thresholdTimeout) != null ? (0, _date.ms)(addOptions.thresholdTimeout) : typeof options.thresholdTimeout === 'function' ? (0, _date.ms)(options.thresholdTimeout(key)) : options.thresholdTimeout && (0, _date.ms)(options.thresholdTimeout);
56
57
  (0, _assert.default)(duration != null || threshold != null, '参数 threshold 和 duration 必须至少设置 1 个');
57
58
  const redisKey = !key ? redisKeyPrefix : `${redisKeyPrefix}_${key}`;
58
59
  const res = await _x.x.redis.multi([['llen', redisKey], ['lpush', redisKey, JSON.stringify(data)]]).exec();
59
- const count = parseInt(res[0][1], 10) + 1; // 时段与阈值并存
60
+ const count = parseInt(res[0][1], 10) + 1; // 仅时段
61
+ // 1分钟内的合并推送
60
62
 
61
- if (duration != null && threshold != null) {
62
- if (count === threshold) {
63
- await task.add({
64
- key: key,
65
- redisKey: redisKey,
66
- count: threshold
67
- });
68
- } else if (count === 1) {
63
+ if (duration != null) {
64
+ if (count === 1) {
69
65
  await task.add({
70
66
  key: key,
71
67
  redisKey: redisKey
@@ -73,18 +69,21 @@ function defineSliceTask(options) {
73
69
  delay: duration
74
70
  });
75
71
  }
76
- } // 仅时段
77
- else if (duration != null) {
78
- if (count === 1) {
72
+ } // 仅阈值
73
+ // 满10条推送
74
+ else if (threshold != null) {
75
+ if (thresholdTimeout) {
76
+ const delayTaskId = redisKey;
77
+ await task.getJob(delayTaskId).then(job => job == null ? void 0 : job.remove());
79
78
  await task.add({
80
79
  key: key,
81
80
  redisKey: redisKey
82
81
  }, {
83
- delay: duration
82
+ jobId: delayTaskId,
83
+ delay: thresholdTimeout
84
84
  });
85
85
  }
86
- } // 仅阈值
87
- else if (threshold != null) {
86
+
88
87
  if (count === threshold) {
89
88
  await task.add({
90
89
  key: key,
@@ -40,19 +40,15 @@ export function defineSliceTask(options) {
40
40
  const key = (addOptions == null ? void 0 : addOptions.key) || '';
41
41
  const duration = (addOptions == null ? void 0 : addOptions.duration) != null ? ms(addOptions.duration) : typeof options.duration === 'function' ? ms(options.duration(key)) : options.duration && ms(options.duration);
42
42
  const threshold = (addOptions == null ? void 0 : addOptions.threshold) || typeof options.threshold && (typeof options.threshold === 'function' ? options.threshold(key) : options.threshold);
43
+ const thresholdTimeout = (addOptions == null ? void 0 : addOptions.thresholdTimeout) != null ? ms(addOptions.thresholdTimeout) : typeof options.thresholdTimeout === 'function' ? ms(options.thresholdTimeout(key)) : options.thresholdTimeout && ms(options.thresholdTimeout);
43
44
  assert(duration != null || threshold != null, '参数 threshold 和 duration 必须至少设置 1 个');
44
45
  const redisKey = !key ? redisKeyPrefix : `${redisKeyPrefix}_${key}`;
45
46
  const res = await x.redis.multi([['llen', redisKey], ['lpush', redisKey, JSON.stringify(data)]]).exec();
46
- const count = parseInt(res[0][1], 10) + 1; // 时段与阈值并存
47
+ const count = parseInt(res[0][1], 10) + 1; // 仅时段
48
+ // 1分钟内的合并推送
47
49
 
48
- if (duration != null && threshold != null) {
49
- if (count === threshold) {
50
- await task.add({
51
- key: key,
52
- redisKey: redisKey,
53
- count: threshold
54
- });
55
- } else if (count === 1) {
50
+ if (duration != null) {
51
+ if (count === 1) {
56
52
  await task.add({
57
53
  key: key,
58
54
  redisKey: redisKey
@@ -60,18 +56,21 @@ export function defineSliceTask(options) {
60
56
  delay: duration
61
57
  });
62
58
  }
63
- } // 仅时段
64
- else if (duration != null) {
65
- if (count === 1) {
59
+ } // 仅阈值
60
+ // 满10条推送
61
+ else if (threshold != null) {
62
+ if (thresholdTimeout) {
63
+ const delayTaskId = redisKey;
64
+ await task.getJob(delayTaskId).then(job => job == null ? void 0 : job.remove());
66
65
  await task.add({
67
66
  key: key,
68
67
  redisKey: redisKey
69
68
  }, {
70
- delay: duration
69
+ jobId: delayTaskId,
70
+ delay: thresholdTimeout
71
71
  });
72
72
  }
73
- } // 仅阈值
74
- else if (threshold != null) {
73
+
75
74
  if (count === threshold) {
76
75
  await task.add({
77
76
  key: key,
@@ -170,6 +170,10 @@ export declare namespace XTask {
170
170
  * 阈值数量
171
171
  */
172
172
  threshold?: number | ((key: K) => number);
173
+ /**
174
+ * 阈值数量超时时间
175
+ */
176
+ thresholdTimeout?: MsValue | ((key: K) => MsValue);
173
177
  /**
174
178
  * 处理器
175
179
  */
@@ -179,6 +183,7 @@ export declare namespace XTask {
179
183
  add: (data: T, options?: {
180
184
  duration?: MsValue;
181
185
  threshold?: number;
186
+ thresholdTimeout?: MsValue;
182
187
  }) => Promise<void>;
183
188
  }
184
189
  interface SliceTask<T, K> {
@@ -186,6 +191,7 @@ export declare namespace XTask {
186
191
  key: K;
187
192
  duration?: MsValue;
188
193
  threshold?: number;
194
+ thresholdTimeout?: MsValue;
189
195
  }) => Promise<void>;
190
196
  }
191
197
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.35.3",
3
+ "version": "1.35.5",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",