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

Show notification on new changes to pre-empt edit conflicts #1967

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions modules/friendlytag.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ Twinkle.tag.callback = function friendlytagCallback() {
// Redirects and files: Add a link to each template's description page
Morebits.quickForm.getElements(result, 'tags').forEach(generateLinks);
}

Twinkle.notifyOnChanges();
};


Expand Down
1 change: 1 addition & 0 deletions modules/twinkleprod.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Twinkle.prod.callback = function twinkleprodCallback() {
evt.initEvent('change', true, true);
result.prodtype[0].dispatchEvent(evt);

Twinkle.notifyOnChanges();
};


Expand Down
2 changes: 2 additions & 0 deletions modules/twinklespeedy.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ Twinkle.speedy.initDialog = function twinklespeedyInitDialog(callbackfunc) {

// Check for prior deletions. Just once, upon init
Twinkle.speedy.callback.priorDeletionCount();

Twinkle.notifyOnChanges();
};

Twinkle.speedy.callback.modeChanged = function twinklespeedyCallbackModeChanged(form) {
Expand Down
2 changes: 2 additions & 0 deletions modules/twinklexfd.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ Twinkle.xfd.callback = function twinklexfdCallback() {
var evt = document.createEvent('Event');
evt.initEvent('change', true, true);
result.venue.dispatchEvent(evt);

Twinkle.notifyOnChanges();
};

Twinkle.xfd.callback.wrongVenueWarning = function twinklexfdWrongVenueWarning(venue) {
Expand Down
52 changes: 52 additions & 0 deletions twinkle.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,58 @@ Twinkle.makeFindSourcesDiv = function makeSourcesDiv(divID) {
}
};

Twinkle.notifyOnChanges = function notifyOnChanges() {
let checkIfChanges = function (response) {
if (response.query.pages[0].missing) {
mw.notify('This page has just been deleted!', {
title: 'Twinkle',
autoHide: false,
type: 'error'
});
return true;
}
if (response.query.pages[0].revisions) {
mw.notify('Page has been edited in the meantime. Reload to see latest changes.', {
title: 'Twinkle',
autoHide: false,
type: 'warn'
});
return true;
}
return false;
};
let api = new mw.Api();
api.get({
action: 'query',
format: 'json',
prop: 'revisions',
revids: mw.config.get('wgRevisionId'),
formatversion: '2',
rvprop: 'ids|timestamp',
rvslots: 'main'
}).then(function (response) {
if (!checkIfChanges(response)) {
let timestamp = response.query.pages[0].revisions[0].timestamp;
let interval = setInterval(function () {
api.get({
action: 'query',
prop: 'revisions',
titles: Morebits.pageNameNorm,
formatversion: '2',
rvprop: 'ids',
rvslots: 'main',
rvstart: new Date(new Date(timestamp).getTime() + 1000).toISOString(),
rvdir: 'newer'
}).then(function (response) {
if (checkIfChanges(response)) {
clearInterval(interval);
}
});
}, 5000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this run every 5 seconds that the page is open, indefinitely?

If the page loses focus, does this still run?

If so, this could really hammer the servers. Sometimes I'll keep dozens of tabs open overnight. If needed, we should consider writing a back-off algorithm. For example, change the check rate to every hour after the first 5 minutes. Or only run when the tab is focused.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siddharthvp , any thoughts on this one? I want to clear the PR queue this week :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, on second thought, this would probably only do the 5 second check when that particular Morebits window is open, which is pretty rare. Not as big a problem as I was thinking.

A backoff alg might not be a bad idea though, since hammering the servers every 5 seconds for hours would probably not be ideal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be updated to stop polling once the Twinkle dialog has been closed. And we can include a document.hasFocus() check as well. I'll likely be able to get to this only next week :(

I think adding backoff is overcomplicating things, as people are presumably not going to keep Twinkle open for hours. Even if a few folks do, 1 read request every 5 seconds is nothing – the servers can handle a lot more than that. Besides, most browsers tend to throttle or suspend JS execution in inactive tabs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 read request every 5 seconds is nothing – the servers can handle a lot more than that.

Some pages like watchlist and RecentChanges in core already do this kind of polling. So does DiscussionTools I think to show new comments on talk page? (not sure about DT as I use CD instead - which also does it btw).

}
});
};

/** Twinkle-specific utility functions shared by multiple modules */
// Used in batch, unlink, and deprod to sort pages by namespace, as
// json formatversion=2 sorts by pageid instead (#1251)
Expand Down
Loading