@azure-net/kit 3.0.4 → 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,4 +1,5 @@
|
|
|
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;
|
|
@@ -6,7 +7,7 @@ export interface AsyncSignalOptions<TData, TError = Error> {
|
|
|
6
7
|
initialData?: TData;
|
|
7
8
|
beforeSend?: (meta: {
|
|
8
9
|
initial: boolean;
|
|
9
|
-
|
|
10
|
+
source: AsyncSignalSource;
|
|
10
11
|
}) => void | Promise<void>;
|
|
11
12
|
onSuccess?: (data: TData) => void | Promise<void>;
|
|
12
13
|
onError?: (error: TError) => void | Promise<void>;
|
|
@@ -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;
|
|
@@ -55,7 +55,7 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
55
55
|
let currentPromise = null;
|
|
56
56
|
let currentRunId = 0;
|
|
57
57
|
let started = false;
|
|
58
|
-
const run = async (runId,
|
|
58
|
+
const run = async (runId, source) => {
|
|
59
59
|
const initial = !started;
|
|
60
60
|
started = true;
|
|
61
61
|
if (abortController) {
|
|
@@ -63,7 +63,7 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
63
63
|
}
|
|
64
64
|
abortController = new AbortController();
|
|
65
65
|
if (options.beforeSend) {
|
|
66
|
-
await options.beforeSend({ initial,
|
|
66
|
+
await options.beforeSend({ initial, source });
|
|
67
67
|
}
|
|
68
68
|
status = 'pending';
|
|
69
69
|
error = undefined;
|
|
@@ -96,12 +96,12 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
};
|
|
99
|
-
const start = (
|
|
99
|
+
const start = (source) => {
|
|
100
100
|
if (currentPromise) {
|
|
101
101
|
return currentPromise;
|
|
102
102
|
}
|
|
103
103
|
const runId = ++currentRunId;
|
|
104
|
-
const localPromise = run(runId,
|
|
104
|
+
const localPromise = run(runId, source);
|
|
105
105
|
currentPromise = localPromise;
|
|
106
106
|
return localPromise;
|
|
107
107
|
};
|
|
@@ -110,11 +110,13 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
110
110
|
await currentPromise;
|
|
111
111
|
return;
|
|
112
112
|
}
|
|
113
|
-
await start(
|
|
113
|
+
await start('manual');
|
|
114
114
|
};
|
|
115
115
|
if (EnvironmentUtil.isBrowser) {
|
|
116
116
|
const signalKey = key ?? asyncSignalManager.generateKey();
|
|
117
|
-
asyncSignalManager.register(signalKey, () =>
|
|
117
|
+
asyncSignalManager.register(signalKey, (source) => {
|
|
118
|
+
return start(source);
|
|
119
|
+
});
|
|
118
120
|
$effect(() => {
|
|
119
121
|
return () => {
|
|
120
122
|
asyncSignalManager.unregister(signalKey);
|
|
@@ -129,7 +131,7 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
129
131
|
return;
|
|
130
132
|
}
|
|
131
133
|
if (!isFirst) {
|
|
132
|
-
void start(
|
|
134
|
+
void start('auto');
|
|
133
135
|
}
|
|
134
136
|
isFirst = false;
|
|
135
137
|
});
|
|
@@ -138,11 +140,11 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
138
140
|
if (immediate) {
|
|
139
141
|
if (EnvironmentUtil.isServer && server) {
|
|
140
142
|
untrack(() => {
|
|
141
|
-
void start(
|
|
143
|
+
void start('auto');
|
|
142
144
|
});
|
|
143
145
|
}
|
|
144
146
|
else if (EnvironmentUtil.isBrowser) {
|
|
145
|
-
void start(
|
|
147
|
+
void start('auto');
|
|
146
148
|
}
|
|
147
149
|
}
|
|
148
150
|
return {
|
|
@@ -161,7 +163,7 @@ export const createAsyncSignal = (handler, options = {}) => {
|
|
|
161
163
|
get ready() {
|
|
162
164
|
if (currentPromise)
|
|
163
165
|
return currentPromise;
|
|
164
|
-
return start(
|
|
166
|
+
return start('auto');
|
|
165
167
|
},
|
|
166
168
|
execute,
|
|
167
169
|
refresh: execute,
|