mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-20 11:31:47 +08:00
1ceed6c145
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Introduced a Document Summary module, enabling live and cached document summaries with cloud revalidation. * Added a feature flag for enabling battery save mode. * Added explicit pause and resume controls for sync operations, accessible via UI events and programmatically. * **Improvements** * Enhanced sync and indexing logic to support pausing, resuming, and battery save mode, with improved job prioritization. * Updated navigation and preview components to use the new document summary service and improved priority handling. * Improved logging and state reporting for sync and indexing processes. * Refined backlink handling with reactive loading states and cloud revalidation. * Replaced backlink and link management to use a new dedicated document links service. * Enhanced workspace engine to conditionally enable battery save mode based on feature flags and workspace flavor. * **Bug Fixes** * Removed unnecessary debug console logs from various components for cleaner output. * **Refactor** * Replaced battery save mode methods with explicit pause/resume methods throughout the app and services. * Modularized and streamlined document summary and sync-related code for better maintainability. * Restructured backlink components to improve visibility handling and data fetching. * Simplified and improved document backlink data fetching with retry and loading state management. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
81 lines
1.6 KiB
TypeScript
81 lines
1.6 KiB
TypeScript
import { BinarySearchTree } from './binary-search-tree';
|
|
|
|
export class PriorityQueue {
|
|
tree = new BinarySearchTree<{ id: string; priority: number }>((a, b) => {
|
|
return a.priority === b.priority
|
|
? a.id === b.id
|
|
? 0
|
|
: a.id > b.id
|
|
? 1
|
|
: -1
|
|
: a.priority - b.priority;
|
|
});
|
|
priorityMap = new Map<string, number>();
|
|
|
|
push(id: string, priority: number = 0) {
|
|
const oldPriority = this.priorityMap.get(id);
|
|
if (oldPriority === priority) {
|
|
return;
|
|
}
|
|
if (oldPriority !== undefined) {
|
|
this.remove(id);
|
|
}
|
|
this.tree.insert({ id, priority });
|
|
this.priorityMap.set(id, priority);
|
|
}
|
|
|
|
pop(minimumPriority?: number) {
|
|
const node = this.tree.max();
|
|
|
|
if (!node) {
|
|
return null;
|
|
}
|
|
|
|
if (
|
|
minimumPriority !== undefined &&
|
|
node.getValue().priority < minimumPriority
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
this.tree.removeNode(node);
|
|
|
|
const { id } = node.getValue();
|
|
this.priorityMap.delete(id);
|
|
|
|
return id;
|
|
}
|
|
|
|
remove(id: string) {
|
|
const priority = this.priorityMap.get(id);
|
|
if (priority === undefined) {
|
|
return false;
|
|
}
|
|
const removed = this.tree.remove({ id, priority });
|
|
if (removed) {
|
|
this.priorityMap.delete(id);
|
|
}
|
|
|
|
return removed;
|
|
}
|
|
|
|
has(id: string) {
|
|
return this.priorityMap.has(id);
|
|
}
|
|
|
|
clear() {
|
|
this.tree.clear();
|
|
this.priorityMap.clear();
|
|
}
|
|
|
|
setPriority(id: string, priority: number) {
|
|
if (this.remove(id)) {
|
|
this.push(id, priority);
|
|
}
|
|
}
|
|
|
|
get length() {
|
|
return this.tree.count.bind(this.tree);
|
|
}
|
|
}
|