@rainlanguage/ui-components 0.0.1-alpha.76 → 0.0.1-alpha.78
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.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/services/awaitTransactionIndexing.d.ts +163 -0
- package/dist/services/awaitTransactionIndexing.js +120 -0
- package/dist/stores/transactionStore.js +22 -53
- package/dist/storesGeneric/cachedWritableStore.d.ts +57 -0
- package/dist/storesGeneric/cachedWritableStore.js +93 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -82,6 +82,7 @@ export { QKEY_VAULTS, QKEY_VAULT, QKEY_VAULT_CHANGES, QKEY_ORDERS, QKEY_ORDER, Q
|
|
|
82
82
|
export { darkChartTheme, lightChartTheme } from './utils/lightweightChartsThemes';
|
|
83
83
|
export { lightCodeMirrorTheme, darkCodeMirrorTheme } from './utils/codeMirrorThemes';
|
|
84
84
|
export { default as transactionStore } from './stores/transactionStore';
|
|
85
|
+
export { cachedWritableStore, cachedWritableIntOptional, cachedWritableStringOptional, cachedWritableString } from './storesGeneric/cachedWritableStore';
|
|
85
86
|
export { default as logoLight } from './assets/logo-light.svg';
|
|
86
87
|
export { default as logoDark } from './assets/logo-dark.svg';
|
|
87
88
|
export { default as GuiProvider } from './providers/GuiProvider.svelte';
|
package/dist/index.js
CHANGED
|
@@ -81,6 +81,7 @@ export { darkChartTheme, lightChartTheme } from './utils/lightweightChartsThemes
|
|
|
81
81
|
export { lightCodeMirrorTheme, darkCodeMirrorTheme } from './utils/codeMirrorThemes';
|
|
82
82
|
// Stores
|
|
83
83
|
export { default as transactionStore } from './stores/transactionStore';
|
|
84
|
+
export { cachedWritableStore, cachedWritableIntOptional, cachedWritableStringOptional, cachedWritableString } from './storesGeneric/cachedWritableStore';
|
|
84
85
|
// Assets
|
|
85
86
|
export { default as logoLight } from './assets/logo-light.svg';
|
|
86
87
|
export { default as logoDark } from './assets/logo-dark.svg';
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module awaitTransactionIndexing
|
|
3
|
+
* @description Utilities for waiting for transactions to be indexed by a subgraph
|
|
4
|
+
*/
|
|
5
|
+
import type { SgAddOrderWithOrder, SgRemoveOrderWithOrder, SgTransaction } from '@rainlanguage/orderbook';
|
|
6
|
+
/**
|
|
7
|
+
* Error message when subgraph indexing times out
|
|
8
|
+
*/
|
|
9
|
+
export declare const TIMEOUT_ERROR = "The subgraph took too long to respond. Your transaction may still be processing.";
|
|
10
|
+
/**
|
|
11
|
+
* Result of a subgraph indexing operation
|
|
12
|
+
* @template T The type of data returned by the subgraph
|
|
13
|
+
*/
|
|
14
|
+
export type IndexingResult<T> = {
|
|
15
|
+
/**
|
|
16
|
+
* The successful result of the indexing operation
|
|
17
|
+
*/
|
|
18
|
+
value?: {
|
|
19
|
+
/**
|
|
20
|
+
* The transaction hash
|
|
21
|
+
*/
|
|
22
|
+
txHash: string;
|
|
23
|
+
/**
|
|
24
|
+
* Message to display on successful indexing
|
|
25
|
+
*/
|
|
26
|
+
successMessage: string;
|
|
27
|
+
/**
|
|
28
|
+
* Optional order hash if available
|
|
29
|
+
*/
|
|
30
|
+
orderHash?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Optional network key
|
|
33
|
+
*/
|
|
34
|
+
network?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Optional data returned from the subgraph
|
|
37
|
+
*/
|
|
38
|
+
data?: T;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Error message if indexing failed
|
|
42
|
+
*/
|
|
43
|
+
error?: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Generic function to handle waiting for subgraph indexing
|
|
47
|
+
* Returns a promise that resolves with an object containing either value or error
|
|
48
|
+
*
|
|
49
|
+
* @template T The type of data returned by the subgraph
|
|
50
|
+
* @param options Configuration options for the indexing operation
|
|
51
|
+
* @returns Promise resolving to an IndexingResult
|
|
52
|
+
*/
|
|
53
|
+
export declare const awaitSubgraphIndexing: <T>(options: {
|
|
54
|
+
/**
|
|
55
|
+
* URL of the subgraph to query
|
|
56
|
+
*/
|
|
57
|
+
subgraphUrl: string;
|
|
58
|
+
/**
|
|
59
|
+
* Transaction hash to check for indexing
|
|
60
|
+
*/
|
|
61
|
+
txHash: string;
|
|
62
|
+
/**
|
|
63
|
+
* Message to display on successful indexing
|
|
64
|
+
*/
|
|
65
|
+
successMessage: string;
|
|
66
|
+
/**
|
|
67
|
+
* Maximum number of attempts before timing out
|
|
68
|
+
*/
|
|
69
|
+
maxAttempts?: number;
|
|
70
|
+
/**
|
|
71
|
+
* Interval between attempts in milliseconds
|
|
72
|
+
*/
|
|
73
|
+
interval?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Optional network identifier
|
|
76
|
+
*/
|
|
77
|
+
network?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Function to fetch data from the subgraph
|
|
80
|
+
* @param subgraphUrl URL of the subgraph
|
|
81
|
+
* @param txHash Transaction hash to query
|
|
82
|
+
*/
|
|
83
|
+
fetchData: (subgraphUrl: string, txHash: string) => Promise<T | null | undefined>;
|
|
84
|
+
/**
|
|
85
|
+
* Function to determine if the fetched data indicates success
|
|
86
|
+
* @param data The data returned from the subgraph
|
|
87
|
+
*/
|
|
88
|
+
isSuccess: (data: T) => boolean;
|
|
89
|
+
}) => Promise<IndexingResult<T>>;
|
|
90
|
+
/**
|
|
91
|
+
* Configuration for transaction indexing
|
|
92
|
+
* @template T The type of data returned by the subgraph
|
|
93
|
+
*/
|
|
94
|
+
export interface TransactionConfig<T> {
|
|
95
|
+
/**
|
|
96
|
+
* URL of the subgraph to query
|
|
97
|
+
*/
|
|
98
|
+
subgraphUrl: string;
|
|
99
|
+
/**
|
|
100
|
+
* Transaction hash to check for indexing
|
|
101
|
+
*/
|
|
102
|
+
txHash: string;
|
|
103
|
+
/**
|
|
104
|
+
* Message to display on successful indexing
|
|
105
|
+
*/
|
|
106
|
+
successMessage: string;
|
|
107
|
+
/**
|
|
108
|
+
* Optional network key
|
|
109
|
+
*/
|
|
110
|
+
network?: string;
|
|
111
|
+
/**
|
|
112
|
+
* Maximum number of attempts before timing out
|
|
113
|
+
*/
|
|
114
|
+
maxAttempts?: number;
|
|
115
|
+
/**
|
|
116
|
+
* Interval between attempts in milliseconds
|
|
117
|
+
*/
|
|
118
|
+
interval?: number;
|
|
119
|
+
/**
|
|
120
|
+
* Function to fetch data from the subgraph
|
|
121
|
+
*/
|
|
122
|
+
fetchData: (subgraphUrl: string, txHash: string) => Promise<T>;
|
|
123
|
+
/**
|
|
124
|
+
* Function to determine if the fetched data indicates success
|
|
125
|
+
*/
|
|
126
|
+
isSuccess: (data: T) => boolean;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Creates a configuration for checking general transaction indexing
|
|
130
|
+
*
|
|
131
|
+
* @param subgraphUrl URL of the subgraph to query
|
|
132
|
+
* @param txHash Transaction hash to check for indexing
|
|
133
|
+
* @param successMessage Message to display on successful indexing
|
|
134
|
+
* @param network Optional network key
|
|
135
|
+
* @param maxAttempts Maximum number of attempts before timing out
|
|
136
|
+
* @param interval Interval between attempts in milliseconds
|
|
137
|
+
* @returns Configuration for transaction indexing
|
|
138
|
+
*/
|
|
139
|
+
export declare const getTransactionConfig: (subgraphUrl: string, txHash: string, successMessage: string, network?: string, maxAttempts?: number, interval?: number) => TransactionConfig<SgTransaction>;
|
|
140
|
+
/**
|
|
141
|
+
* Creates a configuration for checking new order indexing
|
|
142
|
+
*
|
|
143
|
+
* @param subgraphUrl URL of the subgraph to query
|
|
144
|
+
* @param txHash Transaction hash to check for indexing
|
|
145
|
+
* @param successMessage Message to display on successful indexing
|
|
146
|
+
* @param network Optional network key
|
|
147
|
+
* @param maxAttempts Maximum number of attempts before timing out
|
|
148
|
+
* @param interval Interval between attempts in milliseconds
|
|
149
|
+
* @returns Configuration for new order indexing
|
|
150
|
+
*/
|
|
151
|
+
export declare const getNewOrderConfig: (subgraphUrl: string, txHash: string, successMessage: string, network?: string, maxAttempts?: number, interval?: number) => TransactionConfig<SgAddOrderWithOrder[]>;
|
|
152
|
+
/**
|
|
153
|
+
* Creates a configuration for checking order removal indexing
|
|
154
|
+
*
|
|
155
|
+
* @param subgraphUrl URL of the subgraph to query
|
|
156
|
+
* @param txHash Transaction hash to check for indexing
|
|
157
|
+
* @param successMessage Message to display on successful indexing
|
|
158
|
+
* @param network Optional network key
|
|
159
|
+
* @param maxAttempts Maximum number of attempts before timing out
|
|
160
|
+
* @param interval Interval between attempts in milliseconds
|
|
161
|
+
* @returns Configuration for order removal indexing
|
|
162
|
+
*/
|
|
163
|
+
export declare const getRemoveOrderConfig: (subgraphUrl: string, txHash: string, successMessage: string, network?: string, maxAttempts?: number, interval?: number) => TransactionConfig<SgRemoveOrderWithOrder[]>;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module awaitTransactionIndexing
|
|
3
|
+
* @description Utilities for waiting for transactions to be indexed by a subgraph
|
|
4
|
+
*/
|
|
5
|
+
import { getTransaction, getTransactionAddOrders, getTransactionRemoveOrders } from '@rainlanguage/orderbook';
|
|
6
|
+
/**
|
|
7
|
+
* Error message when subgraph indexing times out
|
|
8
|
+
*/
|
|
9
|
+
export const TIMEOUT_ERROR = 'The subgraph took too long to respond. Your transaction may still be processing.';
|
|
10
|
+
/**
|
|
11
|
+
* Generic function to handle waiting for subgraph indexing
|
|
12
|
+
* Returns a promise that resolves with an object containing either value or error
|
|
13
|
+
*
|
|
14
|
+
* @template T The type of data returned by the subgraph
|
|
15
|
+
* @param options Configuration options for the indexing operation
|
|
16
|
+
* @returns Promise resolving to an IndexingResult
|
|
17
|
+
*/
|
|
18
|
+
export const awaitSubgraphIndexing = async (options) => {
|
|
19
|
+
const { subgraphUrl, txHash, successMessage, maxAttempts = 10, interval = 1000, network, fetchData, isSuccess } = options;
|
|
20
|
+
return new Promise((resolve) => {
|
|
21
|
+
let attempts = 0;
|
|
22
|
+
const checkInterval = setInterval(async () => {
|
|
23
|
+
attempts++;
|
|
24
|
+
try {
|
|
25
|
+
const data = await fetchData(subgraphUrl, txHash);
|
|
26
|
+
if (data && isSuccess(data)) {
|
|
27
|
+
clearInterval(checkInterval);
|
|
28
|
+
let orderHash;
|
|
29
|
+
// Extract orderHash from order data if it exists in the expected format
|
|
30
|
+
if (Array.isArray(data) && data.length > 0 && data[0]?.order?.orderHash) {
|
|
31
|
+
orderHash = data[0].order.orderHash;
|
|
32
|
+
}
|
|
33
|
+
resolve({
|
|
34
|
+
value: {
|
|
35
|
+
txHash,
|
|
36
|
+
successMessage,
|
|
37
|
+
orderHash,
|
|
38
|
+
network,
|
|
39
|
+
data
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// Continue with the next attempt
|
|
47
|
+
}
|
|
48
|
+
if (attempts >= maxAttempts) {
|
|
49
|
+
clearInterval(checkInterval);
|
|
50
|
+
resolve({
|
|
51
|
+
error: TIMEOUT_ERROR
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}, interval);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Creates a configuration for checking general transaction indexing
|
|
60
|
+
*
|
|
61
|
+
* @param subgraphUrl URL of the subgraph to query
|
|
62
|
+
* @param txHash Transaction hash to check for indexing
|
|
63
|
+
* @param successMessage Message to display on successful indexing
|
|
64
|
+
* @param network Optional network key
|
|
65
|
+
* @param maxAttempts Maximum number of attempts before timing out
|
|
66
|
+
* @param interval Interval between attempts in milliseconds
|
|
67
|
+
* @returns Configuration for transaction indexing
|
|
68
|
+
*/
|
|
69
|
+
export const getTransactionConfig = (subgraphUrl, txHash, successMessage, network, maxAttempts, interval) => ({
|
|
70
|
+
subgraphUrl,
|
|
71
|
+
txHash,
|
|
72
|
+
successMessage,
|
|
73
|
+
network,
|
|
74
|
+
maxAttempts,
|
|
75
|
+
interval,
|
|
76
|
+
fetchData: getTransaction,
|
|
77
|
+
isSuccess: (tx) => !!tx
|
|
78
|
+
});
|
|
79
|
+
/**
|
|
80
|
+
* Creates a configuration for checking new order indexing
|
|
81
|
+
*
|
|
82
|
+
* @param subgraphUrl URL of the subgraph to query
|
|
83
|
+
* @param txHash Transaction hash to check for indexing
|
|
84
|
+
* @param successMessage Message to display on successful indexing
|
|
85
|
+
* @param network Optional network key
|
|
86
|
+
* @param maxAttempts Maximum number of attempts before timing out
|
|
87
|
+
* @param interval Interval between attempts in milliseconds
|
|
88
|
+
* @returns Configuration for new order indexing
|
|
89
|
+
*/
|
|
90
|
+
export const getNewOrderConfig = (subgraphUrl, txHash, successMessage, network, maxAttempts, interval) => ({
|
|
91
|
+
subgraphUrl,
|
|
92
|
+
txHash,
|
|
93
|
+
successMessage,
|
|
94
|
+
network,
|
|
95
|
+
maxAttempts,
|
|
96
|
+
interval,
|
|
97
|
+
fetchData: getTransactionAddOrders,
|
|
98
|
+
isSuccess: (addOrders) => addOrders?.length > 0
|
|
99
|
+
});
|
|
100
|
+
/**
|
|
101
|
+
* Creates a configuration for checking order removal indexing
|
|
102
|
+
*
|
|
103
|
+
* @param subgraphUrl URL of the subgraph to query
|
|
104
|
+
* @param txHash Transaction hash to check for indexing
|
|
105
|
+
* @param successMessage Message to display on successful indexing
|
|
106
|
+
* @param network Optional network key
|
|
107
|
+
* @param maxAttempts Maximum number of attempts before timing out
|
|
108
|
+
* @param interval Interval between attempts in milliseconds
|
|
109
|
+
* @returns Configuration for order removal indexing
|
|
110
|
+
*/
|
|
111
|
+
export const getRemoveOrderConfig = (subgraphUrl, txHash, successMessage, network, maxAttempts, interval) => ({
|
|
112
|
+
subgraphUrl,
|
|
113
|
+
txHash,
|
|
114
|
+
successMessage,
|
|
115
|
+
network,
|
|
116
|
+
maxAttempts,
|
|
117
|
+
interval,
|
|
118
|
+
fetchData: getTransactionRemoveOrders,
|
|
119
|
+
isSuccess: (removeOrders) => removeOrders?.length > 0
|
|
120
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { writable } from 'svelte/store';
|
|
2
2
|
import { sendTransaction, switchChain, waitForTransactionReceipt } from '@wagmi/core';
|
|
3
|
-
import { getTransaction, getTransactionAddOrders, getTransactionRemoveOrders } from '@rainlanguage/orderbook';
|
|
4
3
|
import { getExplorerLink } from '../services/getExplorerLink';
|
|
4
|
+
import { awaitSubgraphIndexing, getNewOrderConfig, getRemoveOrderConfig, getTransactionConfig } from '../services/awaitTransactionIndexing';
|
|
5
5
|
export const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000';
|
|
6
6
|
export const ONE = BigInt('1000000000000000000');
|
|
7
7
|
export var TransactionStatus;
|
|
@@ -52,24 +52,13 @@ const transactionStore = () => {
|
|
|
52
52
|
status: TransactionStatus.PENDING_SUBGRAPH,
|
|
53
53
|
message: 'Waiting for transaction to be indexed...'
|
|
54
54
|
}));
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
clearInterval(interval);
|
|
63
|
-
transactionSuccess(txHash, successMessage);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
catch {
|
|
67
|
-
if (attempts >= 10) {
|
|
68
|
-
clearInterval(interval);
|
|
69
|
-
return transactionError(TransactionErrorMessage.TIMEOUT);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}, 1000);
|
|
55
|
+
const result = await awaitSubgraphIndexing(getTransactionConfig(subgraphUrl, txHash, successMessage));
|
|
56
|
+
if (result.error) {
|
|
57
|
+
return transactionError(TransactionErrorMessage.TIMEOUT);
|
|
58
|
+
}
|
|
59
|
+
if (result.value) {
|
|
60
|
+
return transactionSuccess(result.value.txHash, result.value.successMessage);
|
|
61
|
+
}
|
|
73
62
|
};
|
|
74
63
|
const awaitNewOrderIndexing = async (subgraphUrl, txHash, network) => {
|
|
75
64
|
update((state) => ({
|
|
@@ -77,23 +66,13 @@ const transactionStore = () => {
|
|
|
77
66
|
status: TransactionStatus.PENDING_SUBGRAPH,
|
|
78
67
|
message: 'Waiting for new order to be indexed...'
|
|
79
68
|
}));
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return transactionSuccess(txHash, '', addOrders[0].order.orderHash, network);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
catch {
|
|
91
|
-
if (attempts >= 10) {
|
|
92
|
-
clearInterval(interval);
|
|
93
|
-
return transactionError(TransactionErrorMessage.TIMEOUT);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}, 1000);
|
|
69
|
+
const result = await awaitSubgraphIndexing(getNewOrderConfig(subgraphUrl, txHash, '', network));
|
|
70
|
+
if (result.error) {
|
|
71
|
+
return transactionError(TransactionErrorMessage.TIMEOUT);
|
|
72
|
+
}
|
|
73
|
+
if (result.value) {
|
|
74
|
+
return transactionSuccess(result.value.txHash, result.value.successMessage, result.value.orderHash, result.value.network);
|
|
75
|
+
}
|
|
97
76
|
};
|
|
98
77
|
const awaitRemoveOrderIndexing = async (subgraphUrl, txHash) => {
|
|
99
78
|
update((state) => ({
|
|
@@ -101,23 +80,13 @@ const transactionStore = () => {
|
|
|
101
80
|
status: TransactionStatus.PENDING_SUBGRAPH,
|
|
102
81
|
message: 'Waiting for order removal to be indexed...'
|
|
103
82
|
}));
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return transactionSuccess(txHash, 'Order removed successfully');
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
catch {
|
|
115
|
-
if (attempts >= 10) {
|
|
116
|
-
clearInterval(interval);
|
|
117
|
-
return transactionError(TransactionErrorMessage.TIMEOUT);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}, 1000);
|
|
83
|
+
const result = await awaitSubgraphIndexing(getRemoveOrderConfig(subgraphUrl, txHash, 'Order removed successfully'));
|
|
84
|
+
if (result.error) {
|
|
85
|
+
return transactionError(TransactionErrorMessage.TIMEOUT);
|
|
86
|
+
}
|
|
87
|
+
if (result.value) {
|
|
88
|
+
return transactionSuccess(result.value.txHash, result.value.successMessage);
|
|
89
|
+
}
|
|
121
90
|
};
|
|
122
91
|
const checkingWalletAllowance = (message) => update((state) => ({
|
|
123
92
|
...state,
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a writable Svelte store that persists its value to localStorage.
|
|
3
|
+
*
|
|
4
|
+
* @template T - The type of the value stored in the store
|
|
5
|
+
* @param {string} key - The localStorage key used to store the value
|
|
6
|
+
* @param {T} defaultValue - The default value to use when no value is found in localStorage
|
|
7
|
+
* @param {function(T): string} serialize - Function to convert the store value to a string for storage
|
|
8
|
+
* @param {function(string): T} deserialize - Function to convert the stored string back to the original type
|
|
9
|
+
* @returns {import('svelte/store').Writable<T>} A writable store that automatically syncs with localStorage
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Create a store for a boolean value
|
|
13
|
+
* const darkMode = cachedWritableStore(
|
|
14
|
+
* 'darkMode',
|
|
15
|
+
* false,
|
|
16
|
+
* value => JSON.stringify(value),
|
|
17
|
+
* str => JSON.parse(str)
|
|
18
|
+
* );
|
|
19
|
+
*
|
|
20
|
+
* // Create a store for a complex object
|
|
21
|
+
* const userPreferences = cachedWritableStore(
|
|
22
|
+
* 'userPrefs',
|
|
23
|
+
* { theme: 'light', fontSize: 14 },
|
|
24
|
+
* value => JSON.stringify(value),
|
|
25
|
+
* str => JSON.parse(str)
|
|
26
|
+
* );
|
|
27
|
+
*/
|
|
28
|
+
export declare function cachedWritableStore<T>(key: string, defaultValue: T, serialize: (value: T) => string, deserialize: (serialized: string) => T): import("svelte/store").Writable<T>;
|
|
29
|
+
export declare const cachedWritableString: (key: string, defaultValue?: string) => import("svelte/store").Writable<string>;
|
|
30
|
+
export declare const cachedWritableInt: (key: string, defaultValue?: number) => import("svelte/store").Writable<number>;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a writable store that can hold an optional value of type T and persists to localStorage.
|
|
33
|
+
*
|
|
34
|
+
* @template T - The type of the value stored
|
|
35
|
+
* @param {string} key - The localStorage key to use for persistence
|
|
36
|
+
* @param {T | undefined} defaultValue - The default value if nothing is found in localStorage
|
|
37
|
+
* @param {function} serialize - Function to convert the value to a string for storage
|
|
38
|
+
* @param {function} deserialize - Function to convert the stored string back to a value
|
|
39
|
+
* @returns A writable store that persists to localStorage and can hold undefined values
|
|
40
|
+
*/
|
|
41
|
+
export declare const cachedWritableOptionalStore: <T>(key: string, defaultValue: T | undefined, serialize: (value: T) => string, deserialize: (serialized: string) => T) => import("svelte/store").Writable<T | undefined>;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a writable store that can hold an optional number value and persists to localStorage.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} key - The localStorage key to use for persistence
|
|
46
|
+
* @param {number | undefined} defaultValue - The default value if nothing is found in localStorage
|
|
47
|
+
* @returns A writable store that persists to localStorage and can hold an optional number
|
|
48
|
+
*/
|
|
49
|
+
export declare const cachedWritableIntOptional: (key: string, defaultValue?: undefined) => import("svelte/store").Writable<number | undefined>;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a writable store that can hold an optional string value and persists to localStorage.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} key - The localStorage key to use for persistence
|
|
54
|
+
* @param {string | undefined} defaultValue - The default value if nothing is found in localStorage
|
|
55
|
+
* @returns A writable store that persists to localStorage and can hold an optional string
|
|
56
|
+
*/
|
|
57
|
+
export declare const cachedWritableStringOptional: (key: string, defaultValue?: undefined) => import("svelte/store").Writable<string | undefined>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { writable } from 'svelte/store';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a writable Svelte store that persists its value to localStorage.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The type of the value stored in the store
|
|
6
|
+
* @param {string} key - The localStorage key used to store the value
|
|
7
|
+
* @param {T} defaultValue - The default value to use when no value is found in localStorage
|
|
8
|
+
* @param {function(T): string} serialize - Function to convert the store value to a string for storage
|
|
9
|
+
* @param {function(string): T} deserialize - Function to convert the stored string back to the original type
|
|
10
|
+
* @returns {import('svelte/store').Writable<T>} A writable store that automatically syncs with localStorage
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Create a store for a boolean value
|
|
14
|
+
* const darkMode = cachedWritableStore(
|
|
15
|
+
* 'darkMode',
|
|
16
|
+
* false,
|
|
17
|
+
* value => JSON.stringify(value),
|
|
18
|
+
* str => JSON.parse(str)
|
|
19
|
+
* );
|
|
20
|
+
*
|
|
21
|
+
* // Create a store for a complex object
|
|
22
|
+
* const userPreferences = cachedWritableStore(
|
|
23
|
+
* 'userPrefs',
|
|
24
|
+
* { theme: 'light', fontSize: 14 },
|
|
25
|
+
* value => JSON.stringify(value),
|
|
26
|
+
* str => JSON.parse(str)
|
|
27
|
+
* );
|
|
28
|
+
*/
|
|
29
|
+
export function cachedWritableStore(key, defaultValue, serialize, deserialize) {
|
|
30
|
+
const getCache = () => {
|
|
31
|
+
try {
|
|
32
|
+
const cached = localStorage.getItem(key);
|
|
33
|
+
return cached !== null ? deserialize(cached) : defaultValue;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return defaultValue;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const setCache = (value) => {
|
|
40
|
+
try {
|
|
41
|
+
if (value !== undefined) {
|
|
42
|
+
localStorage.setItem(key, serialize(value));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
localStorage.removeItem(key);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Silently ignore localStorage errors to allow the application to function
|
|
50
|
+
// without persistence in environments where localStorage is unavailable
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const data = writable(getCache());
|
|
54
|
+
data.subscribe((value) => {
|
|
55
|
+
setCache(value);
|
|
56
|
+
});
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
export const cachedWritableString = (key, defaultValue = '') => cachedWritableStore(key, defaultValue, (v) => v, (v) => v);
|
|
60
|
+
export const cachedWritableInt = (key, defaultValue = 0) => cachedWritableStore(key, defaultValue, (v) => v.toString(), (v) => {
|
|
61
|
+
const parsed = Number.parseInt(v);
|
|
62
|
+
return isNaN(parsed) ? defaultValue : parsed;
|
|
63
|
+
});
|
|
64
|
+
/**
|
|
65
|
+
* Creates a writable store that can hold an optional value of type T and persists to localStorage.
|
|
66
|
+
*
|
|
67
|
+
* @template T - The type of the value stored
|
|
68
|
+
* @param {string} key - The localStorage key to use for persistence
|
|
69
|
+
* @param {T | undefined} defaultValue - The default value if nothing is found in localStorage
|
|
70
|
+
* @param {function} serialize - Function to convert the value to a string for storage
|
|
71
|
+
* @param {function} deserialize - Function to convert the stored string back to a value
|
|
72
|
+
* @returns A writable store that persists to localStorage and can hold undefined values
|
|
73
|
+
*/
|
|
74
|
+
export const cachedWritableOptionalStore = (key, defaultValue = undefined, serialize, deserialize) => cachedWritableStore(key, defaultValue, (v) => (v !== undefined ? serialize(v) : ''), (v) => (v !== '' ? deserialize(v) : undefined));
|
|
75
|
+
/**
|
|
76
|
+
* Creates a writable store that can hold an optional number value and persists to localStorage.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} key - The localStorage key to use for persistence
|
|
79
|
+
* @param {number | undefined} defaultValue - The default value if nothing is found in localStorage
|
|
80
|
+
* @returns A writable store that persists to localStorage and can hold an optional number
|
|
81
|
+
*/
|
|
82
|
+
export const cachedWritableIntOptional = (key, defaultValue = undefined) => cachedWritableOptionalStore(key, defaultValue, (v) => v.toString(), (v) => {
|
|
83
|
+
const parsed = Number.parseInt(v);
|
|
84
|
+
return isNaN(parsed) ? (defaultValue ?? 0) : parsed;
|
|
85
|
+
});
|
|
86
|
+
/**
|
|
87
|
+
* Creates a writable store that can hold an optional string value and persists to localStorage.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} key - The localStorage key to use for persistence
|
|
90
|
+
* @param {string | undefined} defaultValue - The default value if nothing is found in localStorage
|
|
91
|
+
* @returns A writable store that persists to localStorage and can hold an optional string
|
|
92
|
+
*/
|
|
93
|
+
export const cachedWritableStringOptional = (key, defaultValue = undefined) => cachedWritableOptionalStore(key, defaultValue, (v) => v, (v) => v);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rainlanguage/ui-components",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.78",
|
|
4
4
|
"description": "A component library for building Svelte applications to be used with Raindex.",
|
|
5
5
|
"license": "LicenseRef-DCL-1.0",
|
|
6
6
|
"author": "Rain Open Source Software Ltd",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@fontsource/dm-sans": "5.1.0",
|
|
54
54
|
"@imask/svelte": "7.6.1",
|
|
55
55
|
"@observablehq/plot": "0.6.16",
|
|
56
|
-
"@rainlanguage/orderbook": "0.0.1-alpha.
|
|
56
|
+
"@rainlanguage/orderbook": "0.0.1-alpha.78",
|
|
57
57
|
"@reown/appkit": "1.6.4",
|
|
58
58
|
"@reown/appkit-adapter-wagmi": "1.6.4",
|
|
59
59
|
"@sentry/sveltekit": "7.120.0",
|