(args: {
channel: string;
parentTs: string;
seenCount: number;
intervalMs: number;
timeoutMs: number;
onSample: (sample: {
elapsedMs: number;
text: string | undefined;
message: SlackMessage | undefined;
}) => Promise<void> | void;
})
| 137 | * doesn't keep reporting the first (parent) reply. |
| 138 | */ |
| 139 | export async function watchForNextReply(args: { |
| 140 | channel: string; |
| 141 | parentTs: string; |
| 142 | seenCount: number; |
| 143 | intervalMs: number; |
| 144 | timeoutMs: number; |
| 145 | onSample: (sample: { |
| 146 | elapsedMs: number; |
| 147 | text: string | undefined; |
| 148 | message: SlackMessage | undefined; |
| 149 | }) => Promise<void> | void; |
| 150 | }): Promise<{ |
| 151 | finalText: string | undefined; |
| 152 | finalMessage: SlackMessage | undefined; |
| 153 | }> { |
| 154 | const start = Date.now(); |
| 155 | let target: SlackMessage | undefined; |
| 156 | let stable = 0; |
| 157 | let lastLen = -1; |
| 158 | while (Date.now() - start < args.timeoutMs) { |
| 159 | const replies = await threadReplies(args.channel, args.parentTs); |
| 160 | const bot = replies.filter((m) => m.user === BOT_USER_ID); |
| 161 | target = bot.length > args.seenCount ? bot[bot.length - 1] : undefined; |
| 162 | const text = target?.text; |
| 163 | await args.onSample({ |
| 164 | elapsedMs: Date.now() - start, |
| 165 | text, |
| 166 | message: target, |
| 167 | }); |
| 168 | const len = text?.length ?? 0; |
| 169 | if (target && len === lastLen && len > 0) { |
| 170 | stable++; |
| 171 | if (stable >= 3) break; |
| 172 | } else { |
| 173 | stable = 0; |
| 174 | lastLen = len; |
| 175 | } |
| 176 | await new Promise((r) => setTimeout(r, args.intervalMs)); |
| 177 | } |
| 178 | return { finalText: target?.text, finalMessage: target }; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Looser sibling of watchForReply for cases where the reply is in the |
no test coverage detected