Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: smarter partial update debounce #42

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/wet-hotels-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@callstack/byorg-utils': minor
'@callstack/byorg-slack': patch
---

utils: debouncePartialUpdate function for smart debounce handling
1 change: 0 additions & 1 deletion packages/slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@callstack/slack-rich-text": "workspace:*",
"@slack/bolt": "^4.1.1",
"@slack/web-api": "^7.8.0",
"p-debounce": "^4.0.0",
"ts-regex-builder": "^1.8.2"
},
"devDependencies": {
Expand Down
17 changes: 8 additions & 9 deletions packages/slack/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import Slack, { LogLevel, SayFn } from '@slack/bolt';
import { ContextBlock, RichTextBlock, WebClient } from '@slack/web-api';
import { Application } from '@callstack/byorg-core';
import { logger } from '@callstack/byorg-utils';
import { debouncePartialUpdate, logger } from '@callstack/byorg-utils';
import { parseMarkdownToRichTextBlock } from '@callstack/slack-rich-text';
import pDebounce from 'p-debounce';
import {
SlackMessage,
SlackApplicationConfig as SlackApplicationConfig,
ConversationMode,
} from './types.js';
import { fetchFileInfo, getThread } from './slack-api.js';
import { formatGroupMessage, toCoreMessage } from './messages.js';
import { wait } from './utils.js';
import { getBotId } from './bot-id.js';

type MessageBlock = RichTextBlock | ContextBlock;

const UPDATE_THROTTLE_TIME = 1000;
const MIN_UPDATE_TIME = 3000;
const MIN_RESPONSE_LENGTH = 250;
const NOTIFICATION_TEXT_LIMIT = 200;

const RESPONDING_BLOCK: MessageBlock = {
Expand Down Expand Up @@ -91,8 +90,6 @@ function configureSlackApp(app: Application, slack: Slack.App): void {
blocks,
});
responseMessageTs = responseMessage.ts as string;

await wait(UPDATE_THROTTLE_TIME);
return;
}

Expand All @@ -104,15 +101,17 @@ function configureSlackApp(app: Application, slack: Slack.App): void {
parse: 'none',
});

await wait(UPDATE_THROTTLE_TIME);
return;
};

let updatePartialResponsePromise: Promise<void> = Promise.resolve();
const updateResponseMessageWithThrottle = pDebounce.promise(updateResponseMessage);
const updateResponseMessageWithDebounce = debouncePartialUpdate(updateResponseMessage, {
minUpdateTime: MIN_UPDATE_TIME,
minResponseLength: MIN_RESPONSE_LENGTH,
});

const handlePartialResponse = (response: string): void => {
updatePartialResponsePromise = updateResponseMessageWithThrottle(response);
updatePartialResponsePromise = updateResponseMessageWithDebounce(response);
};

const startTime = performance.now();
Expand Down
3 changes: 0 additions & 3 deletions packages/slack/src/utils.ts

This file was deleted.

38 changes: 38 additions & 0 deletions packages/utils/src/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export type PartialUpdateFn = (text: string) => Promise<void>;
export type DebouncePartialUpdateOptions = {
minUpdateTime: number;
minResponseLength: number;
};

export function debouncePartialUpdate(
partialUpdate: PartialUpdateFn,
{ minResponseLength, minUpdateTime }: DebouncePartialUpdateOptions,
) {
let pendingPromise: Promise<void> | undefined;
let lastUpdate: number | undefined;

return async function (text: string) {
// Wait for the last update to finish
if (pendingPromise) {
return pendingPromise;
}

// Time-based debounce
if (lastUpdate !== undefined && performance.now() - lastUpdate < minUpdateTime) {
return;
}

// Skip too short initial messages
if (text.length < minResponseLength) {
return;
}

try {
lastUpdate = performance.now();
pendingPromise = partialUpdate(text);
return await pendingPromise;
} finally {
pendingPromise = undefined;
}
};
}
3 changes: 3 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ export type { Logger, LogLevel } from './logger/types.js';
export { requireEnv } from './env.js';

export { withCache } from './with-cache.js';

export { debouncePartialUpdate } from './debounce.js';
export type { PartialUpdateFn, DebouncePartialUpdateOptions } from './debounce.js';
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading