1+ import { resolveGlobalMap } from "openclaw/plugin-sdk/text-runtime" ;
12import type { DiscordComponentEntry , DiscordModalEntry } from "./components.js" ;
23
34const DEFAULT_COMPONENT_TTL_MS = 30 * 60 * 1000 ;
45const DISCORD_COMPONENT_ENTRIES_KEY = Symbol . for ( "openclaw.discord.componentEntries" ) ;
56const DISCORD_MODAL_ENTRIES_KEY = Symbol . for ( "openclaw.discord.modalEntries" ) ;
67
7- function resolveGlobalMap < TKey , TValue > ( key : symbol ) : Map < TKey , TValue > {
8- const globalStore = globalThis as Record < PropertyKey , unknown > ;
9- if ( globalStore [ key ] instanceof Map ) {
10- return globalStore [ key ] as Map < TKey , TValue > ;
11- }
12- const created = new Map < TKey , TValue > ( ) ;
13- globalStore [ key ] = created ;
14- return created ;
15- }
16-
178const componentEntries = resolveGlobalMap < string , DiscordComponentEntry > (
189 DISCORD_COMPONENT_ENTRIES_KEY ,
1910) ;
@@ -33,68 +24,66 @@ function normalizeEntryTimestamps<T extends { createdAt?: number; expiresAt?: nu
3324 return { ...entry , createdAt, expiresAt } ;
3425}
3526
36- export function registerDiscordComponentEntries ( params : {
37- entries : DiscordComponentEntry [ ] ;
38- modals : DiscordModalEntry [ ] ;
39- ttlMs ?: number ;
40- messageId ?: string ;
41- } ) : void {
42- const now = Date . now ( ) ;
43- const ttlMs = params . ttlMs ?? DEFAULT_COMPONENT_TTL_MS ;
44- for ( const entry of params . entries ) {
27+ function registerEntries <
28+ T extends { id : string ; messageId ?: string ; createdAt ?: number ; expiresAt ?: number } ,
29+ > (
30+ entries : T [ ] ,
31+ store : Map < string , T > ,
32+ params : { now : number ; ttlMs : number ; messageId ?: string } ,
33+ ) : void {
34+ for ( const entry of entries ) {
4535 const normalized = normalizeEntryTimestamps (
4636 { ...entry , messageId : params . messageId ?? entry . messageId } ,
47- now ,
48- ttlMs ,
37+ params . now ,
38+ params . ttlMs ,
4939 ) ;
50- componentEntries . set ( entry . id , normalized ) ;
51- }
52- for ( const modal of params . modals ) {
53- const normalized = normalizeEntryTimestamps (
54- { ...modal , messageId : params . messageId ?? modal . messageId } ,
55- now ,
56- ttlMs ,
57- ) ;
58- modalEntries . set ( modal . id , normalized ) ;
40+ store . set ( entry . id , normalized ) ;
5941 }
6042}
6143
62- export function resolveDiscordComponentEntry ( params : {
63- id : string ;
64- consume ?: boolean ;
65- } ) : DiscordComponentEntry | null {
66- const entry = componentEntries . get ( params . id ) ;
44+ function resolveEntry < T extends { expiresAt ?: number } > (
45+ store : Map < string , T > ,
46+ params : { id : string ; consume ?: boolean } ,
47+ ) : T | null {
48+ const entry = store . get ( params . id ) ;
6749 if ( ! entry ) {
6850 return null ;
6951 }
7052 const now = Date . now ( ) ;
7153 if ( isExpired ( entry , now ) ) {
72- componentEntries . delete ( params . id ) ;
54+ store . delete ( params . id ) ;
7355 return null ;
7456 }
7557 if ( params . consume !== false ) {
76- componentEntries . delete ( params . id ) ;
58+ store . delete ( params . id ) ;
7759 }
7860 return entry ;
7961}
8062
63+ export function registerDiscordComponentEntries ( params : {
64+ entries : DiscordComponentEntry [ ] ;
65+ modals : DiscordModalEntry [ ] ;
66+ ttlMs ?: number ;
67+ messageId ?: string ;
68+ } ) : void {
69+ const now = Date . now ( ) ;
70+ const ttlMs = params . ttlMs ?? DEFAULT_COMPONENT_TTL_MS ;
71+ registerEntries ( params . entries , componentEntries , { now, ttlMs, messageId : params . messageId } ) ;
72+ registerEntries ( params . modals , modalEntries , { now, ttlMs, messageId : params . messageId } ) ;
73+ }
74+
75+ export function resolveDiscordComponentEntry ( params : {
76+ id : string ;
77+ consume ?: boolean ;
78+ } ) : DiscordComponentEntry | null {
79+ return resolveEntry ( componentEntries , params ) ;
80+ }
81+
8182export function resolveDiscordModalEntry ( params : {
8283 id : string ;
8384 consume ?: boolean ;
8485} ) : DiscordModalEntry | null {
85- const entry = modalEntries . get ( params . id ) ;
86- if ( ! entry ) {
87- return null ;
88- }
89- const now = Date . now ( ) ;
90- if ( isExpired ( entry , now ) ) {
91- modalEntries . delete ( params . id ) ;
92- return null ;
93- }
94- if ( params . consume !== false ) {
95- modalEntries . delete ( params . id ) ;
96- }
97- return entry ;
86+ return resolveEntry ( modalEntries , params ) ;
9887}
9988
10089export function clearDiscordComponentEntries ( ) : void {
0 commit comments