1 | import type { PayloadAction, BaseActionCreator } from '../createAction';
|
2 | import type { Dispatch as ReduxDispatch, AnyAction, MiddlewareAPI, Middleware, Action as ReduxAction } from 'redux';
|
3 | import type { ThunkDispatch } from 'redux-thunk';
|
4 | import type { TaskAbortError } from './exceptions';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export declare type AbortSignalWithReason<T> = AbortSignal & {
|
10 | reason?: T;
|
11 | };
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export interface TypedActionCreator<Type extends string> {
|
17 | (...args: any[]): ReduxAction<Type>;
|
18 | type: Type;
|
19 | match: MatchFunction<any>;
|
20 | }
|
21 |
|
22 | export declare type AnyListenerPredicate<State> = (action: AnyAction, currentState: State, originalState: State) => boolean;
|
23 |
|
24 | export declare type ListenerPredicate<Action extends AnyAction, State> = (action: AnyAction, currentState: State, originalState: State) => action is Action;
|
25 |
|
26 | export interface ConditionFunction<State> {
|
27 | (predicate: AnyListenerPredicate<State>, timeout?: number): Promise<boolean>;
|
28 | (predicate: AnyListenerPredicate<State>, timeout?: number): Promise<boolean>;
|
29 | (predicate: () => boolean, timeout?: number): Promise<boolean>;
|
30 | }
|
31 |
|
32 | export declare type MatchFunction<T> = (v: any) => v is T;
|
33 |
|
34 | export interface ForkedTaskAPI {
|
35 | |
36 |
|
37 |
|
38 |
|
39 | pause<W>(waitFor: Promise<W>): Promise<W>;
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | delay(timeoutMs: number): Promise<void>;
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 | signal: AbortSignal;
|
52 | }
|
53 |
|
54 | export interface AsyncTaskExecutor<T> {
|
55 | (forkApi: ForkedTaskAPI): Promise<T>;
|
56 | }
|
57 |
|
58 | export interface SyncTaskExecutor<T> {
|
59 | (forkApi: ForkedTaskAPI): T;
|
60 | }
|
61 |
|
62 | export declare type ForkedTaskExecutor<T> = AsyncTaskExecutor<T> | SyncTaskExecutor<T>;
|
63 |
|
64 | export declare type TaskResolved<T> = {
|
65 | readonly status: 'ok';
|
66 | readonly value: T;
|
67 | };
|
68 |
|
69 | export declare type TaskRejected = {
|
70 | readonly status: 'rejected';
|
71 | readonly error: unknown;
|
72 | };
|
73 |
|
74 | export declare type TaskCancelled = {
|
75 | readonly status: 'cancelled';
|
76 | readonly error: TaskAbortError;
|
77 | };
|
78 |
|
79 | export declare type TaskResult<Value> = TaskResolved<Value> | TaskRejected | TaskCancelled;
|
80 |
|
81 | export interface ForkedTask<T> {
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | result: Promise<TaskResult<T>>;
|
96 | |
97 |
|
98 |
|
99 |
|
100 | cancel(): void;
|
101 | }
|
102 |
|
103 | export interface ForkOptions {
|
104 | |
105 |
|
106 |
|
107 |
|
108 | autoJoin: boolean;
|
109 | }
|
110 |
|
111 | export interface ListenerEffectAPI<State, Dispatch extends ReduxDispatch<AnyAction>, ExtraArgument = unknown> extends MiddlewareAPI<Dispatch, State> {
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | getOriginalState: () => State;
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 | unsubscribe(): void;
|
143 | |
144 |
|
145 |
|
146 | subscribe(): void;
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | condition: ConditionFunction<State>;
|
170 | |
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | take: TakePattern<State>;
|
195 | |
196 |
|
197 |
|
198 | cancelActiveListeners: () => void;
|
199 | |
200 |
|
201 |
|
202 |
|
203 |
|
204 | signal: AbortSignal;
|
205 | |
206 |
|
207 |
|
208 |
|
209 | delay(timeoutMs: number): Promise<void>;
|
210 | |
211 |
|
212 |
|
213 |
|
214 |
|
215 | fork<T>(executor: ForkedTaskExecutor<T>, options?: ForkOptions): ForkedTask<T>;
|
216 | |
217 |
|
218 |
|
219 |
|
220 |
|
221 | pause<M>(promise: Promise<M>): Promise<M>;
|
222 | extra: ExtraArgument;
|
223 | }
|
224 |
|
225 | export declare type ListenerEffect<Action extends AnyAction, State, Dispatch extends ReduxDispatch<AnyAction>, ExtraArgument = unknown> = (action: Action, api: ListenerEffectAPI<State, Dispatch, ExtraArgument>) => void | Promise<void>;
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | export interface ListenerErrorInfo {
|
231 | |
232 |
|
233 |
|
234 | raisedBy: 'effect' | 'predicate';
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | export interface ListenerErrorHandler {
|
243 | (error: unknown, errorInfo: ListenerErrorInfo): void;
|
244 | }
|
245 |
|
246 | export interface CreateListenerMiddlewareOptions<ExtraArgument = unknown> {
|
247 | extra?: ExtraArgument;
|
248 | |
249 |
|
250 |
|
251 | onError?: ListenerErrorHandler;
|
252 | }
|
253 |
|
254 | export declare type ListenerMiddleware<State = unknown, Dispatch extends ThunkDispatch<State, unknown, AnyAction> = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown> = Middleware<{
|
255 | (action: ReduxAction<'listenerMiddleware/add'>): UnsubscribeListener;
|
256 | }, State, Dispatch>;
|
257 |
|
258 | export interface ListenerMiddlewareInstance<State = unknown, Dispatch extends ThunkDispatch<State, unknown, AnyAction> = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown> {
|
259 | middleware: ListenerMiddleware<State, Dispatch, ExtraArgument>;
|
260 | startListening: AddListenerOverloads<UnsubscribeListener, State, Dispatch, ExtraArgument>;
|
261 | stopListening: RemoveListenerOverloads<State, Dispatch>;
|
262 | |
263 |
|
264 |
|
265 | clearListeners: () => void;
|
266 | }
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | export declare type TakePatternOutputWithoutTimeout<State, Predicate extends AnyListenerPredicate<State>> = Predicate extends MatchFunction<infer Action> ? Promise<[Action, State, State]> : Promise<[AnyAction, State, State]>;
|
272 |
|
273 | export declare type TakePatternOutputWithTimeout<State, Predicate extends AnyListenerPredicate<State>> = Predicate extends MatchFunction<infer Action> ? Promise<[Action, State, State] | null> : Promise<[AnyAction, State, State] | null>;
|
274 |
|
275 | export interface TakePattern<State> {
|
276 | <Predicate extends AnyListenerPredicate<State>>(predicate: Predicate): TakePatternOutputWithoutTimeout<State, Predicate>;
|
277 | <Predicate extends AnyListenerPredicate<State>>(predicate: Predicate, timeout: number): TakePatternOutputWithTimeout<State, Predicate>;
|
278 | <Predicate extends AnyListenerPredicate<State>>(predicate: Predicate, timeout?: number | undefined): TakePatternOutputWithTimeout<State, Predicate>;
|
279 | }
|
280 |
|
281 | export interface UnsubscribeListenerOptions {
|
282 | cancelActive?: true;
|
283 | }
|
284 |
|
285 | export declare type UnsubscribeListener = (unsubscribeOptions?: UnsubscribeListenerOptions) => void;
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | export interface AddListenerOverloads<Return, State = unknown, Dispatch extends ReduxDispatch = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown, AdditionalOptions = unknown> {
|
291 |
|
292 | <MA extends AnyAction, LP extends ListenerPredicate<MA, State>>(options: {
|
293 | actionCreator?: never;
|
294 | type?: never;
|
295 | matcher?: never;
|
296 | predicate: LP;
|
297 | effect: ListenerEffect<ListenerPredicateGuardedActionType<LP>, State, Dispatch, ExtraArgument>;
|
298 | } & AdditionalOptions): Return;
|
299 |
|
300 | <C extends TypedActionCreator<any>>(options: {
|
301 | actionCreator: C;
|
302 | type?: never;
|
303 | matcher?: never;
|
304 | predicate?: never;
|
305 | effect: ListenerEffect<ReturnType<C>, State, Dispatch, ExtraArgument>;
|
306 | } & AdditionalOptions): Return;
|
307 |
|
308 | <T extends string>(options: {
|
309 | actionCreator?: never;
|
310 | type: T;
|
311 | matcher?: never;
|
312 | predicate?: never;
|
313 | effect: ListenerEffect<ReduxAction<T>, State, Dispatch, ExtraArgument>;
|
314 | } & AdditionalOptions): Return;
|
315 |
|
316 | <MA extends AnyAction, M extends MatchFunction<MA>>(options: {
|
317 | actionCreator?: never;
|
318 | type?: never;
|
319 | matcher: M;
|
320 | predicate?: never;
|
321 | effect: ListenerEffect<GuardedType<M>, State, Dispatch, ExtraArgument>;
|
322 | } & AdditionalOptions): Return;
|
323 |
|
324 | <LP extends AnyListenerPredicate<State>>(options: {
|
325 | actionCreator?: never;
|
326 | type?: never;
|
327 | matcher?: never;
|
328 | predicate: LP;
|
329 | effect: ListenerEffect<AnyAction, State, Dispatch, ExtraArgument>;
|
330 | } & AdditionalOptions): Return;
|
331 | }
|
332 |
|
333 | export declare type RemoveListenerOverloads<State = unknown, Dispatch extends ReduxDispatch = ThunkDispatch<State, unknown, AnyAction>> = AddListenerOverloads<boolean, State, Dispatch, any, UnsubscribeListenerOptions>;
|
334 |
|
335 | export interface RemoveListenerAction<Action extends AnyAction, State, Dispatch extends ReduxDispatch<AnyAction>> {
|
336 | type: 'listenerMiddleware/remove';
|
337 | payload: {
|
338 | type: string;
|
339 | listener: ListenerEffect<Action, State, Dispatch>;
|
340 | };
|
341 | }
|
342 |
|
343 |
|
344 |
|
345 | export declare type TypedAddListener<State, Dispatch extends ReduxDispatch<AnyAction> = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown, Payload = ListenerEntry<State, Dispatch>, T extends string = 'listenerMiddleware/add'> = BaseActionCreator<Payload, T> & AddListenerOverloads<PayloadAction<Payload, T>, State, Dispatch, ExtraArgument>;
|
346 |
|
347 |
|
348 |
|
349 | export declare type TypedRemoveListener<State, Dispatch extends ReduxDispatch<AnyAction> = ThunkDispatch<State, unknown, AnyAction>, Payload = ListenerEntry<State, Dispatch>, T extends string = 'listenerMiddleware/remove'> = BaseActionCreator<Payload, T> & AddListenerOverloads<PayloadAction<Payload, T>, State, Dispatch, any, UnsubscribeListenerOptions>;
|
350 |
|
351 |
|
352 |
|
353 | export declare type TypedStartListening<State, Dispatch extends ReduxDispatch<AnyAction> = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown> = AddListenerOverloads<UnsubscribeListener, State, Dispatch, ExtraArgument>;
|
354 |
|
355 |
|
356 | export declare type TypedStopListening<State, Dispatch extends ReduxDispatch<AnyAction> = ThunkDispatch<State, unknown, AnyAction>> = RemoveListenerOverloads<State, Dispatch>;
|
357 |
|
358 |
|
359 | export declare type TypedCreateListenerEntry<State, Dispatch extends ReduxDispatch<AnyAction> = ThunkDispatch<State, unknown, AnyAction>> = AddListenerOverloads<ListenerEntry<State, Dispatch>, State, Dispatch>;
|
360 |
|
361 |
|
362 |
|
363 |
|
364 | export declare type ListenerEntry<State = unknown, Dispatch extends ReduxDispatch<AnyAction> = ReduxDispatch<AnyAction>> = {
|
365 | id: string;
|
366 | effect: ListenerEffect<any, State, Dispatch>;
|
367 | unsubscribe: () => void;
|
368 | pending: Set<AbortController>;
|
369 | type?: string;
|
370 | predicate: ListenerPredicate<AnyAction, State>;
|
371 | };
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | export declare type FallbackAddListenerOptions = {
|
377 | actionCreator?: TypedActionCreator<string>;
|
378 | type?: string;
|
379 | matcher?: MatchFunction<any>;
|
380 | predicate?: ListenerPredicate<any, any>;
|
381 | } & {
|
382 | effect: ListenerEffect<any, any, any>;
|
383 | };
|
384 |
|
385 |
|
386 |
|
387 |
|
388 | export declare type GuardedType<T> = T extends (x: any, ...args: unknown[]) => x is infer T ? T : never;
|
389 |
|
390 | export declare type ListenerPredicateGuardedActionType<T> = T extends ListenerPredicate<infer Action, any> ? Action : never;
|