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 ListenerEffectAPI<State, Dispatch extends ReduxDispatch<AnyAction>, ExtraArgument = unknown> extends MiddlewareAPI<Dispatch, State> {
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | getOriginalState: () => State;
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 | unsubscribe(): void;
|
135 | |
136 |
|
137 |
|
138 | subscribe(): void;
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | condition: ConditionFunction<State>;
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | take: TakePattern<State>;
|
187 | |
188 |
|
189 |
|
190 | cancelActiveListeners: () => void;
|
191 | |
192 |
|
193 |
|
194 |
|
195 |
|
196 | signal: AbortSignal;
|
197 | |
198 |
|
199 |
|
200 |
|
201 | delay(timeoutMs: number): Promise<void>;
|
202 | |
203 |
|
204 |
|
205 |
|
206 | fork<T>(executor: ForkedTaskExecutor<T>): ForkedTask<T>;
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 | pause<M>(promise: Promise<M>): Promise<M>;
|
213 | extra: ExtraArgument;
|
214 | }
|
215 |
|
216 | export declare type ListenerEffect<Action extends AnyAction, State, Dispatch extends ReduxDispatch<AnyAction>, ExtraArgument = unknown> = (action: Action, api: ListenerEffectAPI<State, Dispatch, ExtraArgument>) => void | Promise<void>;
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | export interface ListenerErrorInfo {
|
222 | |
223 |
|
224 |
|
225 | raisedBy: 'effect' | 'predicate';
|
226 | }
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | export interface ListenerErrorHandler {
|
234 | (error: unknown, errorInfo: ListenerErrorInfo): void;
|
235 | }
|
236 |
|
237 | export interface CreateListenerMiddlewareOptions<ExtraArgument = unknown> {
|
238 | extra?: ExtraArgument;
|
239 | |
240 |
|
241 |
|
242 | onError?: ListenerErrorHandler;
|
243 | }
|
244 |
|
245 | export declare type ListenerMiddleware<State = unknown, Dispatch extends ThunkDispatch<State, unknown, AnyAction> = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown> = Middleware<{
|
246 | (action: ReduxAction<'listenerMiddleware/add'>): UnsubscribeListener;
|
247 | }, State, Dispatch>;
|
248 |
|
249 | export interface ListenerMiddlewareInstance<State = unknown, Dispatch extends ThunkDispatch<State, unknown, AnyAction> = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown> {
|
250 | middleware: ListenerMiddleware<State, Dispatch, ExtraArgument>;
|
251 | startListening: AddListenerOverloads<UnsubscribeListener, State, Dispatch, ExtraArgument>;
|
252 | stopListening: RemoveListenerOverloads<State, Dispatch>;
|
253 | |
254 |
|
255 |
|
256 | clearListeners: () => void;
|
257 | }
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | export declare type TakePatternOutputWithoutTimeout<State, Predicate extends AnyListenerPredicate<State>> = Predicate extends MatchFunction<infer Action> ? Promise<[Action, State, State]> : Promise<[AnyAction, State, State]>;
|
263 |
|
264 | export declare type TakePatternOutputWithTimeout<State, Predicate extends AnyListenerPredicate<State>> = Predicate extends MatchFunction<infer Action> ? Promise<[Action, State, State] | null> : Promise<[AnyAction, State, State] | null>;
|
265 |
|
266 | export interface TakePattern<State> {
|
267 | <Predicate extends AnyListenerPredicate<State>>(predicate: Predicate): TakePatternOutputWithoutTimeout<State, Predicate>;
|
268 | <Predicate extends AnyListenerPredicate<State>>(predicate: Predicate, timeout: number): TakePatternOutputWithTimeout<State, Predicate>;
|
269 | <Predicate extends AnyListenerPredicate<State>>(predicate: Predicate, timeout?: number | undefined): TakePatternOutputWithTimeout<State, Predicate>;
|
270 | }
|
271 |
|
272 | export interface UnsubscribeListenerOptions {
|
273 | cancelActive?: true;
|
274 | }
|
275 |
|
276 | export declare type UnsubscribeListener = (unsubscribeOptions?: UnsubscribeListenerOptions) => void;
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | export interface AddListenerOverloads<Return, State = unknown, Dispatch extends ReduxDispatch = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown, AdditionalOptions = unknown> {
|
282 |
|
283 | <MA extends AnyAction, LP extends ListenerPredicate<MA, State>>(options: {
|
284 | actionCreator?: never;
|
285 | type?: never;
|
286 | matcher?: never;
|
287 | predicate: LP;
|
288 | effect: ListenerEffect<ListenerPredicateGuardedActionType<LP>, State, Dispatch, ExtraArgument>;
|
289 | } & AdditionalOptions): Return;
|
290 |
|
291 | <C extends TypedActionCreator<any>>(options: {
|
292 | actionCreator: C;
|
293 | type?: never;
|
294 | matcher?: never;
|
295 | predicate?: never;
|
296 | effect: ListenerEffect<ReturnType<C>, State, Dispatch, ExtraArgument>;
|
297 | } & AdditionalOptions): Return;
|
298 |
|
299 | <T extends string>(options: {
|
300 | actionCreator?: never;
|
301 | type: T;
|
302 | matcher?: never;
|
303 | predicate?: never;
|
304 | effect: ListenerEffect<ReduxAction<T>, State, Dispatch, ExtraArgument>;
|
305 | } & AdditionalOptions): Return;
|
306 |
|
307 | <MA extends AnyAction, M extends MatchFunction<MA>>(options: {
|
308 | actionCreator?: never;
|
309 | type?: never;
|
310 | matcher: M;
|
311 | predicate?: never;
|
312 | effect: ListenerEffect<GuardedType<M>, State, Dispatch, ExtraArgument>;
|
313 | } & AdditionalOptions): Return;
|
314 |
|
315 | <LP extends AnyListenerPredicate<State>>(options: {
|
316 | actionCreator?: never;
|
317 | type?: never;
|
318 | matcher?: never;
|
319 | predicate: LP;
|
320 | effect: ListenerEffect<AnyAction, State, Dispatch, ExtraArgument>;
|
321 | } & AdditionalOptions): Return;
|
322 | }
|
323 |
|
324 | export declare type RemoveListenerOverloads<State = unknown, Dispatch extends ReduxDispatch = ThunkDispatch<State, unknown, AnyAction>> = AddListenerOverloads<boolean, State, Dispatch, any, UnsubscribeListenerOptions>;
|
325 |
|
326 | export interface RemoveListenerAction<Action extends AnyAction, State, Dispatch extends ReduxDispatch<AnyAction>> {
|
327 | type: 'listenerMiddleware/remove';
|
328 | payload: {
|
329 | type: string;
|
330 | listener: ListenerEffect<Action, State, Dispatch>;
|
331 | };
|
332 | }
|
333 |
|
334 |
|
335 |
|
336 | 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>;
|
337 |
|
338 |
|
339 |
|
340 | 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>;
|
341 |
|
342 |
|
343 |
|
344 | export declare type TypedStartListening<State, Dispatch extends ReduxDispatch<AnyAction> = ThunkDispatch<State, unknown, AnyAction>, ExtraArgument = unknown> = AddListenerOverloads<UnsubscribeListener, State, Dispatch, ExtraArgument>;
|
345 |
|
346 |
|
347 | export declare type TypedStopListening<State, Dispatch extends ReduxDispatch<AnyAction> = ThunkDispatch<State, unknown, AnyAction>> = RemoveListenerOverloads<State, Dispatch>;
|
348 |
|
349 |
|
350 | export declare type TypedCreateListenerEntry<State, Dispatch extends ReduxDispatch<AnyAction> = ThunkDispatch<State, unknown, AnyAction>> = AddListenerOverloads<ListenerEntry<State, Dispatch>, State, Dispatch>;
|
351 |
|
352 |
|
353 |
|
354 |
|
355 | export declare type ListenerEntry<State = unknown, Dispatch extends ReduxDispatch<AnyAction> = ReduxDispatch<AnyAction>> = {
|
356 | id: string;
|
357 | effect: ListenerEffect<any, State, Dispatch>;
|
358 | unsubscribe: () => void;
|
359 | pending: Set<AbortController>;
|
360 | type?: string;
|
361 | predicate: ListenerPredicate<AnyAction, State>;
|
362 | };
|
363 |
|
364 |
|
365 |
|
366 |
|
367 | export declare type FallbackAddListenerOptions = {
|
368 | actionCreator?: TypedActionCreator<string>;
|
369 | type?: string;
|
370 | matcher?: MatchFunction<any>;
|
371 | predicate?: ListenerPredicate<any, any>;
|
372 | } & {
|
373 | effect: ListenerEffect<any, any, any>;
|
374 | };
|
375 |
|
376 |
|
377 |
|
378 |
|
379 | export declare type GuardedType<T> = T extends (x: any, ...args: unknown[]) => x is infer T ? T : never;
|
380 |
|
381 | export declare type ListenerPredicateGuardedActionType<T> = T extends ListenerPredicate<infer Action, any> ? Action : never;
|