@next-core/brick-utils 2.40.0 → 2.41.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.41.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.40.0...@next-core/brick-utils@2.41.0) (2022-09-07)
7
+
8
+
9
+ ### Features
10
+
11
+ * support makeThrottledAggregation ([b44074c](https://github.com/easyops-cn/next-core/commit/b44074ca2d11c080a6e9d64212a81a5a3a154c8c))
12
+
13
+
14
+
15
+
16
+
6
17
  # [2.40.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.39.6...@next-core/brick-utils@2.40.0) (2022-09-06)
7
18
 
8
19
 
@@ -23080,6 +23080,68 @@
23080
23080
  };
23081
23081
  }
23082
23082
 
23083
+ var poolMap = new Map();
23084
+
23085
+ /**
23086
+ * Make a throttled aggregation function.
23087
+ *
23088
+ * Note: the `id` should be a primitive value, typically a string or a number.
23089
+ * If the `id` must be an array or an object, encode it in JSON.
23090
+ *
23091
+ * @param namespace Should be unique for each purpose.
23092
+ * @param request Accept ids, fire request and return the promise.
23093
+ * @param select Select the specific data for a specific id in the aggregated result.
23094
+ * @param wait Throttle wait time in milliseconds, defaults to 100.
23095
+ */
23096
+ function makeThrottledAggregation(namespace, request, select) {
23097
+ var wait = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 100;
23098
+ // Each namespace share a pool.
23099
+ var pool = poolMap.get(namespace);
23100
+
23101
+ if (!pool) {
23102
+ pool = {
23103
+ current: null,
23104
+ fullIds: new Map()
23105
+ };
23106
+ poolMap.set(namespace, pool);
23107
+ }
23108
+
23109
+ function enqueue(id) {
23110
+ var aggregatedIds = new Set();
23111
+ aggregatedIds.add(id);
23112
+ var promise = new Promise((resolve, reject) => {
23113
+ setTimeout(() => {
23114
+ pool.current = null;
23115
+ request([...aggregatedIds]).then(resolve, reject);
23116
+ }, wait);
23117
+ });
23118
+ return {
23119
+ promise,
23120
+ aggregatedIds
23121
+ };
23122
+ }
23123
+
23124
+ return function (id) {
23125
+ var cached = pool.fullIds.get(id);
23126
+
23127
+ if (cached) {
23128
+ return cached.then(r => select(r, id));
23129
+ }
23130
+
23131
+ if (pool.current) {
23132
+ pool.current.aggregatedIds.add(id);
23133
+ } else {
23134
+ pool.current = enqueue(id);
23135
+ }
23136
+
23137
+ var {
23138
+ promise
23139
+ } = pool.current;
23140
+ pool.fullIds.set(id, promise);
23141
+ return promise.then(r => select(r, id));
23142
+ };
23143
+ }
23144
+
23083
23145
  Object.defineProperty(exports, 'pipes', {
23084
23146
  enumerable: true,
23085
23147
  get: function () { return pipes.pipes; }
@@ -23113,6 +23175,7 @@
23113
23175
  exports.isSnippetNode = isSnippetNode;
23114
23176
  exports.lint = lint;
23115
23177
  exports.loadScript = loadScript;
23178
+ exports.makeThrottledAggregation = makeThrottledAggregation;
23116
23179
  exports.mapCustomApisToNameAndNamespace = mapCustomApisToNameAndNamespace;
23117
23180
  exports.matchPath = matchPath;
23118
23181
  exports.normalizeBuilderNode = normalizeBuilderNode;