@jayfong/x-server 1.35.4 → 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,33 +53,14 @@ 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
- // 满10条推送,若超过1分钟仍未满10条,则亦推送
61
-
62
- if (duration != null && threshold != null) {
63
- const delayTaskId = redisKey;
64
- await task.getJob(delayTaskId).then(job => job == null ? void 0 : job.remove());
65
- await task.add({
66
- key: key,
67
- redisKey: redisKey
68
- }, {
69
- jobId: delayTaskId,
70
- delay: duration
71
- });
72
-
73
- if (count === threshold) {
74
- await task.add({
75
- key: key,
76
- redisKey: redisKey,
77
- count: threshold
78
- });
79
- }
80
- } // 仅时段
60
+ const count = parseInt(res[0][1], 10) + 1; // 仅时段
81
61
  // 1分钟内的合并推送
82
- else if (duration != null) {
62
+
63
+ if (duration != null) {
83
64
  if (count === 1) {
84
65
  await task.add({
85
66
  key: key,
@@ -91,6 +72,18 @@ function defineSliceTask(options) {
91
72
  } // 仅阈值
92
73
  // 满10条推送
93
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());
78
+ await task.add({
79
+ key: key,
80
+ redisKey: redisKey
81
+ }, {
82
+ jobId: delayTaskId,
83
+ delay: thresholdTimeout
84
+ });
85
+ }
86
+
94
87
  if (count === threshold) {
95
88
  await task.add({
96
89
  key: key,
@@ -40,33 +40,14 @@ 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
- // 满10条推送,若超过1分钟仍未满10条,则亦推送
48
-
49
- if (duration != null && threshold != null) {
50
- const delayTaskId = redisKey;
51
- await task.getJob(delayTaskId).then(job => job == null ? void 0 : job.remove());
52
- await task.add({
53
- key: key,
54
- redisKey: redisKey
55
- }, {
56
- jobId: delayTaskId,
57
- delay: duration
58
- });
59
-
60
- if (count === threshold) {
61
- await task.add({
62
- key: key,
63
- redisKey: redisKey,
64
- count: threshold
65
- });
66
- }
67
- } // 仅时段
47
+ const count = parseInt(res[0][1], 10) + 1; // 仅时段
68
48
  // 1分钟内的合并推送
69
- else if (duration != null) {
49
+
50
+ if (duration != null) {
70
51
  if (count === 1) {
71
52
  await task.add({
72
53
  key: key,
@@ -78,6 +59,18 @@ export function defineSliceTask(options) {
78
59
  } // 仅阈值
79
60
  // 满10条推送
80
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());
65
+ await task.add({
66
+ key: key,
67
+ redisKey: redisKey
68
+ }, {
69
+ jobId: delayTaskId,
70
+ delay: thresholdTimeout
71
+ });
72
+ }
73
+
81
74
  if (count === threshold) {
82
75
  await task.add({
83
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.4",
3
+ "version": "1.35.5",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",