mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
63 lines
1.3 KiB
TypeScript
63 lines
1.3 KiB
TypeScript
import type { RelayEvent } from "@/shared/api/types";
|
|||
|
|
|
||
|
|
export type RelaySubscriptionFilter = {
|
||
|
|
kinds: number[];
|
||
|
|
"#h"?: string[];
|
||
|
|
limit: number;
|
||
|
|
};
|
||
|
|
|
||
|
|
type HistorySubscription = {
|
||
|
|
mode: "history";
|
||
|
|
events: RelayEvent[];
|
||
|
|
resolve: (events: RelayEvent[]) => void;
|
||
|
|
reject: (error: Error) => void;
|
||
|
|
timeout: ReturnType<typeof setTimeout>;
|
||
|
|
};
|
||
|
|
|
||
|
|
type LiveSubscription = {
|
||
|
|
mode: "live";
|
||
|
|
onEvent: (event: RelayEvent) => void;
|
||
|
|
resolveReady?: () => void;
|
||
|
|
};
|
||
|
|
|
||
|
|
export type PendingEvent = {
|
||
|
|
event: RelayEvent;
|
||
|
|
resolve: (event: RelayEvent) => void;
|
||
|
|
reject: (error: Error) => void;
|
||
|
|
timeout: ReturnType<typeof setTimeout>;
|
||
|
|
};
|
||
|
|
|
||
|
|
export type RelaySubscription = HistorySubscription | LiveSubscription;
|
||
|
|
|
||
|
|
export function sortEvents(events: RelayEvent[]) {
|
||
|
|
return [...events].sort((left, right) => left.created_at - right.created_at);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getTextPayload(message: unknown) {
|
||
|
|
if (typeof message === "string") {
|
||
|
|
return message;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (
|
||
|
|
typeof message === "object" &&
|
||
|
|
message !== null &&
|
||
|
|
"type" in message &&
|
||
|
|
message.type === "Text" &&
|
||
|
|
"data" in message &&
|
||
|
|
typeof message.data === "string"
|
||
|
|
) {
|
||
|
|
return message.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (
|
||
|
|
typeof message === "object" &&
|
||
|
|
message !== null &&
|
||
|
|
"Text" in message &&
|
||
|
|
typeof message.Text === "string"
|
||
|
|
) {
|
||
|
|
return message.Text;
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|