-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.d.ts
321 lines (260 loc) · 7.83 KB
/
index.d.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
159
160
161
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* connect react components with wire
*
* @packageDocumentation
*/
import { DependencyList } from 'react';
import { Dispatch } from 'react';
import { SetStateAction } from 'react';
declare type CB<T> = (t: T) => void;
declare type CovarianceGuard<T> = [T, unknown];
export declare function createSelector<V, Fns extends {} = {}>(
options: WritableSelectorOptions<V>,
): Wire<V, Fns>;
export declare function createSelector<V, Fns extends {} = {}>(
options: ReadOnlySelectorOptions<V>,
): ReadonlyWire<V, Fns>;
export declare function createWire<V, Fns extends {} = {}>(
initialValue: V,
): Wire<V, Fns>;
export declare type Defined<T> = T extends undefined ? never : T;
declare type Fn = (...args: any) => any;
export declare interface FnsWire<Fns extends {}> extends FnsWireGuard<Fns> {
fn: <K extends KeyOfMethods<Fns>>(name: K, value: Fns[K]) => () => void;
fns: Methods<Fns>;
}
declare interface FnsWireGuard<Fns> {
/**
* @internal
* Covariance hack:
* never use this variable.
* don't remove space at the start
*/
' fns-wire': StrictMethodsGuard<Fns>;
}
export declare function getLinkIds(wire: undefined): undefined;
export declare function getLinkIds(wire: WireId): LinkIds;
export declare function getLinkIds(
wire: WireId | undefined,
): LinkIds | undefined;
export declare function getWireId(wire: undefined): undefined;
export declare function getWireId(wire: WireId): string;
export declare function getWireId(wire: WireId | undefined): string | undefined;
declare type GetWireValue = <V>(wire: ReadonlyStateWire<V>) => V;
declare type InitializerOrValue<V> = V | (() => V);
export declare type Interceptor<Value> = (
nextValue: Defined<Value>,
preValue: Value,
) => Value | undefined;
/**
* isDefined check if the value is undefined or not.
* it helps with typescript generic types.
* @param value -
*/
export declare const isDefined: <V>(
value: V | undefined,
) => value is Defined<V>;
declare type IsNever<T> = [T] extends [never] ? true : false;
declare type KeyOfMethods<T> = NonNever<keyof Methods<T>>;
export declare type LinkIds = Array<string | LinkIds>;
declare type MethodKeys<T> = {
[P in keyof T]: T[P] extends Fn ? P : never;
}[keyof T];
declare type Methods<T> = Pick<T, MethodKeys<T>>;
declare type NonNever<T> = IsNever<T> extends true ? any : T;
export declare interface ReadOnlySelectorOptions<V> {
get: (options: { get: GetWireValue }) => V;
}
export declare interface ReadonlyStateWire<V>
extends ReadonlyStateWireGuard<V>,
WireId {
/**
* get current value
* @returns current value
*/
getValue(): V;
/**
* subscribe for value change
* @param callback -
* @returns unsubscribe function
*/
subscribe(callback: (value: Defined<V>) => void): () => void;
}
declare interface ReadonlyStateWireGuard<V> {
' state-wire': CovarianceGuard<V>;
}
export declare type ReadonlyWire<
V,
Fns extends {} = {},
> = ReadonlyStateWire<V> & FnsWire<Fns>;
declare type SetWireValue = <V>(wire: StateWire<V>, value: Defined<V>) => void;
export declare interface StateWire<V> extends StateWireGuard<V>, WireId {
/**
* get current value
* @returns current value
*/
getValue(): V;
/**
* set value
* @param value -
*/
setValue(value: Defined<V>): void;
/**
* subscribe for value change
* @param callback -
* @returns unsubscribe function
*/
subscribe(callback: (value: Defined<V>) => void): () => void;
}
declare interface StateWireGuard<V> {
' state-wire': StrictGuard<V>;
}
declare type StrictGuard<T> = [T, CB<T>];
declare type StrictMethodsGuard<Fns> = {
[P in keyof Methods<Fns>]: StrictGuard<Fns[P]>;
};
/**
*
* subscribe for function calls
*
* @param wire -
* @param name - name of `fns` function
* @param fn - memoized callback function
*
* @example
```ts
// subscribe for `sample` function call
useFn(
wire,
'sample',
useCallback(value => {
console.log(value);
}, []),
);
// call `sample` function
wire.fns.sample(5);
```
*/
export declare function useFn<Fns extends {}, K extends KeyOfMethods<Fns>>(
wire: FnsWire<Fns> | null | undefined,
name: K,
fn: Fns[K],
): void;
/**
* returns new wire and intercepting setValue of returned wire
*
* @param wire - up-link wire
* @param interceptor - interceptor function
* @returns new intercepted wire
*
* @remarks
*
* the interceptor function gets the next value and previous value and returns a value. the returned value be set on
* up-link wire.
* if interceptor function returns `undefined` or the same value as previous value, up-link wire value will not change.
*
* @example
*
* ```tsx
* const valueWire = useInterceptor(
* props.valueWire,
* useCallback(
* (nextValue, preValue) => (props.submittingWire.getValue() ? preValue : nextValue),
* [props.submittingWire]
* )
* );
* ```
*
*/
export declare function useInterceptor<W extends StateWire<any>>(
wire: W,
interceptor: Interceptor<WireState<W>>,
): W;
export declare function useSelector<V, Fns extends {} = {}>(
options: WritableSelectorOptions<V>,
deps?: DependencyList,
): Wire<V, Fns>;
export declare function useSelector<V, Fns extends {} = {}>(
options: ReadOnlySelectorOptions<V>,
deps?: DependencyList,
): ReadonlyWire<V, Fns>;
export declare function useSubscribe<V>(
wire: ReadonlyStateWire<V>,
callback: (value: Defined<V>) => void,
): void;
export declare function useWire<V, Fns extends {} = {}>(
upLink: Wire<V, Fns>,
): Wire<V, Fns>;
export declare function useWire<V, Fns extends {} = {}>(
upLink: Wire<V | undefined, Fns> | null | undefined,
initialValue: InitializerOrValue<V>,
): Wire<V, Fns>;
export declare function useWire<V, Fns extends {} = {}>(
upLink: Wire<V, Fns> | null | undefined,
initialValue: InitializerOrValue<V>,
): Wire<V, Fns>;
export declare function useWire<V, Fns extends {} = {}>(
upLink: Wire<V, Fns> | null | undefined,
initialValue?: InitializerOrValue<V | undefined>,
): Wire<V | undefined, Fns>;
export declare function useWireState<V>(
upLink: StateWire<V>,
): valueAndAction<V>;
export declare function useWireState<V>(
wire: StateWire<V | undefined> | null | undefined,
initialValue: InitializerOrValue<V>,
): valueAndAction<V>;
export declare function useWireState<V>(
wire: StateWire<V> | null | undefined,
initialValue: InitializerOrValue<V>,
): valueAndAction<V>;
export declare function useWireState<V>(
wire: StateWire<V> | null | undefined,
initialValue?: InitializerOrValue<V | undefined>,
): valueAndAction<V | undefined>;
export declare function useWireValue(
wire: null | undefined,
defaultValue?: unknown,
): undefined;
export declare function useWireValue<W extends ReadonlyStateWire<any>>(
wire: W,
): WireState<W>;
export declare function useWireValue<W extends ReadonlyStateWire<any>>(
wire: W | null | undefined,
defaultValue: Defined<WireState<W>>,
): Defined<WireState<W>>;
export declare function useWireValue<W extends ReadonlyStateWire<any>>(
wire: W,
defaultValue?: WireState<W> | undefined,
): WireState<W>;
export declare function useWireValue<W extends ReadonlyStateWire<any>>(
wire: W | null | undefined,
defaultValue?: WireState<W>,
): WireState<W> | undefined;
declare type valueAndAction<V> = [V, Dispatch<SetStateAction<Defined<V>>>];
export declare type Wire<V, Fns extends {} = {}> = StateWire<V> & FnsWire<Fns>;
export declare type WireFns<W extends FnsWire<any>> =
W extends FnsWire<infer Fns> ? Fns : never;
export declare interface WireId {
/**
* @internal
*/
_getId(): string;
/**
* @internal
*/
_getLinkIds(): LinkIds;
}
export declare type WireState<W extends ReadonlyStateWire<any>> =
W extends ReadonlyStateWire<infer V> ? V : never;
export declare interface WritableSelectorOptions<V> {
get: (options: { get: GetWireValue }) => V;
set: (
options: {
get: GetWireValue;
set: SetWireValue;
},
value: V,
) => void;
}
export {};