@azure-net/kit 3.0.3 → 3.0.45
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,9 +1,14 @@
|
|
|
1
1
|
export type AsyncStatus = 'idle' | 'pending' | 'success' | 'error';
|
|
2
|
+
export type AsyncSignalSource = 'auto' | 'manual' | 'global';
|
|
2
3
|
export interface AsyncSignalOptions<TData, TError = Error> {
|
|
3
4
|
server?: boolean;
|
|
4
5
|
immediate?: boolean;
|
|
5
6
|
watch?: (() => unknown)[];
|
|
6
7
|
initialData?: TData;
|
|
8
|
+
beforeSend?: (meta: {
|
|
9
|
+
initial: boolean;
|
|
10
|
+
source: AsyncSignalSource;
|
|
11
|
+
}) => void | Promise<void>;
|
|
7
12
|
onSuccess?: (data: TData) => void | Promise<void>;
|
|
8
13
|
onError?: (error: TError) => void | Promise<void>;
|
|
9
14
|
key?: string;
|
|
@@ -25,7 +25,7 @@ const createAsyncSignalManager = () => {
|
|
|
25
25
|
if (instances) {
|
|
26
26
|
const instance = instances.get(key);
|
|
27
27
|
try {
|
|
28
|
-
await instance?.();
|
|
28
|
+
await instance?.('global');
|
|
29
29
|
}
|
|
30
30
|
catch {
|
|
31
31
|
return;
|
|
@@ -35,7 +35,7 @@ const createAsyncSignalManager = () => {
|
|
|
35
35
|
const refreshAll = async () => {
|
|
36
36
|
if (instances) {
|
|
37
37
|
try {
|
|
38
|
-
await Promise.all(instances.values().map((val) => val()));
|
|
38
|
+
await Promise.all(instances.values().map((val) => val('global')));
|
|
39
39
|
}
|
|
40
40
|
catch {
|
|
41
41
|
return;
|
|
@@ -54,11 +54,17 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
54
54
|
let abortController = null;
|
|
55
55
|
let currentPromise = null;
|
|
56
56
|
let currentRunId = 0;
|
|
57
|
-
|
|
57
|
+
let started = false;
|
|
58
|
+
const run = async (runId, source) => {
|
|
59
|
+
const initial = !started;
|
|
60
|
+
started = true;
|
|
58
61
|
if (abortController) {
|
|
59
62
|
abortController.abort();
|
|
60
63
|
}
|
|
61
64
|
abortController = new AbortController();
|
|
65
|
+
if (options.beforeSend) {
|
|
66
|
+
await options.beforeSend({ initial, source });
|
|
67
|
+
}
|
|
62
68
|
status = 'pending';
|
|
63
69
|
error = undefined;
|
|
64
70
|
try {
|
|
@@ -90,22 +96,27 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
90
96
|
}
|
|
91
97
|
}
|
|
92
98
|
};
|
|
93
|
-
const start = () => {
|
|
99
|
+
const start = (source) => {
|
|
100
|
+
if (currentPromise) {
|
|
101
|
+
return currentPromise;
|
|
102
|
+
}
|
|
94
103
|
const runId = ++currentRunId;
|
|
95
|
-
const localPromise = run(runId);
|
|
104
|
+
const localPromise = run(runId, source);
|
|
96
105
|
currentPromise = localPromise;
|
|
97
106
|
return localPromise;
|
|
98
107
|
};
|
|
99
108
|
const execute = async () => {
|
|
100
|
-
if (
|
|
109
|
+
if (currentPromise) {
|
|
101
110
|
await currentPromise;
|
|
102
111
|
return;
|
|
103
112
|
}
|
|
104
|
-
await start();
|
|
113
|
+
await start('manual');
|
|
105
114
|
};
|
|
106
115
|
if (EnvironmentUtil.isBrowser) {
|
|
107
116
|
const signalKey = key ?? asyncSignalManager.generateKey();
|
|
108
|
-
asyncSignalManager.register(signalKey, () =>
|
|
117
|
+
asyncSignalManager.register(signalKey, (source) => {
|
|
118
|
+
return start(source);
|
|
119
|
+
});
|
|
109
120
|
$effect(() => {
|
|
110
121
|
return () => {
|
|
111
122
|
asyncSignalManager.unregister(signalKey);
|
|
@@ -120,7 +131,7 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
120
131
|
return;
|
|
121
132
|
}
|
|
122
133
|
if (!isFirst) {
|
|
123
|
-
void
|
|
134
|
+
void start('auto');
|
|
124
135
|
}
|
|
125
136
|
isFirst = false;
|
|
126
137
|
});
|
|
@@ -129,11 +140,11 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
129
140
|
if (immediate) {
|
|
130
141
|
if (EnvironmentUtil.isServer && server) {
|
|
131
142
|
untrack(() => {
|
|
132
|
-
void
|
|
143
|
+
void start('auto');
|
|
133
144
|
});
|
|
134
145
|
}
|
|
135
146
|
else if (EnvironmentUtil.isBrowser) {
|
|
136
|
-
void
|
|
147
|
+
void start('auto');
|
|
137
148
|
}
|
|
138
149
|
}
|
|
139
150
|
return {
|
|
@@ -152,7 +163,7 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
152
163
|
get ready() {
|
|
153
164
|
if (currentPromise)
|
|
154
165
|
return currentPromise;
|
|
155
|
-
return start();
|
|
166
|
+
return start('auto');
|
|
156
167
|
},
|
|
157
168
|
execute,
|
|
158
169
|
refresh: execute,
|