@lemon-fe/kits 1.4.22-alpha.3 → 1.4.22-alpha.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.
@@ -2,13 +2,16 @@ import React, { type ReactNode } from 'react';
2
2
  import type { Locale } from '../locale/locale';
3
3
  import { message, Modal } from '@lemon-fe/components';
4
4
  import { type HooksOpts } from '@lemon-fe/hooks';
5
+ /** 为了性能考虑所有配置都只使用初始值,防止上下文频繁更新导致渲染问题 */
5
6
  declare type AppConfigProps = {
6
7
  children?: ReactNode | null;
7
8
  locale?: Locale;
8
9
  hooksConfig?: Partial<HooksOpts>;
10
+ batchOperatorConfig?: BatchOperatorConfig;
9
11
  };
10
12
  declare type BatchOperatorConfig = {
11
13
  openError?: (err: Error) => void;
14
+ concurrency?: number;
12
15
  };
13
16
  export declare const AppContext: React.Context<{
14
17
  message: ReturnType<typeof message.useMessage>[0];
@@ -17,6 +20,10 @@ export declare const AppContext: React.Context<{
17
20
  }>;
18
21
  declare function AppProvider(props: AppConfigProps): JSX.Element;
19
22
  declare namespace AppProvider {
20
- var useApp;
23
+ var useApp: () => {
24
+ message: import("antd/lib/message").MessageInstance;
25
+ modal: Omit<import("antd/lib/modal/confirm").ModalStaticFunctions, "warn">;
26
+ batchOperatorConfig?: BatchOperatorConfig | undefined;
27
+ };
21
28
  }
22
29
  export default AppProvider;
@@ -14,6 +14,8 @@ import React, { createContext, useMemo, useContext, useCallback } from 'react';
14
14
  import { PageLoading, message, Modal, ConfigProvider, Result, Button, RequestErrorMessage } from '@lemon-fe/components';
15
15
  import { HooksConfigProvider, useLatestRef } from '@lemon-fe/hooks';
16
16
  import { isRequestError } from '@lemon-fe/utils';
17
+
18
+ /** 为了性能考虑所有配置都只使用初始值,防止上下文频繁更新导致渲染问题 */
17
19
  import { jsx as _jsx } from "react/jsx-runtime";
18
20
  import { jsxs as _jsxs } from "react/jsx-runtime";
19
21
  export var AppContext = /*#__PURE__*/createContext({
@@ -60,9 +62,9 @@ export default function AppProvider(props) {
60
62
  }
61
63
  }, []);
62
64
  var batchOperatorConfig = useMemo(function () {
63
- var result = {
65
+ var result = _objectSpread({
64
66
  openError: _showError
65
- };
67
+ }, props.batchOperatorConfig);
66
68
  return result;
67
69
  }, []);
68
70
  var appContext = useMemo(function () {
@@ -75,7 +77,7 @@ export default function AppProvider(props) {
75
77
  },
76
78
  batchOperatorConfig: batchOperatorConfig
77
79
  };
78
- }, [batchOperatorConfig]);
80
+ }, []);
79
81
  var hooksConfig = useMemo(function () {
80
82
  var result = _objectSpread({
81
83
  showToast: function showToast() {
@@ -89,6 +89,11 @@ interface Props<RecordType extends Record<string, any> = Record<string, any>, Re
89
89
  * @description 显示完整信息,可点击
90
90
  */
91
91
  openError?: (err: Error) => void;
92
+ /**
93
+ * @default 1
94
+ * @description 并发数
95
+ */
96
+ concurrency?: number;
92
97
  }
93
98
  export default function useBatchOperate<RecordType extends Record<string, any> = Record<string, any>, ResultType = any, ParamsType = any>(options: Props<RecordType, ResultType, ParamsType>): {
94
99
  run: (rows: RecordType[], otherOpts?: Partial<Props<RecordType, ResultType, ParamsType>>) => void;
@@ -36,6 +36,7 @@ import { ConfigProvider, Modal, Row, Spin, Typography } from '@lemon-fe/componen
36
36
  import { Actions, ComponentConfigureContext, DataGrid, Icons, Layout, Section } from '@lemon-fe/components';
37
37
  import { Subject } from '@lemon-fe/utils';
38
38
  import { get } from 'lodash';
39
+ import PQueue from 'p-queue';
39
40
  import { jsx as _jsx } from "react/jsx-runtime";
40
41
  import { jsxs as _jsxs } from "react/jsx-runtime";
41
42
  var Text = Typography.Text;
@@ -51,6 +52,7 @@ var BatchOperate = /*#__PURE__*/function (_Component) {
51
52
  _defineProperty(_assertThisInitialized(_this), "resultsSubject", new Subject());
52
53
  _defineProperty(_assertThisInitialized(_this), "mounted", true);
53
54
  _defineProperty(_assertThisInitialized(_this), "locale", defaultLocale);
55
+ _defineProperty(_assertThisInitialized(_this), "queue", void 0);
54
56
  _defineProperty(_assertThisInitialized(_this), "renderIconTip", function (type, err) {
55
57
  if (!(err instanceof Error)) {
56
58
  return /*#__PURE__*/_jsx(Icons.Tip, {
@@ -141,23 +143,6 @@ var BatchOperate = /*#__PURE__*/function (_Component) {
141
143
  }
142
144
  }, _callee);
143
145
  })));
144
- _defineProperty(_assertThisInitialized(_this), "handleStart", function () {
145
- var _this$state = _this.state,
146
- active = _this$state.active,
147
- data = _this$state.data,
148
- paused = _this$state.paused;
149
- if (!paused) {
150
- return;
151
- }
152
- if (active) {
153
- var idx = data.findIndex(function (item) {
154
- return _this.getRowKey(item) === active;
155
- });
156
- _this.run(idx + 1);
157
- } else {
158
- _this.run(0);
159
- }
160
- });
161
146
  if ((_props$locale = props.locale) !== null && _props$locale !== void 0 && _props$locale.UseBatchOperator) {
162
147
  _this.locale = props.locale.UseBatchOperator;
163
148
  }
@@ -170,22 +155,41 @@ var BatchOperate = /*#__PURE__*/function (_Component) {
170
155
  process: _this.getProcess()
171
156
  };
172
157
  _this.resultsSubject.next(_this.state.results);
158
+ _this.queue = new PQueue({
159
+ concurrency: props.concurrency || 1,
160
+ autoStart: false
161
+ });
162
+ _this.queue.on('idle', function () {
163
+ // finish
164
+ _this.setState({
165
+ paused: true
166
+ });
167
+ });
173
168
  return _this;
174
169
  }
175
170
  _createClass(BatchOperate, [{
176
171
  key: "componentWillUnmount",
177
172
  value: function componentWillUnmount() {
178
173
  this.mounted = false;
174
+ this.queue.pause();
175
+ }
176
+ }, {
177
+ key: "componentDidUpdate",
178
+ value: function componentDidUpdate(prevProps, prevState) {
179
+ if (this.state.results !== prevState.results) {
180
+ this.resultsSubject.next(this.state.results);
181
+ }
179
182
  }
180
183
  }, {
181
184
  key: "changeResult",
182
185
  value: function changeResult(key, val) {
183
- var newResults = new Map(this.state.results);
184
- newResults.set(key, val);
185
- this.setState({
186
- results: newResults
186
+ this.setState(function (prev) {
187
+ var newResults = new Map(prev.results);
188
+ newResults.set(key, val);
189
+ return {
190
+ results: newResults
191
+ };
187
192
  });
188
- this.resultsSubject.next(newResults);
189
193
  }
190
194
  }, {
191
195
  key: "deleteResult",
@@ -381,33 +385,18 @@ var BatchOperate = /*#__PURE__*/function (_Component) {
381
385
  }()
382
386
  }, {
383
387
  key: "run",
384
- value: function run(idx) {
388
+ value: function run(record) {
385
389
  var _this3 = this;
386
- var data = this.state.data;
387
- if (idx >= data.length) {
388
- this.setState({
389
- paused: true,
390
- active: undefined
391
- });
392
- return;
393
- }
394
- this.setState({
395
- paused: false
396
- });
397
390
  var params = this.props.params;
398
- var record = data[idx];
399
391
  var id = this.getRowKey(record);
400
- this.setState({
401
- active: id
402
- });
403
392
  var grid = this.grid.current;
404
393
  if (grid && grid.ready()) {
405
394
  grid.api.ensureNodeVisible(record);
406
395
  }
407
- this.processNode(id, 0, params, function () {
408
- if (_this3.mounted && !_this3.state.paused) {
409
- _this3.run(idx + 1);
410
- }
396
+ return new Promise(function (res) {
397
+ _this3.processNode(id, 0, params, function () {
398
+ res();
399
+ });
411
400
  });
412
401
  }
413
402
  }, {
@@ -632,21 +621,19 @@ var BatchOperate = /*#__PURE__*/function (_Component) {
632
621
  }, {
633
622
  key: "render",
634
623
  value: function render() {
635
- var _this6 = this,
636
- _results$get;
624
+ var _this6 = this;
637
625
  var _this$props4 = this.props,
638
626
  modalTitle = _this$props4.modalTitle,
639
627
  _afterClose = _this$props4.afterClose,
640
628
  rowKey = _this$props4.rowKey,
641
629
  _this$props4$footer = _this$props4.footer,
642
630
  footer = _this$props4$footer === void 0 ? null : _this$props4$footer;
643
- var _this$state2 = this.state,
644
- results = _this$state2.results,
645
- open = _this$state2.open,
646
- data = _this$state2.data,
647
- active = _this$state2.active,
648
- cols = _this$state2.cols,
649
- paused = _this$state2.paused;
631
+ var _this$state = this.state,
632
+ results = _this$state.results,
633
+ open = _this$state.open,
634
+ data = _this$state.data,
635
+ cols = _this$state.cols,
636
+ paused = _this$state.paused;
650
637
  var _this$locale3 = this.locale,
651
638
  detailsText = _this$locale3.detailsText,
652
639
  continueText = _this$locale3.continueText,
@@ -675,9 +662,9 @@ var BatchOperate = /*#__PURE__*/function (_Component) {
675
662
  _this6.setState({
676
663
  results: new Map(),
677
664
  paused: true,
678
- active: undefined,
679
665
  data: []
680
666
  });
667
+ _this6.queue.pause();
681
668
  _afterClose();
682
669
  },
683
670
  keyboard: false,
@@ -689,13 +676,26 @@ var BatchOperate = /*#__PURE__*/function (_Component) {
689
676
  children: "".concat(detailsText).concat(results.size > 0 ? "(".concat(results.size, "/").concat(data.length, ")") : '')
690
677
  }), /*#__PURE__*/_jsx(Actions, {
691
678
  actions: [paused ? {
692
- disabled: data.length <= 0 || results.size >= data.length || active !== undefined && ((_results$get = results.get(active)) === null || _results$get === void 0 ? void 0 : _results$get.loading),
693
- text: active ? continueText : startExecText,
694
- onClick: this.handleStart
679
+ disabled: data.length <= 0 || results.size >= data.length,
680
+ text: this.queue.size > 0 ? continueText : startExecText,
681
+ onClick: function onClick() {
682
+ if (_this6.queue.size === 0) {
683
+ _this6.queue.addAll(_this6.state.data.map(function (row) {
684
+ return function () {
685
+ return _this6.run(row);
686
+ };
687
+ }));
688
+ }
689
+ _this6.queue.start();
690
+ _this6.setState({
691
+ paused: false
692
+ });
693
+ }
695
694
  } : {
696
695
  disabled: data.length <= 0,
697
696
  text: suspendExecText,
698
697
  onClick: function onClick() {
698
+ _this6.queue.pause();
699
699
  _this6.setState({
700
700
  paused: true
701
701
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lemon-fe/kits",
3
- "version": "1.4.22-alpha.3",
3
+ "version": "1.4.22-alpha.5",
4
4
  "description": "> TODO: description",
5
5
  "homepage": "",
6
6
  "license": "ISC",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@ant-design/icons": "^4.7.0",
27
- "@lemon-fe/components": "^1.4.22-alpha.3",
27
+ "@lemon-fe/components": "^1.4.22-alpha.5",
28
28
  "@lemon-fe/hooks": "^1.4.15",
29
29
  "@lemon-fe/utils": "^1.4.15",
30
30
  "ag-grid-community": "29.2.0",
@@ -32,6 +32,7 @@
32
32
  "classnames": "^2.2.6",
33
33
  "lodash": "^4.17.0",
34
34
  "moment": "^2.29.4",
35
+ "p-queue": "^6.6.2",
35
36
  "react-error-boundary": "^4.0.11"
36
37
  },
37
38
  "peerDependencies": {
@@ -46,5 +47,5 @@
46
47
  "publishConfig": {
47
48
  "registry": "https://registry.npmjs.org"
48
49
  },
49
- "gitHead": "8689b1f8504006653c00dbb88c37b62e455a8148"
50
+ "gitHead": "0c4dc48dacd31fd903bbcbad684f4da20b51b543"
50
51
  }