-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathpayload-decoder.svelte
More file actions
70 lines (62 loc) · 1.94 KB
/
payload-decoder.svelte
File metadata and controls
70 lines (62 loc) · 1.94 KB
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
<script lang="ts">
import { type Snippet } from 'svelte';
import {
decodeEventAttributes,
decodePayloadAndParseDataToJSON,
decodePayloadsAndParseDataToJSON,
isRawPayload,
isRawPayloads,
type PayloadContainingObject,
type PotentiallyDecodable,
} from '$lib/utilities/decode-payload';
import { stringifyWithBigInt } from '$lib/utilities/parse-with-big-int';
interface Props {
value: PotentiallyDecodable | PayloadContainingObject;
onDecode?: (decodedPayloads: string[]) => void;
children: Snippet<[decodedPayloads: string[]]>;
loading?: Snippet;
}
let { value, onDecode, children, loading }: Props = $props();
let decodedValue = $state<string[]>([]);
let isDecoding = $state(true);
$effect(() => {
const controller = new AbortController();
isDecoding = true;
(async () => {
try {
let result: string[];
if (isRawPayload(value)) {
const decodedPayloadData =
await decodePayloadAndParseDataToJSON(value);
if (controller.signal.aborted) return;
result = [stringifyWithBigInt(decodedPayloadData)];
} else if (isRawPayloads(value)) {
const parsedPayloadsData =
await decodePayloadsAndParseDataToJSON(value);
if (controller.signal.aborted) return;
result = parsedPayloadsData.map((data) => stringifyWithBigInt(data));
} else {
const decoded = await decodeEventAttributes(value);
if (controller.signal.aborted) return;
result = [stringifyWithBigInt(decoded)];
}
decodedValue = result;
isDecoding = false;
onDecode?.(result);
} catch {
if (!controller.signal.aborted) {
isDecoding = false;
}
}
})();
return () => {
controller.abort();
isDecoding = false;
};
});
</script>
{#if isDecoding}
{@render loading?.()}
{:else}
{@render children(decodedValue)}
{/if}