@talismn/util 0.5.5 → 0.5.7
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.
- package/dist/index.d.mts +270 -0
- package/dist/index.d.ts +270 -0
- package/dist/index.js +490 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +421 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +23 -19
- package/dist/declarations/src/BigMath.d.ts +0 -10
- package/dist/declarations/src/FunctionPropertyNames.d.ts +0 -8
- package/dist/declarations/src/Prettify.d.ts +0 -3
- package/dist/declarations/src/addTrailingSlash.d.ts +0 -1
- package/dist/declarations/src/classNames.d.ts +0 -2
- package/dist/declarations/src/deferred.d.ts +0 -15
- package/dist/declarations/src/firstThenDebounce.d.ts +0 -8
- package/dist/declarations/src/formatDecimals.d.ts +0 -12
- package/dist/declarations/src/formatPrice.d.ts +0 -1
- package/dist/declarations/src/getLoadable.d.ts +0 -25
- package/dist/declarations/src/getQuery.d.ts +0 -45
- package/dist/declarations/src/getSharedObservable.d.ts +0 -13
- package/dist/declarations/src/hasOwnProperty.d.ts +0 -1
- package/dist/declarations/src/index.d.ts +0 -31
- package/dist/declarations/src/isAbortError.d.ts +0 -1
- package/dist/declarations/src/isArrayOf.d.ts +0 -1
- package/dist/declarations/src/isAscii.d.ts +0 -1
- package/dist/declarations/src/isBigInt.d.ts +0 -1
- package/dist/declarations/src/isBooleanTrue.d.ts +0 -1
- package/dist/declarations/src/isHexString.d.ts +0 -3
- package/dist/declarations/src/isNotNil.d.ts +0 -9
- package/dist/declarations/src/isPromise.d.ts +0 -1
- package/dist/declarations/src/isSubject.d.ts +0 -5
- package/dist/declarations/src/isTruthy.d.ts +0 -1
- package/dist/declarations/src/keepAlive.d.ts +0 -17
- package/dist/declarations/src/planckToTokens.d.ts +0 -3
- package/dist/declarations/src/replaySubjectFrom.d.ts +0 -12
- package/dist/declarations/src/sleep.d.ts +0 -1
- package/dist/declarations/src/splitSubject.d.ts +0 -11
- package/dist/declarations/src/throwAfter.d.ts +0 -1
- package/dist/declarations/src/tokensToPlanck.d.ts +0 -3
- package/dist/declarations/src/validateHexString.d.ts +0 -11
- package/dist/talismn-util.cjs.d.ts +0 -1
- package/dist/talismn-util.cjs.dev.js +0 -498
- package/dist/talismn-util.cjs.js +0 -7
- package/dist/talismn-util.cjs.prod.js +0 -498
- package/dist/talismn-util.esm.js +0 -461
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import * as tailwind_merge from 'tailwind-merge';
|
|
2
|
+
import { OperatorFunction, Observable, Subject, ReplaySubject } from 'rxjs';
|
|
3
|
+
import BigNumber from 'bignumber.js';
|
|
4
|
+
|
|
5
|
+
declare const addTrailingSlash: (url: string) => string;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Javascript's `Math` library for `BigInt`.
|
|
9
|
+
* Taken from https://stackoverflow.com/questions/51867270/is-there-a-library-similar-to-math-that-supports-javascript-bigint/64953280#64953280
|
|
10
|
+
*/
|
|
11
|
+
declare const BigMath: {
|
|
12
|
+
abs(x: bigint): bigint;
|
|
13
|
+
sign(x: bigint): 0n | 1n | -1n;
|
|
14
|
+
min(value: bigint, ...values: bigint[]): bigint;
|
|
15
|
+
max(value: bigint, ...values: bigint[]): bigint;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
declare const classNames: (...classLists: tailwind_merge.ClassNameValue[]) => string;
|
|
19
|
+
declare const cn: (...classLists: tailwind_merge.ClassNameValue[]) => string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* In TypeScript, a deferred promise refers to a pattern that involves creating a promise that can be
|
|
23
|
+
* resolved or rejected at a later point in time, typically by code outside of the current function scope.
|
|
24
|
+
*
|
|
25
|
+
* This pattern is often used when dealing with asynchronous operations that involve multiple steps or when
|
|
26
|
+
* the result of an operation cannot be immediately determined.
|
|
27
|
+
*/
|
|
28
|
+
declare function Deferred<T>(): {
|
|
29
|
+
promise: Promise<T>;
|
|
30
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
31
|
+
reject: (reason?: unknown) => void;
|
|
32
|
+
isPending: () => boolean;
|
|
33
|
+
isResolved: () => boolean;
|
|
34
|
+
isRejected: () => boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** biome-ignore-all lint/complexity/noBannedTypes: legacy */
|
|
38
|
+
type FunctionPropertyNames<T> = {
|
|
39
|
+
[K in keyof T]: T[K] extends Function ? K : never;
|
|
40
|
+
}[keyof T];
|
|
41
|
+
type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;
|
|
42
|
+
type NonFunctionPropertyNames<T> = {
|
|
43
|
+
[K in keyof T]: T[K] extends Function ? never : K;
|
|
44
|
+
}[keyof T];
|
|
45
|
+
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* An rxjs operator which:
|
|
49
|
+
*
|
|
50
|
+
* 1. Emits the first value it receives from the source observable, then:
|
|
51
|
+
* 2. Debounces any future values by `timeout` ms.
|
|
52
|
+
*/
|
|
53
|
+
declare const firstThenDebounce: <T>(timeout: number) => OperatorFunction<T, T>;
|
|
54
|
+
|
|
55
|
+
declare const MAX_DECIMALS_FORMAT = 12;
|
|
56
|
+
/**
|
|
57
|
+
* Custom decimal number formatting for Talisman
|
|
58
|
+
* note that the NumberFormat().format() call is the ressource heavy part, it's not worth trying to optimize other parts
|
|
59
|
+
* @param num input number
|
|
60
|
+
* @param digits number of significant digits to display
|
|
61
|
+
* @param locale locale used to format the number
|
|
62
|
+
* @param options formatting options
|
|
63
|
+
* @returns the formatted value
|
|
64
|
+
*/
|
|
65
|
+
declare const formatDecimals: (num?: string | number | null | BigNumber, digits?: number, options?: Partial<Intl.NumberFormatOptions>, locale?: string) => string;
|
|
66
|
+
|
|
67
|
+
declare const formatPrice: (price: number, currency: string, compact: boolean) => string;
|
|
68
|
+
|
|
69
|
+
type LoadableError = {
|
|
70
|
+
name: string;
|
|
71
|
+
message: string;
|
|
72
|
+
};
|
|
73
|
+
type Loadable<T = unknown> = {
|
|
74
|
+
status: "loading";
|
|
75
|
+
data?: T;
|
|
76
|
+
error?: undefined;
|
|
77
|
+
} | {
|
|
78
|
+
status: "success";
|
|
79
|
+
data: T;
|
|
80
|
+
error?: undefined;
|
|
81
|
+
} | {
|
|
82
|
+
status: "error";
|
|
83
|
+
data?: T;
|
|
84
|
+
error: LoadableError;
|
|
85
|
+
};
|
|
86
|
+
type LoadableStatus = Loadable["status"];
|
|
87
|
+
type LoadableOptions = {
|
|
88
|
+
getError?: (error: unknown) => LoadableError;
|
|
89
|
+
refreshInterval?: number;
|
|
90
|
+
};
|
|
91
|
+
declare function getLoadable$<T>(factory: () => Promise<T>, options?: LoadableOptions): Observable<Loadable<T>>;
|
|
92
|
+
|
|
93
|
+
type GetLoadableQueryParams<TArgs extends unknown[], TResult> = {
|
|
94
|
+
namespace: string;
|
|
95
|
+
args: TArgs;
|
|
96
|
+
queryFn: (args: TArgs, signal: AbortSignal) => Promise<TResult>;
|
|
97
|
+
refreshInterval?: number;
|
|
98
|
+
defaultValue?: TResult;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Thin wrapper around getQuery$ that returns Loadable<T> and optionally
|
|
102
|
+
* primes the stream with a loading state using the provided default value.
|
|
103
|
+
*
|
|
104
|
+
* TODO: consolidate with getQuery$
|
|
105
|
+
*/
|
|
106
|
+
declare const getLoadableQuery$: <TArgs extends unknown[], TResult>(params: GetLoadableQueryParams<TArgs, TResult>) => Observable<Loadable<TResult>>;
|
|
107
|
+
|
|
108
|
+
type QueryStatus = "loading" | "loaded" | "error";
|
|
109
|
+
type QueryResult<T, S extends QueryStatus = "loading" | "loaded" | "error"> = S extends "loading" ? {
|
|
110
|
+
status: "loading";
|
|
111
|
+
data: T | undefined;
|
|
112
|
+
error: undefined;
|
|
113
|
+
} : S extends "loaded" ? {
|
|
114
|
+
status: "loaded";
|
|
115
|
+
data: T;
|
|
116
|
+
error: undefined;
|
|
117
|
+
} : {
|
|
118
|
+
status: "error";
|
|
119
|
+
data: undefined;
|
|
120
|
+
error: unknown;
|
|
121
|
+
};
|
|
122
|
+
type QueryOptions<Output, Args> = {
|
|
123
|
+
namespace: string;
|
|
124
|
+
args: Args;
|
|
125
|
+
queryFn: (args: Args, signal: AbortSignal) => Promise<Output>;
|
|
126
|
+
defaultValue?: Output;
|
|
127
|
+
refreshInterval?: number;
|
|
128
|
+
serializer?: (args: Args) => string;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Creates a shared observable for executing queries with caching, loading states, and automatic refresh capabilities.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const userQuery$ = getQuery$({
|
|
136
|
+
* namespace: 'users',
|
|
137
|
+
* args: { userId: 123 },
|
|
138
|
+
* queryFn: async ({ userId }) => fetchUser(userId),
|
|
139
|
+
* defaultValue: null,
|
|
140
|
+
* refreshInterval: 30000
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* userQuery$.subscribe(result => {
|
|
144
|
+
* if (result.status === 'loaded') {
|
|
145
|
+
* console.log(result.data);
|
|
146
|
+
* }
|
|
147
|
+
* });
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* @deprecated use getLoadableQuery$ instead
|
|
151
|
+
*/
|
|
152
|
+
declare const getQuery$: <Output, Args>({ namespace, args, queryFn, defaultValue, refreshInterval, serializer, }: QueryOptions<Output, Args>) => Observable<QueryResult<Output>>;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* When using react-rxjs hooks and state observables, the options are used as weak map keys.
|
|
156
|
+
* This means that if the options object is recreated on each render, the observable will be recreated as well.
|
|
157
|
+
* This utility function allows you to create a shared observable based on a namespace and arguments that, so react-rxjs can reuse the same observables
|
|
158
|
+
*
|
|
159
|
+
* @param namespace
|
|
160
|
+
* @param args
|
|
161
|
+
* @param createObservable
|
|
162
|
+
* @param serializer
|
|
163
|
+
* @returns
|
|
164
|
+
*/
|
|
165
|
+
declare const getSharedObservable: <Args, Output, ObsOutput = Observable<Output>>(namespace: string, args: Args, createObservable: (args: Args) => ObsOutput, serializer?: (args: Args) => string) => ObsOutput;
|
|
166
|
+
|
|
167
|
+
declare function hasOwnProperty<X, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;
|
|
168
|
+
|
|
169
|
+
declare const isAbortError: (error: unknown) => boolean;
|
|
170
|
+
|
|
171
|
+
declare function isArrayOf<T, P extends Array<unknown>>(array: unknown[], func: new (...args: P) => T): array is T[];
|
|
172
|
+
|
|
173
|
+
declare const isAscii: (str: string) => boolean;
|
|
174
|
+
|
|
175
|
+
declare const isBigInt: (value: unknown) => value is bigint;
|
|
176
|
+
|
|
177
|
+
declare const isBooleanTrue: <T>(x: T | null | undefined) => x is T;
|
|
178
|
+
|
|
179
|
+
type HexString = `0x${string}`;
|
|
180
|
+
declare const REGEX_HEX_STRING: RegExp;
|
|
181
|
+
declare const isHexString: (value: unknown) => value is HexString;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* WARNING: This function only checks against null or undefined, it does not coerce the value.
|
|
185
|
+
* ie: false and 0 are considered not nil
|
|
186
|
+
* Use isTruthy instead for a regular coercion check.
|
|
187
|
+
*
|
|
188
|
+
* @param value
|
|
189
|
+
* @returns whether the value is neither null nor undefined
|
|
190
|
+
*/
|
|
191
|
+
declare const isNotNil: <T>(value: T | null | undefined) => value is T;
|
|
192
|
+
|
|
193
|
+
declare const isPromise: <T = any>(value: any) => value is Promise<T>;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Tests to see if an object is an RxJS {@link Subject}.
|
|
197
|
+
*/
|
|
198
|
+
declare function isSubject<T>(object?: Subject<T> | object): object is Subject<T>;
|
|
199
|
+
|
|
200
|
+
declare const isTruthy: <T>(value: T | null | undefined) => value is T;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* An RxJS operator that keeps the source observable alive for a specified duration
|
|
204
|
+
* after all subscribers have unsubscribed. This prevents expensive re-subscriptions
|
|
205
|
+
* when subscribers come and go frequently.
|
|
206
|
+
*
|
|
207
|
+
* @param keepAliveMs - Duration in milliseconds to keep the source alive after last unsubscription
|
|
208
|
+
* @returns MonoTypeOperatorFunction that can be used in pipe()
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```typescript
|
|
212
|
+
* const data$ = expensive_api_call$.pipe(
|
|
213
|
+
* keepAlive(3000) // Keep alive for 3 seconds
|
|
214
|
+
* );
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
declare const keepAlive: <T>(timeout: number) => OperatorFunction<T, T>;
|
|
218
|
+
|
|
219
|
+
type Prettify<T> = {
|
|
220
|
+
[K in keyof T]: T[K];
|
|
221
|
+
} & {};
|
|
222
|
+
|
|
223
|
+
declare function planckToTokens(planck: string, tokenDecimals: number): string;
|
|
224
|
+
declare function planckToTokens(planck: string, tokenDecimals?: number): string | undefined;
|
|
225
|
+
declare function planckToTokens(planck?: string, tokenDecimals?: number): string | undefined;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Turns a value into a {@link ReplaySubject} of size 1.
|
|
229
|
+
*
|
|
230
|
+
* If the value is already a {@link ReplaySubject}, it will be returned as-is.
|
|
231
|
+
*
|
|
232
|
+
* If the value is a {@link Promise}, it will be awaited,
|
|
233
|
+
* and the awaited value will be published into the {@link ReplaySubject} when it becomes available.
|
|
234
|
+
*
|
|
235
|
+
* For any other type of value, it will be immediately published into the {@link ReplaySubject}.
|
|
236
|
+
*/
|
|
237
|
+
declare const replaySubjectFrom: <T>(initialValue: T | Promise<T> | ReplaySubject<T>) => ReplaySubject<T>;
|
|
238
|
+
|
|
239
|
+
declare const sleep: (ms: number) => Promise<void>;
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Takes a subject and splits it into two parts:
|
|
243
|
+
*
|
|
244
|
+
* 1. A function to submit new values into the subject.
|
|
245
|
+
* 2. An observable for subscribing to new values from the subject.
|
|
246
|
+
*
|
|
247
|
+
* This can be helpful when, to avoid bugs, you want to expose only one
|
|
248
|
+
* of these parts to external code and keep the other part private.
|
|
249
|
+
*/
|
|
250
|
+
declare function splitSubject<T>(subject: Subject<T>): readonly [(value: T) => void, Observable<T>];
|
|
251
|
+
|
|
252
|
+
declare const throwAfter: (ms: number, reason: string) => Promise<never>;
|
|
253
|
+
|
|
254
|
+
declare function tokensToPlanck(tokens: string, tokenDecimals: number): string;
|
|
255
|
+
declare function tokensToPlanck(tokens: string, tokenDecimals?: number): string | undefined;
|
|
256
|
+
declare function tokensToPlanck(tokens?: string, tokenDecimals?: number): string | undefined;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @name validateHexString
|
|
260
|
+
* @description Checks if a string is a hex string. Required to account for type differences between different polkadot libraries
|
|
261
|
+
* @param {string} str - string to check
|
|
262
|
+
* @returns {`0x${string}`} - boolean
|
|
263
|
+
* @example
|
|
264
|
+
* validateHexString("0x1234") // "0x1234"
|
|
265
|
+
* validateHexString("1234") // Error: Expected a hex string
|
|
266
|
+
* validateHexString(1234) // Error: Expected a string
|
|
267
|
+
**/
|
|
268
|
+
declare const validateHexString: (str: string) => `0x${string}`;
|
|
269
|
+
|
|
270
|
+
export { BigMath, Deferred, type FunctionProperties, type FunctionPropertyNames, type GetLoadableQueryParams, type HexString, type Loadable, type LoadableOptions, type LoadableStatus, MAX_DECIMALS_FORMAT, type NonFunctionProperties, type NonFunctionPropertyNames, type Prettify, type QueryResult, type QueryStatus, REGEX_HEX_STRING, addTrailingSlash, classNames, cn, firstThenDebounce, formatDecimals, formatPrice, getLoadable$, getLoadableQuery$, getQuery$, getSharedObservable, hasOwnProperty, isAbortError, isArrayOf, isAscii, isBigInt, isBooleanTrue, isHexString, isNotNil, isPromise, isSubject, isTruthy, keepAlive, planckToTokens, replaySubjectFrom, sleep, splitSubject, throwAfter, tokensToPlanck, validateHexString };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import * as tailwind_merge from 'tailwind-merge';
|
|
2
|
+
import { OperatorFunction, Observable, Subject, ReplaySubject } from 'rxjs';
|
|
3
|
+
import BigNumber from 'bignumber.js';
|
|
4
|
+
|
|
5
|
+
declare const addTrailingSlash: (url: string) => string;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Javascript's `Math` library for `BigInt`.
|
|
9
|
+
* Taken from https://stackoverflow.com/questions/51867270/is-there-a-library-similar-to-math-that-supports-javascript-bigint/64953280#64953280
|
|
10
|
+
*/
|
|
11
|
+
declare const BigMath: {
|
|
12
|
+
abs(x: bigint): bigint;
|
|
13
|
+
sign(x: bigint): 0n | 1n | -1n;
|
|
14
|
+
min(value: bigint, ...values: bigint[]): bigint;
|
|
15
|
+
max(value: bigint, ...values: bigint[]): bigint;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
declare const classNames: (...classLists: tailwind_merge.ClassNameValue[]) => string;
|
|
19
|
+
declare const cn: (...classLists: tailwind_merge.ClassNameValue[]) => string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* In TypeScript, a deferred promise refers to a pattern that involves creating a promise that can be
|
|
23
|
+
* resolved or rejected at a later point in time, typically by code outside of the current function scope.
|
|
24
|
+
*
|
|
25
|
+
* This pattern is often used when dealing with asynchronous operations that involve multiple steps or when
|
|
26
|
+
* the result of an operation cannot be immediately determined.
|
|
27
|
+
*/
|
|
28
|
+
declare function Deferred<T>(): {
|
|
29
|
+
promise: Promise<T>;
|
|
30
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
31
|
+
reject: (reason?: unknown) => void;
|
|
32
|
+
isPending: () => boolean;
|
|
33
|
+
isResolved: () => boolean;
|
|
34
|
+
isRejected: () => boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** biome-ignore-all lint/complexity/noBannedTypes: legacy */
|
|
38
|
+
type FunctionPropertyNames<T> = {
|
|
39
|
+
[K in keyof T]: T[K] extends Function ? K : never;
|
|
40
|
+
}[keyof T];
|
|
41
|
+
type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;
|
|
42
|
+
type NonFunctionPropertyNames<T> = {
|
|
43
|
+
[K in keyof T]: T[K] extends Function ? never : K;
|
|
44
|
+
}[keyof T];
|
|
45
|
+
type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* An rxjs operator which:
|
|
49
|
+
*
|
|
50
|
+
* 1. Emits the first value it receives from the source observable, then:
|
|
51
|
+
* 2. Debounces any future values by `timeout` ms.
|
|
52
|
+
*/
|
|
53
|
+
declare const firstThenDebounce: <T>(timeout: number) => OperatorFunction<T, T>;
|
|
54
|
+
|
|
55
|
+
declare const MAX_DECIMALS_FORMAT = 12;
|
|
56
|
+
/**
|
|
57
|
+
* Custom decimal number formatting for Talisman
|
|
58
|
+
* note that the NumberFormat().format() call is the ressource heavy part, it's not worth trying to optimize other parts
|
|
59
|
+
* @param num input number
|
|
60
|
+
* @param digits number of significant digits to display
|
|
61
|
+
* @param locale locale used to format the number
|
|
62
|
+
* @param options formatting options
|
|
63
|
+
* @returns the formatted value
|
|
64
|
+
*/
|
|
65
|
+
declare const formatDecimals: (num?: string | number | null | BigNumber, digits?: number, options?: Partial<Intl.NumberFormatOptions>, locale?: string) => string;
|
|
66
|
+
|
|
67
|
+
declare const formatPrice: (price: number, currency: string, compact: boolean) => string;
|
|
68
|
+
|
|
69
|
+
type LoadableError = {
|
|
70
|
+
name: string;
|
|
71
|
+
message: string;
|
|
72
|
+
};
|
|
73
|
+
type Loadable<T = unknown> = {
|
|
74
|
+
status: "loading";
|
|
75
|
+
data?: T;
|
|
76
|
+
error?: undefined;
|
|
77
|
+
} | {
|
|
78
|
+
status: "success";
|
|
79
|
+
data: T;
|
|
80
|
+
error?: undefined;
|
|
81
|
+
} | {
|
|
82
|
+
status: "error";
|
|
83
|
+
data?: T;
|
|
84
|
+
error: LoadableError;
|
|
85
|
+
};
|
|
86
|
+
type LoadableStatus = Loadable["status"];
|
|
87
|
+
type LoadableOptions = {
|
|
88
|
+
getError?: (error: unknown) => LoadableError;
|
|
89
|
+
refreshInterval?: number;
|
|
90
|
+
};
|
|
91
|
+
declare function getLoadable$<T>(factory: () => Promise<T>, options?: LoadableOptions): Observable<Loadable<T>>;
|
|
92
|
+
|
|
93
|
+
type GetLoadableQueryParams<TArgs extends unknown[], TResult> = {
|
|
94
|
+
namespace: string;
|
|
95
|
+
args: TArgs;
|
|
96
|
+
queryFn: (args: TArgs, signal: AbortSignal) => Promise<TResult>;
|
|
97
|
+
refreshInterval?: number;
|
|
98
|
+
defaultValue?: TResult;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Thin wrapper around getQuery$ that returns Loadable<T> and optionally
|
|
102
|
+
* primes the stream with a loading state using the provided default value.
|
|
103
|
+
*
|
|
104
|
+
* TODO: consolidate with getQuery$
|
|
105
|
+
*/
|
|
106
|
+
declare const getLoadableQuery$: <TArgs extends unknown[], TResult>(params: GetLoadableQueryParams<TArgs, TResult>) => Observable<Loadable<TResult>>;
|
|
107
|
+
|
|
108
|
+
type QueryStatus = "loading" | "loaded" | "error";
|
|
109
|
+
type QueryResult<T, S extends QueryStatus = "loading" | "loaded" | "error"> = S extends "loading" ? {
|
|
110
|
+
status: "loading";
|
|
111
|
+
data: T | undefined;
|
|
112
|
+
error: undefined;
|
|
113
|
+
} : S extends "loaded" ? {
|
|
114
|
+
status: "loaded";
|
|
115
|
+
data: T;
|
|
116
|
+
error: undefined;
|
|
117
|
+
} : {
|
|
118
|
+
status: "error";
|
|
119
|
+
data: undefined;
|
|
120
|
+
error: unknown;
|
|
121
|
+
};
|
|
122
|
+
type QueryOptions<Output, Args> = {
|
|
123
|
+
namespace: string;
|
|
124
|
+
args: Args;
|
|
125
|
+
queryFn: (args: Args, signal: AbortSignal) => Promise<Output>;
|
|
126
|
+
defaultValue?: Output;
|
|
127
|
+
refreshInterval?: number;
|
|
128
|
+
serializer?: (args: Args) => string;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Creates a shared observable for executing queries with caching, loading states, and automatic refresh capabilities.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* const userQuery$ = getQuery$({
|
|
136
|
+
* namespace: 'users',
|
|
137
|
+
* args: { userId: 123 },
|
|
138
|
+
* queryFn: async ({ userId }) => fetchUser(userId),
|
|
139
|
+
* defaultValue: null,
|
|
140
|
+
* refreshInterval: 30000
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* userQuery$.subscribe(result => {
|
|
144
|
+
* if (result.status === 'loaded') {
|
|
145
|
+
* console.log(result.data);
|
|
146
|
+
* }
|
|
147
|
+
* });
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* @deprecated use getLoadableQuery$ instead
|
|
151
|
+
*/
|
|
152
|
+
declare const getQuery$: <Output, Args>({ namespace, args, queryFn, defaultValue, refreshInterval, serializer, }: QueryOptions<Output, Args>) => Observable<QueryResult<Output>>;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* When using react-rxjs hooks and state observables, the options are used as weak map keys.
|
|
156
|
+
* This means that if the options object is recreated on each render, the observable will be recreated as well.
|
|
157
|
+
* This utility function allows you to create a shared observable based on a namespace and arguments that, so react-rxjs can reuse the same observables
|
|
158
|
+
*
|
|
159
|
+
* @param namespace
|
|
160
|
+
* @param args
|
|
161
|
+
* @param createObservable
|
|
162
|
+
* @param serializer
|
|
163
|
+
* @returns
|
|
164
|
+
*/
|
|
165
|
+
declare const getSharedObservable: <Args, Output, ObsOutput = Observable<Output>>(namespace: string, args: Args, createObservable: (args: Args) => ObsOutput, serializer?: (args: Args) => string) => ObsOutput;
|
|
166
|
+
|
|
167
|
+
declare function hasOwnProperty<X, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown>;
|
|
168
|
+
|
|
169
|
+
declare const isAbortError: (error: unknown) => boolean;
|
|
170
|
+
|
|
171
|
+
declare function isArrayOf<T, P extends Array<unknown>>(array: unknown[], func: new (...args: P) => T): array is T[];
|
|
172
|
+
|
|
173
|
+
declare const isAscii: (str: string) => boolean;
|
|
174
|
+
|
|
175
|
+
declare const isBigInt: (value: unknown) => value is bigint;
|
|
176
|
+
|
|
177
|
+
declare const isBooleanTrue: <T>(x: T | null | undefined) => x is T;
|
|
178
|
+
|
|
179
|
+
type HexString = `0x${string}`;
|
|
180
|
+
declare const REGEX_HEX_STRING: RegExp;
|
|
181
|
+
declare const isHexString: (value: unknown) => value is HexString;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* WARNING: This function only checks against null or undefined, it does not coerce the value.
|
|
185
|
+
* ie: false and 0 are considered not nil
|
|
186
|
+
* Use isTruthy instead for a regular coercion check.
|
|
187
|
+
*
|
|
188
|
+
* @param value
|
|
189
|
+
* @returns whether the value is neither null nor undefined
|
|
190
|
+
*/
|
|
191
|
+
declare const isNotNil: <T>(value: T | null | undefined) => value is T;
|
|
192
|
+
|
|
193
|
+
declare const isPromise: <T = any>(value: any) => value is Promise<T>;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Tests to see if an object is an RxJS {@link Subject}.
|
|
197
|
+
*/
|
|
198
|
+
declare function isSubject<T>(object?: Subject<T> | object): object is Subject<T>;
|
|
199
|
+
|
|
200
|
+
declare const isTruthy: <T>(value: T | null | undefined) => value is T;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* An RxJS operator that keeps the source observable alive for a specified duration
|
|
204
|
+
* after all subscribers have unsubscribed. This prevents expensive re-subscriptions
|
|
205
|
+
* when subscribers come and go frequently.
|
|
206
|
+
*
|
|
207
|
+
* @param keepAliveMs - Duration in milliseconds to keep the source alive after last unsubscription
|
|
208
|
+
* @returns MonoTypeOperatorFunction that can be used in pipe()
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```typescript
|
|
212
|
+
* const data$ = expensive_api_call$.pipe(
|
|
213
|
+
* keepAlive(3000) // Keep alive for 3 seconds
|
|
214
|
+
* );
|
|
215
|
+
* ```
|
|
216
|
+
*/
|
|
217
|
+
declare const keepAlive: <T>(timeout: number) => OperatorFunction<T, T>;
|
|
218
|
+
|
|
219
|
+
type Prettify<T> = {
|
|
220
|
+
[K in keyof T]: T[K];
|
|
221
|
+
} & {};
|
|
222
|
+
|
|
223
|
+
declare function planckToTokens(planck: string, tokenDecimals: number): string;
|
|
224
|
+
declare function planckToTokens(planck: string, tokenDecimals?: number): string | undefined;
|
|
225
|
+
declare function planckToTokens(planck?: string, tokenDecimals?: number): string | undefined;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Turns a value into a {@link ReplaySubject} of size 1.
|
|
229
|
+
*
|
|
230
|
+
* If the value is already a {@link ReplaySubject}, it will be returned as-is.
|
|
231
|
+
*
|
|
232
|
+
* If the value is a {@link Promise}, it will be awaited,
|
|
233
|
+
* and the awaited value will be published into the {@link ReplaySubject} when it becomes available.
|
|
234
|
+
*
|
|
235
|
+
* For any other type of value, it will be immediately published into the {@link ReplaySubject}.
|
|
236
|
+
*/
|
|
237
|
+
declare const replaySubjectFrom: <T>(initialValue: T | Promise<T> | ReplaySubject<T>) => ReplaySubject<T>;
|
|
238
|
+
|
|
239
|
+
declare const sleep: (ms: number) => Promise<void>;
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Takes a subject and splits it into two parts:
|
|
243
|
+
*
|
|
244
|
+
* 1. A function to submit new values into the subject.
|
|
245
|
+
* 2. An observable for subscribing to new values from the subject.
|
|
246
|
+
*
|
|
247
|
+
* This can be helpful when, to avoid bugs, you want to expose only one
|
|
248
|
+
* of these parts to external code and keep the other part private.
|
|
249
|
+
*/
|
|
250
|
+
declare function splitSubject<T>(subject: Subject<T>): readonly [(value: T) => void, Observable<T>];
|
|
251
|
+
|
|
252
|
+
declare const throwAfter: (ms: number, reason: string) => Promise<never>;
|
|
253
|
+
|
|
254
|
+
declare function tokensToPlanck(tokens: string, tokenDecimals: number): string;
|
|
255
|
+
declare function tokensToPlanck(tokens: string, tokenDecimals?: number): string | undefined;
|
|
256
|
+
declare function tokensToPlanck(tokens?: string, tokenDecimals?: number): string | undefined;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @name validateHexString
|
|
260
|
+
* @description Checks if a string is a hex string. Required to account for type differences between different polkadot libraries
|
|
261
|
+
* @param {string} str - string to check
|
|
262
|
+
* @returns {`0x${string}`} - boolean
|
|
263
|
+
* @example
|
|
264
|
+
* validateHexString("0x1234") // "0x1234"
|
|
265
|
+
* validateHexString("1234") // Error: Expected a hex string
|
|
266
|
+
* validateHexString(1234) // Error: Expected a string
|
|
267
|
+
**/
|
|
268
|
+
declare const validateHexString: (str: string) => `0x${string}`;
|
|
269
|
+
|
|
270
|
+
export { BigMath, Deferred, type FunctionProperties, type FunctionPropertyNames, type GetLoadableQueryParams, type HexString, type Loadable, type LoadableOptions, type LoadableStatus, MAX_DECIMALS_FORMAT, type NonFunctionProperties, type NonFunctionPropertyNames, type Prettify, type QueryResult, type QueryStatus, REGEX_HEX_STRING, addTrailingSlash, classNames, cn, firstThenDebounce, formatDecimals, formatPrice, getLoadable$, getLoadableQuery$, getQuery$, getSharedObservable, hasOwnProperty, isAbortError, isArrayOf, isAscii, isBigInt, isBooleanTrue, isHexString, isNotNil, isPromise, isSubject, isTruthy, keepAlive, planckToTokens, replaySubjectFrom, sleep, splitSubject, throwAfter, tokensToPlanck, validateHexString };
|