Skip to content

Commit f4f1233

Browse files
authored
refactor(PagePatroller): add query cache (qiuwenbaike#1688)
* refactor(PagePatroller): add query cache
1 parent e8a0ba7 commit f4f1233

File tree

7 files changed

+151
-71
lines changed

7 files changed

+151
-71
lines changed

dist/PagePatroller/PagePatroller.js

Lines changed: 77 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PagePatroller/PagePatroller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {pagePatroller} from './modules/core';
1+
import {pagePatroller} from './modules/pagePatroller';
22

33
const {wgNamespaceNumber, wgArticleId, wgIsMainPage} = mw.config.get();
44

src/PagePatroller/modules/core.ts renamed to src/PagePatroller/modules/pagePatroller.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as OPTIONS from '../options.json';
2-
import {elementWrap, errorMessage, loading, notBeenPatrolledYet, patrolled, patrolledBy} from './elementWrap';
3-
import {api} from './api';
4-
import {replaceChild} from './replaceChild';
2+
import {elementWrap, errorMessage, loading, notBeenPatrolledYet, patrolled, patrolledBy} from './util/elementWrap';
3+
import {getPatroller} from './util/getPatroller';
4+
import {replaceChild} from './util/replaceChild';
55

66
const pagePatroller = async (): Promise<void> => {
77
const element = elementWrap();
@@ -20,39 +20,10 @@ const pagePatroller = async (): Promise<void> => {
2020
const {wgPageName} = mw.config.get();
2121

2222
try {
23-
const params: ApiQueryRevisionsParams & ApiQueryLogEventsParams = {
24-
action: 'query',
25-
format: 'json',
26-
formatversion: '2',
27-
prop: 'revisions',
28-
titles: wgPageName,
29-
list: 'logevents',
30-
letype: 'patrol',
31-
letitle: wgPageName,
32-
rvprop: 'timestamp',
33-
rvlimit: 1,
34-
rvdir: 'newer',
35-
smaxage: 600,
36-
maxage: 600,
37-
};
38-
const {query} = await api.get(params);
23+
const log = await getPatroller(wgPageName);
24+
const {action, user, timestamp} = log;
3925

40-
if (query?.logevents && query.logevents.length) {
41-
const [log]: [
42-
{
43-
user: string;
44-
timestamp: string;
45-
action: string;
46-
},
47-
] = query.logevents as [
48-
{
49-
user: string;
50-
timestamp: string;
51-
action: string;
52-
},
53-
];
54-
const {action, user} = log;
55-
const {timestamp} = log;
26+
if (action && user && timestamp) {
5627
const date: Date = new Date(timestamp);
5728

5829
if (action === 'patrol') {

src/PagePatroller/modules/elementWrap.tsx renamed to src/PagePatroller/modules/util/elementWrap.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as OPTIONS from '../options.json';
1+
import * as OPTIONS from '../../options.json';
22
import React from 'ext.gadget.React';
3-
import {getMessage} from './i18n';
3+
import {getMessage} from '../i18n';
44

55
const elementWrap = () => {
66
const {skin} = mw.config.get();
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import * as OPTIONS from '../../options.json';
2+
import {api} from '../api';
3+
4+
const getPatroller = async (wgPageName: string) => {
5+
let action: string | undefined;
6+
let user: string | undefined;
7+
let timestamp: string | undefined;
8+
9+
if (mw.storage.getObject(OPTIONS.storageKey + wgPageName)) {
10+
({action, user, timestamp} = mw.storage.getObject(OPTIONS.storageKey + wgPageName) as {
11+
action: string;
12+
user: string;
13+
timestamp: string;
14+
});
15+
} else {
16+
try {
17+
const params: ApiQueryRevisionsParams & ApiQueryLogEventsParams = {
18+
action: 'query',
19+
format: 'json',
20+
formatversion: '2',
21+
prop: 'revisions',
22+
titles: wgPageName,
23+
list: 'logevents',
24+
letype: 'patrol',
25+
letitle: wgPageName,
26+
rvprop: 'timestamp',
27+
rvlimit: 1,
28+
rvdir: 'newer',
29+
smaxage: 600,
30+
maxage: 600,
31+
};
32+
const {query} = await api.get(params);
33+
34+
if (query?.logevents && query.logevents.length) {
35+
const [log]: [
36+
{
37+
user: string;
38+
timestamp: string;
39+
action: string;
40+
},
41+
] = query.logevents as [
42+
{
43+
user: string;
44+
timestamp: string;
45+
action: string;
46+
},
47+
];
48+
({action} = log);
49+
50+
if (action === 'patrol') {
51+
({user, timestamp} = log);
52+
53+
mw.storage.setObject(OPTIONS.storageKey + wgPageName, {action, user, timestamp}, 60 * 60 * 24 * 30);
54+
}
55+
}
56+
} catch (error: unknown) {
57+
console.error('[PagePatroller] Ajax error in `getPatroller` method:', error);
58+
}
59+
}
60+
61+
return {action, user, timestamp};
62+
};
63+
64+
export {getPatroller};
File renamed without changes.

src/PagePatroller/options.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"elementId": "footer-info-patroller",
33
"mountPointSelector": "#footer-info,.page-info",
4+
"storageKey": "ext.gadget.PagePatroller_getPatroller-",
45
"version": "3.0"
56
}

0 commit comments

Comments
 (0)