mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-31 09:52:24 +00:00
Merge pull request #2733 from PR0M3TH3AN/perf-nostr-service-incremental-updates-3861441146552926280
⚡ Optimize video subscription updates using incremental processing
This commit is contained in:
2 files changed
+54
-3
No files matched your search
@@ -130,6 +130,7 @@ export class VideoEventBuffer {
|
||||
}
|
||||
}
|
||||
}
|
||||
updatedVideos.push(video);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -1743,9 +1743,59 @@ export class NostrService {
|
||||
limit: this.nostrClient.clampVideoRequestLimit(200),
|
||||
};
|
||||
const subscription = this.nostrClient.subscribeVideos(
|
||||
() => {
|
||||
const updated = this.nostrClient.getActiveVideos();
|
||||
applyAndNotify(updated, "subscription");
|
||||
(updatedVideos) => {
|
||||
if (!Array.isArray(updatedVideos) || !updatedVideos.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const activeUpdates = [];
|
||||
const deletedUpdates = [];
|
||||
|
||||
for (const v of updatedVideos) {
|
||||
if (v.deleted) {
|
||||
deletedUpdates.push(v);
|
||||
} else {
|
||||
activeUpdates.push(v);
|
||||
}
|
||||
}
|
||||
|
||||
if (deletedUpdates.length > 0) {
|
||||
const map = this.ensureVideosMap();
|
||||
for (const v of deletedUpdates) {
|
||||
if (v.id) {
|
||||
map.delete(v.id);
|
||||
}
|
||||
}
|
||||
this.markAuthorIndexDirty();
|
||||
}
|
||||
|
||||
const filtered = this.filterVideos(activeUpdates, {
|
||||
blacklistedEventIds,
|
||||
isAuthorBlocked,
|
||||
});
|
||||
|
||||
if (filtered.length > 0) {
|
||||
this.cacheVideos(filtered);
|
||||
}
|
||||
|
||||
if (filtered.length > 0 || deletedUpdates.length > 0) {
|
||||
// Persist the full list (latest N) from the now-updated cache
|
||||
const map = this.ensureVideosMap();
|
||||
const fullList = Array.from(map.values());
|
||||
fullList.sort((a, b) => {
|
||||
const aCreated = Number(a?.created_at) || 0;
|
||||
const bCreated = Number(b?.created_at) || 0;
|
||||
return bCreated - aCreated;
|
||||
});
|
||||
|
||||
this.emit("videos:updated", {
|
||||
videos: fullList,
|
||||
deleted: deletedUpdates,
|
||||
reason: "subscription",
|
||||
});
|
||||
|
||||
persistFilteredVideos(fullList);
|
||||
}
|
||||
},
|
||||
subscriptionOptions,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user