Files
cozsweet-frontend-nextjs/src/integrations/message-queue.ts
T
admin 8cab34864e feat(ui): migrate Flutter UI library to Next.js 16 (App Router)
Migrate all 87 Dart files from /Users/chase/Documents/cozsweet/lib/ui/
to TypeScript React components, replacing Flutter-native features with
Web equivalents and reimplementing the BLoC pattern with React
Context + useReducer.

Highlights
- 1:1 BLoC -> Context+useReducer+side-effects mapping for auth, chat,
  user, and sidebar features (4 providers wired in root-providers.tsx)
- 500px MobileShell primitive eliminates per-screen layout boilerplate
- Headless Dialog primitive (createPortal + ESC + scroll lock) powers
  quota / pwa-install / username / pronouns / subscription / other-options
  dialogs
- New integrations/: facebook-sdk, google-identity, pwa-install,
  media-recorder (Web Speech API + MediaRecorder), browser-detect,
  chat-websocket (singleton), message-queue (throttled send)
- CSS Modules + existing design tokens (no new styling system);
  extended with breakpoints.css and dimensions.css
- Custom hooks: useBreakpoint (useSyncExternalStore),
  useFullVisibility (IntersectionObserver), usePwaInstall,
  usePullToRefresh
- Vitest + jsdom; 15 unit tests (chat-reducer, auth-validators)
- React 19.2.4, Next.js 16.2.7, Zod 4, lottie-react, classnames

Workarounds
- next.config.ts: experimental.staticGenerationRetryCount = 0 to
  bypass Next.js 16.2.7 /_global-error workStore prerender bug
- src/app/global-error.tsx: required Client Component
- src/types/globals.d.ts: centralized window.google / FB /
  SpeechRecognition declarations

Routes (/, /splash, /auth, /chat, /chat/deviceid/[deviceId],
/sidebar) all build and lint clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-09 13:39:49 +08:00

75 lines
1.8 KiB
TypeScript

/**
* 消息发送队列(限流)
*
* 原始 Dart: lib/core/net/message_queue.dart
*
* 业务行为:
* - 入队消息按 `intervalMs` 间隔逐条发送(默认 1000ms)
* - 消费者可声明每条消息是否成功消费;不成功则重新入队
* - 队列可在任意时刻清空(dispose)
*/
export type MessageConsumer = (content: string) => boolean | Promise<boolean>;
export class MessageQueue {
private queue: string[] = [];
private timer: ReturnType<typeof setTimeout> | null = null;
private disposed = false;
private consumer: MessageConsumer | null = null;
constructor(private readonly intervalMs = 1000) {}
/** 注册消费者(消费者必须能处理失败/重试)。 */
setConsumer(consumer: MessageConsumer): void {
this.consumer = consumer;
}
enqueue(content: string): void {
if (this.disposed) return;
this.queue.push(content);
if (!this.timer) this.scheduleNext();
}
private async attemptSend(next: string): Promise<void> {
if (!this.consumer) {
this.queue.unshift(next);
return;
}
let ok = false;
try {
ok = await this.consumer(next);
} catch (e) {
console.error("[MessageQueue] consumer error", e);
ok = false;
}
if (!ok) {
// 消费失败,放回队首等待下次
this.queue.unshift(next);
}
}
private scheduleNext(): void {
if (this.disposed) return;
const next = this.queue.shift();
if (next === undefined) {
this.timer = null;
return;
}
this.timer = setTimeout(() => {
void this.attemptSend(next).finally(() => this.scheduleNext());
}, this.intervalMs);
}
clear(): void {
this.queue = [];
}
dispose(): void {
this.disposed = true;
this.clear();
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}
}