@vrojs/use 0.0.1
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/dist/index.d.ts +65 -0
- package/dist/index.js +148 -0
- package/package.json +51 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as vue from 'vue';
|
|
2
|
+
import { Ref } from 'vue';
|
|
3
|
+
|
|
4
|
+
type AnyPromiseFn = (...args: any[]) => Promise<any>;
|
|
5
|
+
interface UseAsyncTaskOptions<T extends AnyPromiseFn> {
|
|
6
|
+
/**
|
|
7
|
+
* 是否立即执行 onBeforeMount 执行,默认 false
|
|
8
|
+
*/
|
|
9
|
+
immediate?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 是否在 onActivated 执行,默认 false
|
|
12
|
+
*/
|
|
13
|
+
activated?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* 立即执行的时候 第一次需要传递的参数
|
|
16
|
+
*/
|
|
17
|
+
initialParams?: Parameters<T> | (() => Parameters<T>);
|
|
18
|
+
/**
|
|
19
|
+
* 初始数据
|
|
20
|
+
*/
|
|
21
|
+
initialValue?: Awaited<ReturnType<T>>;
|
|
22
|
+
/**
|
|
23
|
+
* 捕获了异常,是否继续抛出,默认 false
|
|
24
|
+
*/
|
|
25
|
+
throwError?: boolean | (() => boolean);
|
|
26
|
+
/**
|
|
27
|
+
* 错误回调,返回 false 或者 undefined 的时候,阻断默认错误 error 逻辑
|
|
28
|
+
*/
|
|
29
|
+
onError?: (err: unknown) => boolean | undefined | Promise<boolean | undefined>;
|
|
30
|
+
}
|
|
31
|
+
interface UseAsyncTaskResult<T, D> {
|
|
32
|
+
data: Ref<D>;
|
|
33
|
+
error: Ref<unknown>;
|
|
34
|
+
loading: Ref<boolean>;
|
|
35
|
+
trigger: T;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 异步任务执行
|
|
39
|
+
*/
|
|
40
|
+
declare function useAsyncTask<T extends AnyPromiseFn>(task: T, options: UseAsyncTaskOptions<T> & Required<Pick<UseAsyncTaskOptions<T>, 'initialValue'>>): UseAsyncTaskResult<T, Awaited<ReturnType<T>>>;
|
|
41
|
+
declare function useAsyncTask<T extends AnyPromiseFn>(task: T, options?: UseAsyncTaskOptions<T>): UseAsyncTaskResult<T, Awaited<ReturnType<T>> | undefined>;
|
|
42
|
+
|
|
43
|
+
declare function useComposition(): {
|
|
44
|
+
handleCompositionStart: (event: CompositionEvent) => void;
|
|
45
|
+
handleCompositionEnd: (event: CompositionEvent) => void;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
declare function useId(): string;
|
|
49
|
+
|
|
50
|
+
interface UseVisibleOptions<T extends Record<string, any>, P = any> {
|
|
51
|
+
showCallback?: (options?: T) => void | Promise<void>;
|
|
52
|
+
hideCallback?: (reason: any) => void | Promise<void>;
|
|
53
|
+
confirmCallback?: (...args: any[]) => P | Promise<P>;
|
|
54
|
+
}
|
|
55
|
+
interface UseVisibleShowOptions {
|
|
56
|
+
beforeClose?: (action: 'cancel' | 'confirm', ...args: any[]) => boolean | Promise<boolean>;
|
|
57
|
+
}
|
|
58
|
+
declare function useVisible<T extends Record<string, any>, R = any>(options?: UseVisibleOptions<T, R>): {
|
|
59
|
+
visible: vue.Ref<boolean, boolean>;
|
|
60
|
+
show: <P = R>(options?: UseVisibleShowOptions & T) => Promise<P>;
|
|
61
|
+
confirm: (...args: any[]) => Promise<void>;
|
|
62
|
+
hide: (reason?: any) => Promise<void>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export { type UseAsyncTaskOptions, type UseAsyncTaskResult, type UseVisibleOptions, type UseVisibleShowOptions, useAsyncTask, useComposition, useId, useVisible };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/useAsyncTask/index.ts
|
|
23
|
+
import { isFunction } from "@daysnap/utils";
|
|
24
|
+
import { onActivated, onBeforeMount, ref } from "vue";
|
|
25
|
+
function useAsyncTask(task, options) {
|
|
26
|
+
const { initialValue, immediate, activated, throwError, onError, initialParams } = options != null ? options : {};
|
|
27
|
+
const data = ref(initialValue);
|
|
28
|
+
const error = ref();
|
|
29
|
+
const loading = ref(false);
|
|
30
|
+
const trigger = (...args) => __async(null, null, function* () {
|
|
31
|
+
var _a;
|
|
32
|
+
try {
|
|
33
|
+
error.value = void 0;
|
|
34
|
+
loading.value = true;
|
|
35
|
+
data.value = yield task(...args);
|
|
36
|
+
return data.value;
|
|
37
|
+
} catch (err) {
|
|
38
|
+
if ((_a = yield onError == null ? void 0 : onError(err)) != null ? _a : true) {
|
|
39
|
+
error.value = err;
|
|
40
|
+
const isThrow = isFunction(throwError) ? throwError() : throwError;
|
|
41
|
+
if (isThrow) {
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} finally {
|
|
46
|
+
loading.value = false;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
const init = () => __async(null, null, function* () {
|
|
50
|
+
const args = isFunction(initialParams) ? initialParams() : initialParams != null ? initialParams : [];
|
|
51
|
+
yield trigger(...args);
|
|
52
|
+
});
|
|
53
|
+
onBeforeMount(() => __async(null, null, function* () {
|
|
54
|
+
if (immediate) {
|
|
55
|
+
yield init();
|
|
56
|
+
}
|
|
57
|
+
}));
|
|
58
|
+
onActivated(() => __async(null, null, function* () {
|
|
59
|
+
if (activated) {
|
|
60
|
+
yield init();
|
|
61
|
+
}
|
|
62
|
+
}));
|
|
63
|
+
return {
|
|
64
|
+
data,
|
|
65
|
+
error,
|
|
66
|
+
loading,
|
|
67
|
+
trigger
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/useComposition/index.ts
|
|
72
|
+
function useComposition() {
|
|
73
|
+
const handleCompositionStart = (event) => {
|
|
74
|
+
;
|
|
75
|
+
event.target.composing = true;
|
|
76
|
+
};
|
|
77
|
+
const handleCompositionEnd = (event) => {
|
|
78
|
+
var _a;
|
|
79
|
+
;
|
|
80
|
+
event.target.composing = false;
|
|
81
|
+
(_a = event.target) == null ? void 0 : _a.dispatchEvent(new Event("input", { bubbles: true }));
|
|
82
|
+
};
|
|
83
|
+
return {
|
|
84
|
+
handleCompositionStart,
|
|
85
|
+
handleCompositionEnd
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/useId/index.ts
|
|
90
|
+
import { getCurrentInstance } from "vue";
|
|
91
|
+
var current = 0;
|
|
92
|
+
function useId() {
|
|
93
|
+
const vm = getCurrentInstance();
|
|
94
|
+
const name = typeof (vm == null ? void 0 : vm.type) === "object" ? vm.type.name || "unknown" : "unknown";
|
|
95
|
+
return `${name}-${++current}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/useVisible/index.ts
|
|
99
|
+
import { ref as ref2 } from "vue";
|
|
100
|
+
function useVisible(options = {}) {
|
|
101
|
+
const { showCallback, hideCallback, confirmCallback } = options;
|
|
102
|
+
const visible = ref2(false);
|
|
103
|
+
let resolve;
|
|
104
|
+
let reject;
|
|
105
|
+
let beforeClose;
|
|
106
|
+
const show = (options2) => __async(null, null, function* () {
|
|
107
|
+
beforeClose = options2 == null ? void 0 : options2.beforeClose;
|
|
108
|
+
yield showCallback == null ? void 0 : showCallback(options2);
|
|
109
|
+
return new Promise((_resolve, _reject) => {
|
|
110
|
+
resolve = _resolve;
|
|
111
|
+
reject = _reject;
|
|
112
|
+
visible.value = true;
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
const hide = (reason) => __async(null, null, function* () {
|
|
116
|
+
var _a;
|
|
117
|
+
const result = (_a = yield beforeClose == null ? void 0 : beforeClose("cancel", reason)) != null ? _a : true;
|
|
118
|
+
if (!result) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
yield hideCallback == null ? void 0 : hideCallback(reason);
|
|
122
|
+
reject == null ? void 0 : reject(reason || "cancel");
|
|
123
|
+
visible.value = false;
|
|
124
|
+
});
|
|
125
|
+
const confirm = (...args) => __async(null, null, function* () {
|
|
126
|
+
var _a;
|
|
127
|
+
const result = (_a = yield beforeClose == null ? void 0 : beforeClose("confirm", ...args)) != null ? _a : true;
|
|
128
|
+
if (!result) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const value = confirmCallback ? yield confirmCallback(...args) : args[0];
|
|
132
|
+
reject = null;
|
|
133
|
+
resolve == null ? void 0 : resolve(value);
|
|
134
|
+
visible.value = false;
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
visible,
|
|
138
|
+
show,
|
|
139
|
+
confirm,
|
|
140
|
+
hide
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
export {
|
|
144
|
+
useAsyncTask,
|
|
145
|
+
useComposition,
|
|
146
|
+
useId,
|
|
147
|
+
useVisible
|
|
148
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vrojs/use",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "vrojs composables",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"keywords": [
|
|
14
|
+
"vro",
|
|
15
|
+
"vrojs",
|
|
16
|
+
"composables",
|
|
17
|
+
"vue"
|
|
18
|
+
],
|
|
19
|
+
"author": "woshiajuana",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"registry": "https://registry.npmjs.org/"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@daysnap/utils": ">=0.1.34",
|
|
27
|
+
"vue": ">=3.0.0",
|
|
28
|
+
"vue-router": ">=4.2.4"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@daysnap/utils": "^0.1.34",
|
|
32
|
+
"rimraf": "^6.0.1",
|
|
33
|
+
"tsup": "^8.5.1",
|
|
34
|
+
"typescript": "^5.9.3",
|
|
35
|
+
"vue": "^3.5.22"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "npm run clean && tsup",
|
|
39
|
+
"clean": "rimraf ./dist",
|
|
40
|
+
"release": "npm run build && dsc publish --pnpm",
|
|
41
|
+
"sync": "dsc sync"
|
|
42
|
+
},
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"types": "./dist/index.d.ts",
|
|
46
|
+
"import": "./dist/index.js",
|
|
47
|
+
"default": "./dist/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./package.json": "./package.json"
|
|
50
|
+
}
|
|
51
|
+
}
|