@daysnap/utils 0.0.67 → 0.0.68

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.
@@ -0,0 +1,10 @@
1
+ import { Loose } from '@daysnap/types';
2
+ /**
3
+ * loading 生成器
4
+ * const withLoading = createWithLoading(() => showLoading())
5
+ * const fn = withLoading(async () => { // ... })
6
+ * fn() 执行的时候就会执行showLoading
7
+ */
8
+ export declare function createWithLoading<O = any>(showLoading: (options: O) => Loose<{
9
+ close: () => any;
10
+ }>): <T extends (...args: any[]) => Promise<any>>(fn: T, options?: O) => (...params: [...Parameters<T>, (O | undefined)?]) => Promise<Awaited<ReturnType<T>>>;
@@ -0,0 +1,35 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * loading 生成器
12
+ * const withLoading = createWithLoading(() => showLoading())
13
+ * const fn = withLoading(async () => { // ... })
14
+ * fn() 执行的时候就会执行showLoading
15
+ */
16
+ export function createWithLoading(showLoading) {
17
+ return function withLoading(fn, options) {
18
+ return (...params) => __awaiter(this, void 0, void 0, function* () {
19
+ if (params.length > fn.length) {
20
+ options = params.pop();
21
+ }
22
+ const toast = options ? showLoading(options) : null;
23
+ try {
24
+ return yield fn(params);
25
+ // eslint-disable-next-line no-useless-catch
26
+ }
27
+ catch (error) {
28
+ throw error;
29
+ }
30
+ finally {
31
+ toast === null || toast === void 0 ? void 0 : toast.close();
32
+ }
33
+ });
34
+ };
35
+ }
package/es/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export * from './cloneSimple';
10
10
  export * from './compareVersion';
11
11
  export * from './compressImage';
12
12
  export * from './createHexColorByHash';
13
+ export * from './createWithLoading';
13
14
  export * from './debounce';
14
15
  export * from './downloadFile';
15
16
  export * from './each';
package/es/index.js CHANGED
@@ -11,6 +11,7 @@ export * from './cloneSimple';
11
11
  export * from './compareVersion';
12
12
  export * from './compressImage';
13
13
  export * from './createHexColorByHash';
14
+ export * from './createWithLoading';
14
15
  export * from './debounce';
15
16
  export * from './downloadFile';
16
17
  export * from './each';
@@ -0,0 +1,10 @@
1
+ import { Loose } from '@daysnap/types';
2
+ /**
3
+ * loading 生成器
4
+ * const withLoading = createWithLoading(() => showLoading())
5
+ * const fn = withLoading(async () => { // ... })
6
+ * fn() 执行的时候就会执行showLoading
7
+ */
8
+ export declare function createWithLoading<O = any>(showLoading: (options: O) => Loose<{
9
+ close: () => any;
10
+ }>): <T extends (...args: any[]) => Promise<any>>(fn: T, options?: O) => (...params: [...Parameters<T>, (O | undefined)?]) => Promise<Awaited<ReturnType<T>>>;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createWithLoading = void 0;
13
+ /**
14
+ * loading 生成器
15
+ * const withLoading = createWithLoading(() => showLoading())
16
+ * const fn = withLoading(async () => { // ... })
17
+ * fn() 执行的时候就会执行showLoading
18
+ */
19
+ function createWithLoading(showLoading) {
20
+ return function withLoading(fn, options) {
21
+ return (...params) => __awaiter(this, void 0, void 0, function* () {
22
+ if (params.length > fn.length) {
23
+ options = params.pop();
24
+ }
25
+ const toast = options ? showLoading(options) : null;
26
+ try {
27
+ return yield fn(params);
28
+ // eslint-disable-next-line no-useless-catch
29
+ }
30
+ catch (error) {
31
+ throw error;
32
+ }
33
+ finally {
34
+ toast === null || toast === void 0 ? void 0 : toast.close();
35
+ }
36
+ });
37
+ };
38
+ }
39
+ exports.createWithLoading = createWithLoading;
package/lib/index.d.ts CHANGED
@@ -10,6 +10,7 @@ export * from './cloneSimple';
10
10
  export * from './compareVersion';
11
11
  export * from './compressImage';
12
12
  export * from './createHexColorByHash';
13
+ export * from './createWithLoading';
13
14
  export * from './debounce';
14
15
  export * from './downloadFile';
15
16
  export * from './each';
package/lib/index.js CHANGED
@@ -27,6 +27,7 @@ __exportStar(require("./cloneSimple"), exports);
27
27
  __exportStar(require("./compareVersion"), exports);
28
28
  __exportStar(require("./compressImage"), exports);
29
29
  __exportStar(require("./createHexColorByHash"), exports);
30
+ __exportStar(require("./createWithLoading"), exports);
30
31
  __exportStar(require("./debounce"), exports);
31
32
  __exportStar(require("./downloadFile"), exports);
32
33
  __exportStar(require("./each"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daysnap/utils",
3
- "version": "0.0.67",
3
+ "version": "0.0.68",
4
4
  "description": "通用的工具库",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -54,6 +54,7 @@
54
54
  "devDependencies": {
55
55
  "@commitlint/cli": "^16.2.1",
56
56
  "@commitlint/config-conventional": "^16.2.1",
57
+ "@daysnap/types": "^0.0.4",
57
58
  "@types/jest": "^27.5.2",
58
59
  "@typescript-eslint/eslint-plugin": "^5.55.0",
59
60
  "@typescript-eslint/parser": "^5.55.0",