Skip to content

Commit

Permalink
fix: watch for flags once per post
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep authored Aug 21, 2024
1 parent 2bf0ddb commit 13e3c08
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/AdvancedFlagging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function Setup(): void {
setupConfiguration();

// TODO make more specific & remove jQuery
// appends advanced flagging link to new/edited posts
// appends Advanced Flagging link to new/edited posts
$(document).ajaxComplete(() => setupPostPage());

isDone = true;
Expand Down
6 changes: 3 additions & 3 deletions src/UserscriptTools/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ export default class Post {
// Watch for manual flags if the user has chosen to do so
const watchFlags = Store.config[Cached.Configuration.watchFlags];

// don't watch for flags if the user doesn't want to
// exclude listener from running in deleted posts
// Don't watch for flags if the user doesn't want to.
// Exclude listener from running in deleted posts.
if (!watchFlags || this.deleted) return;

addXHRListener(xhr => {
Expand Down Expand Up @@ -338,7 +338,7 @@ export default class Post {
await addProgress(event, flagType, this);
$(this.flagged).fadeIn();
}, { once: true });
});
}, this.id);
}

public filterReporters(feedbacks: FlagTypeFeedbacks): Reporter[] {
Expand Down
10 changes: 9 additions & 1 deletion src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,16 @@ export async function delay(milliseconds: number): Promise<void> {

// Credits: https://github.com/SOBotics/Userscripts/blob/master/Natty/NattyReporter.user.js#L101
const callbacks: ((request: XMLHttpRequest) => void)[] = [];
const postIds: number[] = [];

export function addXHRListener(
callback: (request: XMLHttpRequest) => void,
postId?: number
): void {
// used so that the script watches for flags once per post
if (postId && postIds.includes(postId)) return;
else if (postId) postIds.push(postId);

export function addXHRListener(callback: (request: XMLHttpRequest) => void): void {
callbacks.push(callback);
}

Expand Down

0 comments on commit 13e3c08

Please sign in to comment.