@xn-lib/core 0.1.2 → 0.1.4

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.
@@ -1,2 +1,4 @@
1
1
  export * from './use-dict';
2
2
  export * from './use-reset-ref';
3
+ export * from './use-auto-promise';
4
+ export * from './requestQuenue';
@@ -0,0 +1,2 @@
1
+ export * from './use-request-queue';
2
+ export * from './use-advanced-request-queue';
@@ -0,0 +1,20 @@
1
+ export interface RequestTask<T = any> {
2
+ id: string;
3
+ request: () => Promise<T>;
4
+ priority?: number;
5
+ retryCount?: number;
6
+ timeout?: number;
7
+ resolve: (value: T) => void;
8
+ reject: (reason?: any) => void;
9
+ status: 'pending' | 'running' | 'success' | 'error' | 'cancelled';
10
+ startTime?: number;
11
+ timeoutId?: number;
12
+ cancelController?: AbortController;
13
+ }
14
+ export interface UseRequestQueueOptions {
15
+ maxConcurrent?: number;
16
+ timeout?: number;
17
+ retryCount?: number;
18
+ autoStart?: boolean;
19
+ useAbortController?: boolean;
20
+ }
@@ -0,0 +1,139 @@
1
+ import { RequestTask, UseRequestQueueOptions } from './type';
2
+ export interface AdvancedRequestTask<T = any> extends RequestTask<T> {
3
+ dependencies?: string[];
4
+ cacheKey?: string;
5
+ cacheTTL?: number;
6
+ group?: string;
7
+ }
8
+ export interface UseAdvancedRequestQueueOptions extends UseRequestQueueOptions {
9
+ enableCache?: boolean;
10
+ defaultCacheTTL?: number;
11
+ }
12
+ export declare function useAdvancedRequestQueue(options?: UseAdvancedRequestQueueOptions): {
13
+ cache: Record<string, {
14
+ data: any;
15
+ timestamp: number;
16
+ ttl: number;
17
+ }>;
18
+ taskGroups: Record<string, AdvancedRequestTask<any>[]>;
19
+ dependencyGraph: Record<string, string[]>;
20
+ cacheStats: import("vue").ComputedRef<{
21
+ total: number;
22
+ expired: number;
23
+ }>;
24
+ addTaskWithCache: <T>(request: () => Promise<T>, options?: Partial<AdvancedRequestTask<T>>) => Promise<T>;
25
+ addTaskWithDependencies: <T>(request: () => Promise<T>, dependencies: string[], options?: Partial<AdvancedRequestTask<T>>) => Promise<T>;
26
+ addTasksToGroup: <T>(groupName: string, requests: Array<{
27
+ request: () => Promise<T>;
28
+ options?: Partial<AdvancedRequestTask<T>>;
29
+ }>) => Promise<T[]>;
30
+ clearCache: () => void;
31
+ clearCacheByKey: (cacheKey: string) => void;
32
+ clearExpiredCache: () => void;
33
+ waitForDependencies: (dependencies: string[]) => Promise<void>;
34
+ queue: import("vue").Reactive<RequestTask<any>[]>;
35
+ runningTasks: import("vue").Reactive<Map<string, AbortController>>;
36
+ taskResults: Record<string, any>;
37
+ taskErrors: Record<string, any>;
38
+ isRunning: import("vue").Ref<boolean, boolean>;
39
+ stats: {
40
+ total: number;
41
+ success: number;
42
+ error: number;
43
+ pending: number;
44
+ running: number;
45
+ cancelled: number;
46
+ };
47
+ completedCount: import("vue").ComputedRef<number>;
48
+ progress: import("vue").ComputedRef<number>;
49
+ activeTasks: import("vue").ComputedRef<{
50
+ id: string;
51
+ request: () => Promise<any>;
52
+ priority?: number | undefined;
53
+ retryCount?: number | undefined;
54
+ timeout?: number | undefined;
55
+ resolve: (value: any) => void;
56
+ reject: (reason?: any) => void;
57
+ status: "pending" | "running" | "success" | "error" | "cancelled";
58
+ startTime?: number | undefined;
59
+ timeoutId?: number | undefined;
60
+ cancelController?: {
61
+ readonly signal: {
62
+ readonly aborted: boolean;
63
+ onabort: ((this: AbortSignal, ev: Event) => any) | null;
64
+ readonly reason: any;
65
+ throwIfAborted: () => void;
66
+ addEventListener: {
67
+ <K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
68
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
69
+ };
70
+ removeEventListener: {
71
+ <K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
72
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
73
+ };
74
+ dispatchEvent: (event: Event) => boolean;
75
+ };
76
+ abort: (reason?: any) => void;
77
+ } | undefined;
78
+ }[]>;
79
+ pendingTasks: import("vue").ComputedRef<{
80
+ id: string;
81
+ request: () => Promise<any>;
82
+ priority?: number | undefined;
83
+ retryCount?: number | undefined;
84
+ timeout?: number | undefined;
85
+ resolve: (value: any) => void;
86
+ reject: (reason?: any) => void;
87
+ status: "pending" | "running" | "success" | "error" | "cancelled";
88
+ startTime?: number | undefined;
89
+ timeoutId?: number | undefined;
90
+ cancelController?: {
91
+ readonly signal: {
92
+ readonly aborted: boolean;
93
+ onabort: ((this: AbortSignal, ev: Event) => any) | null;
94
+ readonly reason: any;
95
+ throwIfAborted: () => void;
96
+ addEventListener: {
97
+ <K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
98
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
99
+ };
100
+ removeEventListener: {
101
+ <K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
102
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
103
+ };
104
+ dispatchEvent: (event: Event) => boolean;
105
+ };
106
+ abort: (reason?: any) => void;
107
+ } | undefined;
108
+ }[]>;
109
+ runningTaskCount: import("vue").ComputedRef<number>;
110
+ addTask: <T>(request: () => Promise<T>, taskOptions?: Partial<Omit<RequestTask<T>, "resolve" | "reject" | "status" | "request">>) => Promise<T> & {
111
+ cancel?: () => void;
112
+ };
113
+ addTasks: <T>(requests: (() => Promise<T>)[], options?: Partial<Omit<RequestTask<T>, "resolve" | "reject" | "status" | "request">>) => Promise<T[]> & {
114
+ cancelAll?: () => void;
115
+ };
116
+ addTaskList: <T>(tasks: Array<{
117
+ request: () => Promise<T>;
118
+ id?: string;
119
+ priority?: number;
120
+ retryCount?: number;
121
+ timeout?: number;
122
+ }>, commonOptions?: Partial<Omit<RequestTask<T>, "resolve" | "reject" | "status" | "request">>) => Promise<T[]> & {
123
+ cancelAll?: () => void;
124
+ };
125
+ start: () => void;
126
+ pause: () => void;
127
+ clear: () => void;
128
+ removeTask: (taskId: string) => void;
129
+ processQueue: () => Promise<void>;
130
+ cancelTask: (taskId: string, reason?: string) => boolean;
131
+ cancelTasks: (taskIds: string[], reason?: string) => {
132
+ taskId: string;
133
+ success: boolean;
134
+ }[];
135
+ cancelAll: (reason?: string) => {
136
+ taskId: string;
137
+ success: boolean;
138
+ }[];
139
+ };
@@ -0,0 +1,108 @@
1
+ import { RequestTask, UseRequestQueueOptions } from './type';
2
+ export declare function useRequestQueue(options?: UseRequestQueueOptions): {
3
+ queue: import("vue").Reactive<RequestTask<any>[]>;
4
+ runningTasks: import("vue").Reactive<Map<string, AbortController>>;
5
+ taskResults: Record<string, any>;
6
+ taskErrors: Record<string, any>;
7
+ isRunning: import("vue").Ref<boolean, boolean>;
8
+ stats: {
9
+ total: number;
10
+ success: number;
11
+ error: number;
12
+ pending: number;
13
+ running: number;
14
+ cancelled: number;
15
+ };
16
+ completedCount: import("vue").ComputedRef<number>;
17
+ progress: import("vue").ComputedRef<number>;
18
+ activeTasks: import("vue").ComputedRef<{
19
+ id: string;
20
+ request: () => Promise<any>;
21
+ priority?: number | undefined;
22
+ retryCount?: number | undefined;
23
+ timeout?: number | undefined;
24
+ resolve: (value: any) => void;
25
+ reject: (reason?: any) => void;
26
+ status: "pending" | "running" | "success" | "error" | "cancelled";
27
+ startTime?: number | undefined;
28
+ timeoutId?: number | undefined;
29
+ cancelController?: {
30
+ readonly signal: {
31
+ readonly aborted: boolean;
32
+ onabort: ((this: AbortSignal, ev: Event) => any) | null;
33
+ readonly reason: any;
34
+ throwIfAborted: () => void;
35
+ addEventListener: {
36
+ <K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
37
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
38
+ };
39
+ removeEventListener: {
40
+ <K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
41
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
42
+ };
43
+ dispatchEvent: (event: Event) => boolean;
44
+ };
45
+ abort: (reason?: any) => void;
46
+ } | undefined;
47
+ }[]>;
48
+ pendingTasks: import("vue").ComputedRef<{
49
+ id: string;
50
+ request: () => Promise<any>;
51
+ priority?: number | undefined;
52
+ retryCount?: number | undefined;
53
+ timeout?: number | undefined;
54
+ resolve: (value: any) => void;
55
+ reject: (reason?: any) => void;
56
+ status: "pending" | "running" | "success" | "error" | "cancelled";
57
+ startTime?: number | undefined;
58
+ timeoutId?: number | undefined;
59
+ cancelController?: {
60
+ readonly signal: {
61
+ readonly aborted: boolean;
62
+ onabort: ((this: AbortSignal, ev: Event) => any) | null;
63
+ readonly reason: any;
64
+ throwIfAborted: () => void;
65
+ addEventListener: {
66
+ <K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
67
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
68
+ };
69
+ removeEventListener: {
70
+ <K extends keyof AbortSignalEventMap>(type: K, listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
71
+ (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
72
+ };
73
+ dispatchEvent: (event: Event) => boolean;
74
+ };
75
+ abort: (reason?: any) => void;
76
+ } | undefined;
77
+ }[]>;
78
+ runningTaskCount: import("vue").ComputedRef<number>;
79
+ addTask: <T>(request: () => Promise<T>, taskOptions?: Partial<Omit<RequestTask<T>, "resolve" | "reject" | "status" | "request">>) => Promise<T> & {
80
+ cancel?: () => void;
81
+ };
82
+ addTasks: <T>(requests: (() => Promise<T>)[], options?: Partial<Omit<RequestTask<T>, "resolve" | "reject" | "status" | "request">>) => Promise<T[]> & {
83
+ cancelAll?: () => void;
84
+ };
85
+ addTaskList: <T>(tasks: Array<{
86
+ request: () => Promise<T>;
87
+ id?: string;
88
+ priority?: number;
89
+ retryCount?: number;
90
+ timeout?: number;
91
+ }>, commonOptions?: Partial<Omit<RequestTask<T>, "resolve" | "reject" | "status" | "request">>) => Promise<T[]> & {
92
+ cancelAll?: () => void;
93
+ };
94
+ start: () => void;
95
+ pause: () => void;
96
+ clear: () => void;
97
+ removeTask: (taskId: string) => void;
98
+ processQueue: () => Promise<void>;
99
+ cancelTask: (taskId: string, reason?: string) => boolean;
100
+ cancelTasks: (taskIds: string[], reason?: string) => {
101
+ taskId: string;
102
+ success: boolean;
103
+ }[];
104
+ cancelAll: (reason?: string) => {
105
+ taskId: string;
106
+ success: boolean;
107
+ }[];
108
+ };
@@ -0,0 +1,9 @@
1
+ import { type Ref } from 'vue';
2
+ type TApiFun<TData, TParams extends Array<any>> = (...params: TParams) => Promise<TData>;
3
+ interface AutoRequestOptions {
4
+ loading?: boolean;
5
+ onSuccess?: (data: any) => void;
6
+ }
7
+ type AutoRequestResult<TData, TParams extends Array<any>> = [Ref<boolean>, TApiFun<TData, TParams>];
8
+ export declare function useAutoPromise<TData, TParams extends any[] = any[]>(fun: TApiFun<TData, TParams>, options?: AutoRequestOptions): AutoRequestResult<TData, TParams>;
9
+ export {};
@@ -2,3 +2,4 @@ export * from './dicts';
2
2
  export * from './hooks';
3
3
  export * from './vue';
4
4
  export * from './patterns';
5
+ export * from './utils';
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 生成字符串的简单哈希码
3
+ */
4
+ export declare function hashCode(str: string): string;
5
+ /**
6
+ * 生成更高质量的53位哈希(cyrb53算法)
7
+ */
8
+ export declare function cyrb53(str: string, seed?: number): number;
9
+ /**
10
+ * 生成请求缓存键
11
+ */
12
+ export declare function generateCacheKey(...args: any[]): string;
13
+ /**
14
+ * 异步生成哈希(使用 Web Crypto API)
15
+ */
16
+ export declare function generateSHA256Hash(str: string): Promise<string>;
@@ -0,0 +1 @@
1
+ export * from './hash';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@xn-lib/core",
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "description": "",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.mjs",