@ibiz-template/core 0.0.1-alpha.6 → 0.0.1-beta.130

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.
Files changed (141) hide show
  1. package/dist/system/index.system.js +1 -0
  2. package/out/constant/core/core.d.ts +9 -0
  3. package/out/constant/core/core.d.ts.map +1 -1
  4. package/out/constant/core/core.js +9 -0
  5. package/out/constant/index.d.ts +3 -0
  6. package/out/constant/index.d.ts.map +1 -1
  7. package/out/constant/index.js +3 -0
  8. package/out/constant/login-mode/login-mode.d.ts +23 -0
  9. package/out/constant/login-mode/login-mode.d.ts.map +1 -0
  10. package/out/constant/login-mode/login-mode.js +24 -0
  11. package/out/constant/menu-permission-mode/menu-permission-mode.d.ts +23 -0
  12. package/out/constant/menu-permission-mode/menu-permission-mode.d.ts.map +1 -0
  13. package/out/constant/menu-permission-mode/menu-permission-mode.js +24 -0
  14. package/out/constant/util/util.d.ts +5 -0
  15. package/out/constant/util/util.d.ts.map +1 -0
  16. package/out/constant/util/util.js +5 -0
  17. package/out/context/index.d.ts +49 -27
  18. package/out/context/index.d.ts.map +1 -1
  19. package/out/context/index.js +94 -55
  20. package/out/environment/environment.d.ts.map +1 -1
  21. package/out/environment/environment.js +16 -2
  22. package/out/error/http-error/http-error.d.ts.map +1 -1
  23. package/out/error/http-error/http-error.js +0 -4
  24. package/out/factory-center.d.ts +11 -0
  25. package/out/factory-center.d.ts.map +1 -0
  26. package/out/factory-center.js +10 -0
  27. package/out/ibizsys.d.ts +8 -1
  28. package/out/ibizsys.d.ts.map +1 -1
  29. package/out/ibizsys.js +8 -1
  30. package/out/index.d.ts +2 -1
  31. package/out/index.d.ts.map +1 -1
  32. package/out/index.js +2 -1
  33. package/out/install.d.ts.map +1 -1
  34. package/out/install.js +4 -2
  35. package/out/interface/click-outside/click-outside.d.ts +70 -0
  36. package/out/interface/click-outside/click-outside.d.ts.map +1 -0
  37. package/out/interface/click-outside/click-outside.js +1 -0
  38. package/out/interface/i-environment/i-environment.d.ts +108 -4
  39. package/out/interface/i-environment/i-environment.d.ts.map +1 -1
  40. package/out/interface/index.d.ts +1 -0
  41. package/out/interface/index.d.ts.map +1 -1
  42. package/out/interface/index.js +1 -1
  43. package/out/types.d.ts +41 -0
  44. package/out/types.d.ts.map +1 -1
  45. package/out/utils/click-outside/click-outside.d.ts +14 -0
  46. package/out/utils/click-outside/click-outside.d.ts.map +1 -0
  47. package/out/utils/click-outside/click-outside.js +85 -0
  48. package/out/utils/color/color.d.ts +13 -0
  49. package/out/utils/color/color.d.ts.map +1 -0
  50. package/out/utils/color/color.js +76 -0
  51. package/out/utils/download-file/download-file.d.ts +31 -0
  52. package/out/utils/download-file/download-file.d.ts.map +1 -0
  53. package/out/utils/download-file/download-file.js +107 -0
  54. package/out/utils/event/event.d.ts +34 -0
  55. package/out/utils/event/event.d.ts.map +1 -0
  56. package/out/utils/event/event.js +60 -0
  57. package/out/utils/index.d.ts +11 -1
  58. package/out/utils/index.d.ts.map +1 -1
  59. package/out/utils/index.js +11 -1
  60. package/out/utils/interceptor/core-interceptor.d.ts +15 -0
  61. package/out/utils/interceptor/core-interceptor.d.ts.map +1 -0
  62. package/out/utils/interceptor/core-interceptor.js +30 -0
  63. package/out/utils/interceptor/index.d.ts +3 -0
  64. package/out/utils/interceptor/index.d.ts.map +1 -0
  65. package/out/utils/interceptor/index.js +2 -0
  66. package/out/utils/interceptor/interceptor.d.ts +74 -3
  67. package/out/utils/interceptor/interceptor.d.ts.map +1 -1
  68. package/out/utils/interceptor/interceptor.js +73 -22
  69. package/out/utils/logger/logger.d.ts +3 -0
  70. package/out/utils/logger/logger.d.ts.map +1 -0
  71. package/out/utils/logger/logger.js +2 -0
  72. package/out/utils/net/http-response.d.ts +4 -9
  73. package/out/utils/net/http-response.d.ts.map +1 -1
  74. package/out/utils/net/http-response.js +0 -7
  75. package/out/utils/net/net.d.ts +98 -30
  76. package/out/utils/net/net.d.ts.map +1 -1
  77. package/out/utils/net/net.js +133 -38
  78. package/out/utils/plural/plural.d.ts.map +1 -1
  79. package/out/utils/plural/plural.js +2 -0
  80. package/out/utils/string-util/string-util.d.ts +46 -0
  81. package/out/utils/string-util/string-util.d.ts.map +1 -0
  82. package/out/utils/string-util/string-util.js +71 -0
  83. package/out/utils/sync/await-timeout.d.ts +14 -0
  84. package/out/utils/sync/await-timeout.d.ts.map +1 -0
  85. package/out/utils/sync/await-timeout.js +23 -0
  86. package/out/utils/sync/count-latch.d.ts +60 -0
  87. package/out/utils/sync/count-latch.d.ts.map +1 -0
  88. package/out/utils/sync/count-latch.js +90 -0
  89. package/out/utils/sync/index.d.ts +3 -0
  90. package/out/utils/sync/index.d.ts.map +1 -0
  91. package/out/utils/sync/index.js +2 -0
  92. package/out/utils/upload/select-file.d.ts +53 -0
  93. package/out/utils/upload/select-file.d.ts.map +1 -0
  94. package/out/utils/upload/select-file.js +47 -0
  95. package/out/utils/upload/upload-file.d.ts +46 -0
  96. package/out/utils/upload/upload-file.d.ts.map +1 -0
  97. package/out/utils/upload/upload-file.js +150 -0
  98. package/out/utils/url-helper/url-helper.d.ts +51 -0
  99. package/out/utils/url-helper/url-helper.d.ts.map +1 -0
  100. package/out/utils/url-helper/url-helper.js +59 -0
  101. package/out/utils/util/util.d.ts +20 -4
  102. package/out/utils/util/util.d.ts.map +1 -1
  103. package/out/utils/util/util.js +59 -5
  104. package/package.json +21 -10
  105. package/src/constant/core/core.ts +10 -0
  106. package/src/constant/index.ts +3 -0
  107. package/src/constant/login-mode/login-mode.ts +23 -0
  108. package/src/constant/menu-permission-mode/menu-permission-mode.ts +23 -0
  109. package/src/constant/util/util.ts +5 -0
  110. package/src/context/index.ts +127 -51
  111. package/src/environment/environment.ts +16 -2
  112. package/src/error/http-error/http-error.ts +0 -4
  113. package/src/factory-center.ts +9 -0
  114. package/src/ibizsys.ts +9 -1
  115. package/src/index.ts +2 -1
  116. package/src/install.ts +4 -2
  117. package/src/interface/click-outside/click-outside.ts +71 -0
  118. package/src/interface/i-environment/i-environment.ts +122 -4
  119. package/src/interface/index.ts +1 -0
  120. package/src/types.ts +45 -0
  121. package/src/utils/click-outside/click-outside.ts +110 -0
  122. package/src/utils/color/color.ts +84 -0
  123. package/src/utils/download-file/download-file.ts +109 -0
  124. package/src/utils/event/event.ts +77 -0
  125. package/src/utils/index.ts +11 -1
  126. package/src/utils/interceptor/core-interceptor.ts +35 -0
  127. package/src/utils/interceptor/index.ts +2 -0
  128. package/src/utils/interceptor/interceptor.ts +109 -22
  129. package/src/utils/logger/logger.ts +3 -0
  130. package/src/utils/net/http-response.ts +6 -9
  131. package/src/utils/net/net.ts +171 -50
  132. package/src/utils/plural/plural.ts +3 -0
  133. package/src/utils/string-util/string-util.ts +75 -0
  134. package/src/utils/sync/await-timeout.ts +27 -0
  135. package/src/utils/sync/count-latch.ts +96 -0
  136. package/src/utils/sync/index.ts +2 -0
  137. package/src/utils/upload/select-file.ts +86 -0
  138. package/src/utils/upload/upload-file.ts +206 -0
  139. package/src/utils/url-helper/url-helper.ts +62 -0
  140. package/src/utils/util/util.ts +72 -14
  141. package/LICENSE +0 -21
@@ -0,0 +1,96 @@
1
+ import { RuntimeError } from '../../error';
2
+
3
+ /**
4
+ * 计数插销工具类
5
+ *
6
+ * @author lxm
7
+ * @date 2022-11-24 19:11:49
8
+ * @export
9
+ * @class CountLatch
10
+ */
11
+ export class CountLatch {
12
+ private promise: Promise<void> | null = null;
13
+
14
+ private resolve: ((value: void) => void) | null = null;
15
+
16
+ /**
17
+ * 计数,当前等待的异步逻辑个数
18
+ *
19
+ * @author lxm
20
+ * @date 2022-11-24 19:11:59
21
+ * @type {number}
22
+ */
23
+ count: number = 0;
24
+
25
+ /**
26
+ * 开启promise
27
+ *
28
+ * @author lxm
29
+ * @date 2022-11-24 19:11:32
30
+ * @private
31
+ */
32
+ private startPromise() {
33
+ this.promise = new Promise(resolve => {
34
+ this.resolve = resolve;
35
+ });
36
+ }
37
+
38
+ /**
39
+ * 结束promise
40
+ *
41
+ * @author lxm
42
+ * @date 2022-11-24 19:11:44
43
+ * @private
44
+ */
45
+ private endPromise() {
46
+ if (this.resolve) {
47
+ this.resolve();
48
+ this.resolve = null;
49
+ this.promise = null;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * 上锁,计数加一
55
+ * 第一次计数,开启异步
56
+ *
57
+ * @author lxm
58
+ * @date 2022-11-24 19:11:27
59
+ */
60
+ lock() {
61
+ this.count += 1;
62
+ if (!this.promise) {
63
+ this.startPromise();
64
+ }
65
+ }
66
+
67
+ /**
68
+ * 解锁,计数减一
69
+ * 归零时结束异步
70
+ *
71
+ * @author lxm
72
+ * @date 2022-11-24 19:11:47
73
+ */
74
+ unlock() {
75
+ if (this.count < 1) {
76
+ throw new RuntimeError('lock和unlock次数不匹配!');
77
+ }
78
+ this.count -= 1;
79
+ if (this.count === 0) {
80
+ debugger;
81
+ this.endPromise();
82
+ }
83
+ }
84
+
85
+ /**
86
+ * 等待,计数归零异步结束
87
+ *
88
+ * @author lxm
89
+ * @date 2022-11-24 19:11:20
90
+ */
91
+ async await() {
92
+ if (this.promise) {
93
+ await this.promise;
94
+ }
95
+ }
96
+ }
@@ -0,0 +1,2 @@
1
+ export { awaitTimeout } from './await-timeout';
2
+ export { CountLatch } from './count-latch';
@@ -0,0 +1,86 @@
1
+ import { merge } from 'lodash-es';
2
+
3
+ /**
4
+ * 文件List转数组
5
+ *
6
+ * @author lxm
7
+ * @date 2022-11-18 13:11:03
8
+ * @export
9
+ * @param {FileList} fileList
10
+ * @returns {*}
11
+ */
12
+ export function fileListToArr(fileList: FileList): File[] {
13
+ const files = [];
14
+ for (let i = 0; i < fileList.length; i++) {
15
+ files.push(fileList[i]);
16
+ }
17
+ return files;
18
+ }
19
+
20
+ /**
21
+ * JS打开文件上传操作配置参数
22
+ *
23
+ * @author lxm
24
+ * @date 2022-11-20 21:11:47
25
+ * @export
26
+ * @interface SelectFileOpts
27
+ */
28
+ export interface SelectFileOpts {
29
+ /**
30
+ * 接受的文件类型
31
+ *
32
+ * @author lxm
33
+ * @date 2022-11-20 21:11:52
34
+ * @type {string}
35
+ */
36
+ accept?: string;
37
+ /**
38
+ * 是否支持多选
39
+ *
40
+ * @author lxm
41
+ * @date 2022-11-20 21:11:52
42
+ * @type {boolean}
43
+ */
44
+ multiple?: boolean;
45
+ /**
46
+ * 选中文件后回调
47
+ *
48
+ * @author lxm
49
+ * @date 2022-11-20 21:11:50
50
+ */
51
+ onSelected: (_fileList: File[]) => void;
52
+ }
53
+
54
+ /**
55
+ * JS打开文件上传操作
56
+ *
57
+ * @author lxm
58
+ * @date 2022-11-20 21:11:31
59
+ * @export
60
+ * @param {SelectFileOpts} _opts 配置参数
61
+ */
62
+ export function selectFile(_opts: SelectFileOpts) {
63
+ const opts: Required<SelectFileOpts> = merge(
64
+ {
65
+ multiple: true,
66
+ accept: '',
67
+ },
68
+ _opts,
69
+ );
70
+ const input = document.createElement('input');
71
+ input.setAttribute('type', 'file');
72
+ input.setAttribute('multiple', `${opts.multiple}`);
73
+ input.setAttribute('accept', opts.accept);
74
+ input.onchange = e => {
75
+ const inputEl = e.target as HTMLInputElement;
76
+ const files = inputEl.files ? fileListToArr(inputEl.files) : [];
77
+ if (files.length === 0) {
78
+ return;
79
+ }
80
+ opts.onSelected(files);
81
+ inputEl.value = ''; // 如果不置空,相同的文件不会触发change事件
82
+ };
83
+ document.body.appendChild(input);
84
+ input.click();
85
+ document.body.removeChild(input);
86
+ }
@@ -0,0 +1,206 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-function */
2
+ /* eslint-disable no-param-reassign */
3
+ import { cloneDeep, isFunction, merge, round, uniqueId } from 'lodash-es';
4
+ import { HttpResponse } from '../net/http-response';
5
+ import { selectFile } from './select-file';
6
+
7
+ export interface IUploadFile {
8
+ name: string;
9
+ uid: string;
10
+ status: 'uploading' | 'finished' | 'fail' | 'cancel';
11
+ percentage: number;
12
+ response?: HttpResponse;
13
+ error?: unknown;
14
+ }
15
+
16
+ export interface IUploadFileOpts {
17
+ uploadUrl: string;
18
+ /**
19
+ * 接受的文件类型
20
+ *
21
+ * @author lxm
22
+ * @date 2022-11-20 21:11:52
23
+ * @type {string}
24
+ */
25
+ accept?: string;
26
+ /**
27
+ * 是否支持多选
28
+ *
29
+ * @author lxm
30
+ * @date 2022-11-20 21:11:52
31
+ * @type {Boolean}
32
+ */
33
+ multiple?: boolean;
34
+ separate?: string;
35
+ request?: (_files: File[]) => Promise<HttpResponse>;
36
+ beforeUpload?: (_fileData: File[], _files: IUploadFile[]) => boolean;
37
+ finish?: (_resultFiles: IUploadFile[]) => void;
38
+ success?: (_resultFiles: IUploadFile[], _res: HttpResponse) => void;
39
+ error?: (_resultFiles: IUploadFile[], _error: unknown) => void;
40
+ progress?: (_files: IUploadFile[]) => void;
41
+ }
42
+
43
+ /**
44
+ * 使用上传文件逻辑
45
+ *
46
+ * @author lxm
47
+ * @date 2022-11-20 21:11:52
48
+ * @export
49
+ * @param {IUploadFileOpts} _opts
50
+ * @returns {*}
51
+ */
52
+ export function uploadFile(_opts: IUploadFileOpts) {
53
+ const opts: Required<IUploadFileOpts> = merge(
54
+ {
55
+ multiple: true,
56
+ accept: '',
57
+ separate: true,
58
+ beforeUpload: (_fileData: File[], _files: IUploadFile[]) => true,
59
+ finish: (_resultFiles: IUploadFile[]) => {},
60
+ success: (_resultFiles: IUploadFile[], _res: HttpResponse) => {},
61
+ error: (_resultFiles: IUploadFile[], _error: unknown) => {},
62
+ progress: (_files: IUploadFile[]) => {},
63
+ },
64
+ _opts,
65
+ );
66
+
67
+ /**
68
+ * 进度条回调
69
+ *
70
+ * @author lxm
71
+ * @date 2022-11-20 21:11:47
72
+ * @param {IParams} event
73
+ * @param {IUploadFile[]} files
74
+ */
75
+ const onUploadProgress = (event: IParams, files: IUploadFile[]) => {
76
+ files.forEach(file => {
77
+ file.percentage = round(event.progress * 100);
78
+ });
79
+ opts.progress(cloneDeep(files));
80
+ };
81
+
82
+ /**
83
+ * 上传请求方法,返回响应对象
84
+ *
85
+ * @author lxm
86
+ * @date 2022-11-18 13:11:57
87
+ * @param {File[]} files
88
+ * @returns {*} {Promise<HttpResponse>}
89
+ */
90
+ const uploadRequest = async (
91
+ files: File[],
92
+ onProgress: (_event: IParams) => void,
93
+ ): Promise<HttpResponse> => {
94
+ // 自定义上传请求
95
+ if (opts.request && isFunction(opts.request)) {
96
+ return opts.request(files);
97
+ }
98
+
99
+ // 默认每次单个文件上传,可能存在接口一次上传多个文件,
100
+ const data = new FormData();
101
+ files.forEach(file => {
102
+ data.append('file', file);
103
+ });
104
+ const res = await ibiz.net.request(opts.uploadUrl, {
105
+ method: 'post',
106
+ baseURL: '',
107
+ data,
108
+ headers: { 'Content-Type': 'multipart/form-data' },
109
+ onUploadProgress: onProgress,
110
+ });
111
+ return res;
112
+ };
113
+
114
+ /**
115
+ * 执行一次上传,可能是一个文件也可能是多个文件
116
+ *
117
+ * @author lxm
118
+ * @date 2022-11-18 14:11:54
119
+ * @param {File[]} files 此次上传文件的集合
120
+ * @returns {*} {Promise<IUploadFile[]>}
121
+ */
122
+ const executeSingleUpload = async (files: File[]): Promise<IUploadFile[]> => {
123
+ const resultFiles: IUploadFile[] = files.map(file => {
124
+ return {
125
+ status: 'uploading',
126
+ name: file.name,
127
+ uid: uniqueId(),
128
+ percentage: 0,
129
+ };
130
+ });
131
+
132
+ // 上传前回调,可以取消此次下载
133
+ const pass = opts.beforeUpload(files, resultFiles);
134
+ if (!pass) {
135
+ resultFiles.forEach(file => {
136
+ file.status = 'cancel';
137
+ });
138
+ ibiz.log.debug('取消上传', resultFiles);
139
+ return resultFiles;
140
+ }
141
+
142
+ try {
143
+ const res = await uploadRequest(files, event => {
144
+ onUploadProgress(event, resultFiles);
145
+ });
146
+ resultFiles.forEach(file => {
147
+ file.status = 'finished';
148
+ });
149
+ // 上传成功事件
150
+ opts.success(resultFiles, res);
151
+ resultFiles.forEach(file => {
152
+ file.response = res;
153
+ });
154
+ } catch (error) {
155
+ resultFiles.forEach(file => {
156
+ file.status = 'fail';
157
+ });
158
+ // 上传失败事件
159
+ opts.error(resultFiles, error);
160
+ resultFiles.forEach(file => {
161
+ file.error = error;
162
+ });
163
+ ibiz.log.error(error);
164
+ ibiz.log.error(`${files.map(file => file.name).join(',')}上传失败`);
165
+ }
166
+ return resultFiles;
167
+ };
168
+
169
+ /**
170
+ * 执行上传文件逻辑
171
+ *
172
+ * @author lxm
173
+ * @date 2022-11-18 13:11:21
174
+ * @param {files} File[] 文件集合
175
+ */
176
+ const uploadFiles = async (files: File[]) => {
177
+ const uploadSequence = opts.separate ? files.map(file => [file]) : [files];
178
+ const res = await Promise.allSettled(
179
+ uploadSequence.map(async sequence => {
180
+ return executeSingleUpload(sequence);
181
+ }),
182
+ );
183
+
184
+ // 整合所有的返回文件
185
+ const resultFiles: IUploadFile[] = [];
186
+ res.forEach(result => {
187
+ if (result.status === 'fulfilled') {
188
+ resultFiles.push(...result.value);
189
+ }
190
+ });
191
+
192
+ opts.finish(resultFiles);
193
+ };
194
+
195
+ const select = () => {
196
+ selectFile({
197
+ accept: opts.accept,
198
+ multiple: opts.multiple,
199
+ onSelected: files => {
200
+ uploadFiles(files);
201
+ },
202
+ });
203
+ };
204
+
205
+ select();
206
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * 路径解析助手
3
+ *
4
+ * @author lxm
5
+ * @date 2022-10-11 14:10:12
6
+ * @export
7
+ * @class UrlHelper
8
+ */
9
+ export class UrlHelper {
10
+ /**
11
+ * 路由路径前面的基础路径
12
+ * 如:http://172.16.103.120:30061/portalwebapp/#/index/appportalview?params=123
13
+ * 返回:http://172.16.103.120:30061/portalwebapp/#
14
+ *
15
+ * @author lxm
16
+ * @date 2022-10-11 20:10:29
17
+ * @returns {*}
18
+ */
19
+ static get routeBase() {
20
+ return `${this.appBase}/#`;
21
+ }
22
+
23
+ /**
24
+ * 应用的的基础路径
25
+ * 如:http://172.16.103.120:30061/portalwebapp/#/index/appportalview?params=123
26
+ * 返回:http://172.16.103.120:30061/portalwebapp
27
+ *
28
+ * @author lxm
29
+ * @date 2022-10-11 20:10:29
30
+ * @returns {*}
31
+ */
32
+ static get appBase() {
33
+ const { origin, pathname } = window.location;
34
+ return `${origin}${pathname}`.replace(/\/$/, '');
35
+ }
36
+
37
+ /**
38
+ * #开始到末尾,即路由地址
39
+ * 如:http://172.16.103.120:30061/portalwebapp/#/index/appportalview?params=123
40
+ * 返回:/index/appportalview?params=123
41
+ *
42
+ * @author lxm
43
+ * @date 2022-10-11 16:10:40
44
+ * @returns {*}
45
+ */
46
+ static get routePath() {
47
+ return window.location.hash.replace('#', '');
48
+ }
49
+
50
+ /**
51
+ * 当前地址的全路径,包含域名和参数
52
+ * 如:http://172.16.103.120:30061/portalwebapp/#/index/appportalview?params=123
53
+ *
54
+ * @author lxm
55
+ * @date 2022-10-11 21:10:39
56
+ * @readonly
57
+ * @static
58
+ */
59
+ static get fullPath() {
60
+ return window.location.href;
61
+ }
62
+ }
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { debounce, DebounceSettings } from 'lodash-es';
2
+ import { debounce } from 'lodash-es';
3
3
  import { getCookie } from 'qx-util';
4
4
  import { CoreConst } from '../../constant';
5
5
 
@@ -27,11 +27,12 @@ export function getToken(): string | null {
27
27
  */
28
28
  export function isOverlap(arr1: any[], arr2: any[]): boolean {
29
29
  const newArr = Array.from(new Set([...arr1, ...arr2]));
30
- return newArr.length === arr1.length + arr2.length;
30
+ return newArr.length !== arr1.length + arr2.length;
31
31
  }
32
32
 
33
33
  /**
34
- * 防抖并合并每次的参数
34
+ * 防抖并合并每次的参数,
35
+ * 最后一次才会执行
35
36
  *
36
37
  * @author lxm
37
38
  * @date 2022-09-20 21:09:53
@@ -43,7 +44,6 @@ export function isOverlap(arr1: any[], arr2: any[]): boolean {
43
44
  * newParams: Parameters<T>,
44
45
  * ) => Parameters<T>} mergeFunc 合并的回调函数
45
46
  * @param {number} [wait] 防抖的延迟毫秒数
46
- * @param {DebounceSettings} [options] 防抖函数的额外参数
47
47
  * @returns {*}
48
48
  */
49
49
  export function debounceAndMerge<T extends (...args: any[]) => any>(
@@ -53,19 +53,14 @@ export function debounceAndMerge<T extends (...args: any[]) => any>(
53
53
  newParams: Parameters<T>,
54
54
  ) => Parameters<T>,
55
55
  wait?: number,
56
- options?: DebounceSettings,
57
56
  ) {
58
57
  // 缓存上一次的函数参数
59
58
  let oldParams: Parameters<T> | undefined;
60
- const debounceFunc = debounce(
61
- (...params: Parameters<T>) => {
62
- // 防抖函数执行的时候清空缓存参数
63
- oldParams = undefined;
64
- return func(...params);
65
- },
66
- wait,
67
- options,
68
- );
59
+ const debounceFunc = debounce((...params: Parameters<T>) => {
60
+ // 防抖函数执行的时候清空缓存参数
61
+ oldParams = undefined;
62
+ return func(...params);
63
+ }, wait);
69
64
  return (...args: Parameters<T>) => {
70
65
  // 合并参数,并把参数缓存到oldParams里,用新参数调用防抖函数
71
66
  let newParams = args;
@@ -76,3 +71,66 @@ export function debounceAndMerge<T extends (...args: any[]) => any>(
76
71
  return debounceFunc(...newParams);
77
72
  };
78
73
  }
74
+
75
+ /**
76
+ * 防抖并合并每次的参数,最后一次才会执行
77
+ * 绑定方法为异步方法,给每次调用返回最终执行那次的结果
78
+ *
79
+ * @author lxm
80
+ * @date 2022-09-20 21:09:53
81
+ * @export
82
+ * @template T
83
+ * @param {T} func 要防抖的函数
84
+ * @param {(
85
+ * oldParams: Parameters<T>,
86
+ * newParams: Parameters<T>,
87
+ * ) => Parameters<T>} mergeFunc 合并的回调函数
88
+ * @param {number} [wait] 防抖的延迟毫秒数
89
+ * @returns {*}
90
+ */
91
+ export function debounceAndAsyncMerge<
92
+ T extends (...args: any[]) => Promise<any>,
93
+ >(
94
+ func: T,
95
+ mergeFunc: (
96
+ oldParams: Parameters<T>,
97
+ newParams: Parameters<T>,
98
+ ) => Parameters<T>,
99
+ wait?: number,
100
+ ): T {
101
+ // 缓存上一次的函数参数
102
+ let oldParams: Parameters<T> | undefined;
103
+ let promises: Array<{ resolve: any; reject: any }> = [];
104
+ const debounceFunc = debounce(async (...params: Parameters<T>) => {
105
+ // 防抖函数执行的时候清空缓存参数
106
+ oldParams = undefined;
107
+ try {
108
+ const result = await func(...params);
109
+ promises.forEach(promise => {
110
+ promise.resolve(result);
111
+ });
112
+ promises = [];
113
+ return result;
114
+ } catch (error) {
115
+ promises.forEach(promise => {
116
+ promise.reject(error);
117
+ });
118
+ promises = [];
119
+ }
120
+ }, wait);
121
+ const fun = async (...args: Parameters<T>) => {
122
+ // 合并参数,并把参数缓存到oldParams里,用新参数调用防抖函数
123
+ let newParams = args;
124
+ if (oldParams) {
125
+ newParams = mergeFunc(oldParams, newParams);
126
+ }
127
+ oldParams = newParams;
128
+ // 执行防抖函数
129
+ debounceFunc(...newParams);
130
+ return new Promise((resolve, reject) => {
131
+ // 阻塞当前调用,当防抖函数被执行后返回
132
+ promises.push({ resolve, reject });
133
+ });
134
+ };
135
+ return fun as T;
136
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 千反田丷
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.