/
type.ts
158 lines (152 loc) · 4.71 KB
/
type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type {
Effect,
Event,
EventCallable,
EventPayload,
Store,
} from 'effector';
import type { SourcedField, FetchingStatus } from '../libs/patronus';
import type { CallObject } from './with_call_object';
interface DefaultMeta {
name: string;
}
export interface RemoteOperation<
Params,
Data,
Error,
Meta,
// eslint-disable-next-line @typescript-eslint/ban-types
ExtraLowLevelAPI = {}
> {
/**
* Reactive current request status
*
* + `initial` — the data has never been fetched yet
* + 'pending' — the data is being fetched right now
* + 'done' — the data has been successfully fetched
* + 'fail' — an error occurred while fetching data
*/
$status: Store<FetchingStatus>;
/** Is fetching started? */
$idle: Store<boolean>;
/** Is fetching in progress right now? */
$pending: Store<boolean>;
/** Is fetching failed? */
$failed: Store<boolean>;
/** Is fetching succeeded? */
$succeeded: Store<boolean>;
/** Is fetching finished? */
$finished: Store<boolean>;
/**
* Is operation enabled?
*
* + false — any `start` call will be ignored, event done.skip will be fired immediately
* + true — query will be executed after any `start` call
*/
$enabled: Store<boolean>;
/** Event to trigger operation */
start: EventCallable<Params>;
/** Event to reset the whole state of the operation */
reset: EventCallable<void>;
/** Event that trigered after operation started */
started: Event<{ params: Params; meta: ExecutionMeta }>;
aborted: Event<{ params: Params; meta: ExecutionMeta }>;
/** Set of events that represent end of query */
finished: {
/** Query was successfully ended, data will be passed as a payload */
success: Event<{ params: Params; result: Data; meta: ExecutionMeta }>;
/** Query was failed, error will be passed as a payload */
failure: Event<{ params: Params; error: Error; meta: ExecutionMeta }>;
/** Query execution was skipped due to `enabled` field in config */
skip: Event<{ params: Params; meta: ExecutionMeta }>;
/** Query was ended, it merges `success`, `error` and `skip` */
finally: Event<
{ params: Params; meta: ExecutionMeta } & (
| { status: 'done'; result: Data }
| { status: 'fail'; error: Error }
| { status: 'skip' }
)
>;
};
/**
* DO NOT USE THIS FIELD IN PRODUCTION
*
* It is internal operator details which is useful for testing.
*/
__: {
/**
* Internal effect, which executes to retrieve data.
*
* It must not be used in production. Please use it only for test purposes.
*
* @example
*
* import { locationQuery } from './location';
*
* test('some test', async () => {
* const scope = fork({
* handlers: [[locationQuery.__.executeFx, vi.fn()]]
* });
*
* //...test code
* })
*/
executeFx: Effect<any, any, any>;
/**
* Meta information about operation and its configuration.
*/
meta: Meta & DefaultMeta;
/**
* Distinguish different kinds of operations
*/
kind: unknown;
$latestParams: Store<Params | undefined>;
/**
* Low-level API, it can be changed anytime without any notice!
*/
lowLevelAPI: {
dataSources: Array<DataSource<Params>>;
dataSourceRetrieverFx: Effect<
{ params: Params; meta: ExecutionMeta },
{ result: unknown; stale: boolean },
any
>;
sourced: SourcedField<Params, unknown, unknown>[];
paramsAreMeaningless: boolean;
revalidate: EventCallable<{ params: Params; refresh: boolean }>;
pushData: EventCallable<Data>;
pushError: EventCallable<Error>;
startWithMeta: EventCallable<{ params: Params; meta: ExecutionMeta }>;
callObjectCreated: Event<CallObject>;
} & ExtraLowLevelAPI;
experimentalAPI?: {
attach: <Source, NewParams>(config: {
source: Store<Source>;
mapParams: (params: NewParams, source: Source) => Params;
}) => any;
};
};
}
export type RemoteOperationResult<
Q extends RemoteOperation<any, any, any, any>
> = EventPayload<Q['finished']['success']>['result'];
export type RemoteOperationError<
Q extends RemoteOperation<any, any, any, any>
> = EventPayload<Q['finished']['failure']>['error'];
export type RemoteOperationParams<
Q extends RemoteOperation<any, any, any, any>
> = EventPayload<Q['start']>;
export interface ExecutionMeta {
stopErrorPropagation: boolean;
stale: boolean;
}
export type DataSource<Params> = {
name: string;
get: Effect<
{ params: Params },
{ result: unknown; stale: boolean } | null,
unknown
>;
set?: Effect<{ params: Params; result: unknown }, void, unknown>;
unset?: Effect<{ params: Params }, void, unknown>;
};