Skip to content
Source
Sign inSign up
Open History to see the last commit

threadMessageJump.ts

42 lines1.3 KB

1import type { MessageId } from "@t3tools/contracts";
2​
3// A palette message result opens its thread at that message. The thread
4// screen reads the request while it shows that thread, and finishes it once
5// the feed is there or the message cannot be found. A request whose thread
6// never showed lapses, so a later plain open does not jump, as on web.
7const PENDING_JUMP_MS = 30_000;
8let pending: {
9 readonly threadKey: string;
10 readonly messageId: MessageId;
11 readonly at: number;
12} | null = null;
13const listeners = new Set<() => void>();
14​
15function notify() {
16 for (const listener of listeners) listener();
18​
19export function requestThreadMessageJump(threadKey: string, messageId: MessageId): void {
20 pending = { threadKey, messageId, at: Date.now() };
21 notify();
23​
24/** The message this thread should open at, if a palette pick asked for one. */
25export function pendingThreadMessageJump(threadKey: string): MessageId | null {
26 return pending?.threadKey === threadKey && Date.now() - pending.at < PENDING_JUMP_MS
27 ? pending.messageId
28 : null;
30​
31export function finishThreadMessageJump(threadKey: string): void {
32 if (pending?.threadKey !== threadKey) return;
33 pending = null;
34 notify();
36​
37export function subscribeThreadMessageJump(listener: () => void): () => void {
38 listeners.add(listener);
39 return () => {
40 listeners.delete(listener);
41 };