Skip to content

Commit d2c13dc

Browse files
authored
Merge pull request lichess-org#16000 from schlawg/ui-site-cleansing
relocate storage, trans, socket from site
2 parents c056178 + 9077055 commit d2c13dc

File tree

121 files changed

+568
-513
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+568
-513
lines changed

ui/@types/lichess/index.d.ts

+3-42
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,8 @@ interface Site {
1010
message: string;
1111
date: string;
1212
};
13-
StrongSocket: {
14-
// file://./../../site/src/socket.ts
15-
new (url: string, version: number | false, cfg?: any): any;
16-
firstConnect: Promise<(tpe: string, data: any) => void>;
17-
defaultParams: Record<string, any>;
18-
};
1913
mousetrap: LichessMousetrap; // file://./../../site/src/mousetrap.ts
2014
sri: string;
21-
storage: LichessStorageHelper;
22-
tempStorage: LichessStorageHelper;
23-
once(key: string, mod?: 'always'): boolean;
2415
powertip: LichessPowertip; // file://./../../site/src/powertip.ts
2516
clockWidget(el: HTMLElement, opts: { time: number; pause?: boolean }): void;
2617
spinnerHtml: string;
@@ -42,10 +33,10 @@ interface Site {
4233
pubsub: Pubsub; // file://./../../site/src/pubsub.ts
4334
unload: { expected: boolean };
4435
redirect(o: RedirectTo, beep?: boolean): void;
45-
reload(): void;
36+
reload(err?: any): void;
4637
watchers(el: HTMLElement): void;
4738
announce(d: LichessAnnouncement): void;
48-
trans(i18n: I18nDict): Trans;
39+
trans: Trans;
4940
sound: SoundI; // file://./../../site/src/sound.ts
5041
miniBoard: {
5142
// file://./../../common/src/miniBoard.ts
@@ -200,7 +191,7 @@ declare type SocketSend = (type: string, data?: any, opts?: any, noRetry?: boole
200191
type TransNoArg = (key: string) => string;
201192

202193
interface Trans {
203-
// file://./../../site/src/trans.ts
194+
// file://./../../common/src/trans.ts
204195
(key: string, ...args: Array<string | number>): string;
205196
noarg: TransNoArg;
206197
plural(key: string, count: number, ...args: Array<string | number>): string;
@@ -218,36 +209,6 @@ interface Pubsub {
218209
emit(msg: string, ...args: any[]): void;
219210
}
220211

221-
interface LichessStorageHelper {
222-
make(k: string, ttl?: number): LichessStorage;
223-
boolean(k: string): LichessBooleanStorage;
224-
get(k: string): string | null;
225-
set(k: string, v: string): void;
226-
fire(k: string, v?: string): void;
227-
remove(k: string): void;
228-
}
229-
230-
interface LichessStorage {
231-
get(): string | null;
232-
set(v: any): void;
233-
remove(): void;
234-
listen(f: (e: LichessStorageEvent) => void): void;
235-
fire(v?: string): void;
236-
}
237-
238-
interface LichessBooleanStorage {
239-
get(): boolean;
240-
getOrDefault(defaultValue: boolean): boolean;
241-
set(v: boolean): void;
242-
toggle(): void;
243-
}
244-
245-
interface LichessStorageEvent {
246-
sri: string;
247-
nonce: number;
248-
value?: string;
249-
}
250-
251212
interface LichessAnnouncement {
252213
msg?: string;
253214
date?: string;

ui/analyse/src/analyse.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { patch } from './view/util';
22
import makeBoot from './boot';
33
import makeStart from './start';
4+
import StrongSocket from 'common/socket';
45

56
export { patch };
67

@@ -15,7 +16,7 @@ export function initModule({ mode, cfg }: { mode: 'userAnalysis' | 'replay'; cfg
1516

1617
function userAnalysis(cfg: any) {
1718
cfg.$side = $('.analyse__side').clone();
18-
site.socket = new site.StrongSocket(cfg.socketUrl || '/analysis/socket/v5', cfg.socketVersion, {
19+
site.socket = new StrongSocket(cfg.socketUrl || '/analysis/socket/v5', cfg.socketVersion, {
1920
receive: (t: string, d: any) => analyse.socketReceive(t, d),
2021
});
2122
cfg.socketSend = site.socket.send;

ui/analyse/src/boot.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { AnalyseApi, AnalyseOpts } from './interfaces';
2+
import StrongSocket from 'common/socket';
23

34
export default function (start: (opts: AnalyseOpts) => AnalyseApi) {
45
return function (cfg: AnalyseOpts) {
56
const socketUrl = `/watch/${cfg.data.game.id}/${cfg.data.player.color}/v6`;
6-
site.socket = new site.StrongSocket(socketUrl, cfg.data.player.version, {
7+
site.socket = new StrongSocket(socketUrl, cfg.data.player.version, {
78
params: {
89
userTv: cfg.data.userTv && cfg.data.userTv.id,
910
},

ui/analyse/src/ctrl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import debounce from 'common/debounce';
88
import GamebookPlayCtrl from './study/gamebook/gamebookPlayCtrl';
99
import StudyCtrl from './study/studyCtrl';
1010
import { isTouchDevice } from 'common/device';
11-
import throttle from 'common/throttle';
11+
import { throttle } from 'common/timing';
1212
import {
1313
AnalyseOpts,
1414
AnalyseData,

ui/analyse/src/evalCache.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defined, prop } from 'common';
2-
import throttle from 'common/throttle';
2+
import { throttle } from 'common/timing';
33
import { EvalHit, EvalGetData, EvalPutData } from './interfaces';
44
import { AnalyseSocketSend } from './socket';
55
import { FEN } from 'chessground/types';

ui/analyse/src/ground.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Api as CgApi } from 'chessground/api';
33
import { Config as CgConfig } from 'chessground/config';
44
import * as cg from 'chessground/types';
55
import resizeHandle from 'common/resize';
6+
import { storage } from 'common/storage';
67
import AnalyseCtrl from './ctrl';
78
import * as Prefs from 'common/prefs';
89

@@ -67,7 +68,7 @@ export function makeConfig(ctrl: AnalyseCtrl): CgConfig {
6768
drawable: {
6869
enabled: true,
6970
eraseOnClick: !ctrl.opts.study || !!ctrl.opts.practice,
70-
defaultSnapToValidMove: site.storage.boolean('arrow.snap').getOrDefault(true),
71+
defaultSnapToValidMove: storage.boolean('arrow.snap').getOrDefault(true),
7172
},
7273
highlight: {
7374
lastMove: pref.highlight,

ui/analyse/src/plugins/analyse.nvui.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { renderSetting } from 'nvui/setting';
3232
import { Notify } from 'nvui/notify';
3333
import { commands } from 'nvui/command';
3434
import { bind, MaybeVNodes } from 'common/snabbdom';
35-
import throttle from 'common/throttle';
35+
import { throttle } from 'common/timing';
3636
import { Role } from 'chessground/types';
3737
import explorerView from '../explorer/explorerView';
3838
import { ops, path as treePath } from 'tree';

ui/analyse/src/plugins/analyse.study.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { patch } from '../view/util';
22
import makeBoot from '../boot';
33
import makeStart from '../start';
44
import * as studyDeps from '../study/studyDeps';
5+
import StrongSocket from 'common/socket';
56

67
export { patch };
78

89
export const start = makeStart(patch, studyDeps);
910
export const boot = makeBoot(start);
1011

1112
export function initModule(cfg: any) {
12-
site.socket = new site.StrongSocket(cfg.socketUrl || '/analysis/socket/v5', cfg.socketVersion, {
13+
site.socket = new StrongSocket(cfg.socketUrl || '/analysis/socket/v5', cfg.socketVersion, {
1314
receive: (t: string, d: any) => analyse.socketReceive(t, d),
1415
...(cfg.embed ? { params: { flag: 'embed' } } : {}),
1516
});

ui/analyse/src/serverSideUnderboard.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { stockfishName } from 'common/spinner';
88
import { domDialog } from 'common/dialog';
99
import { FEN } from 'chessground/types';
1010
import { escapeHtml } from 'common';
11+
import { storage } from 'common/storage';
1112

1213
export default function (element: HTMLElement, ctrl: AnalyseCtrl) {
1314
$(element).replaceWith(ctrl.opts.$underboard);
@@ -84,7 +85,7 @@ export default function (element: HTMLElement, ctrl: AnalyseCtrl) {
8485
});
8586
}
8687

87-
const storage = site.storage.make('analysis.panel');
88+
const store = storage.make('analysis.panel');
8889
const setPanel = function (panel: string) {
8990
$menu.children('.active').removeClass('active');
9091
$menu.find(`[data-panel="${panel}"]`).addClass('active');
@@ -102,10 +103,10 @@ export default function (element: HTMLElement, ctrl: AnalyseCtrl) {
102103
};
103104
$menu.on('mousedown', 'span', function (this: HTMLElement) {
104105
const panel = this.dataset.panel!;
105-
storage.set(panel);
106+
store.set(panel);
106107
setPanel(panel);
107108
});
108-
const stored = storage.get();
109+
const stored = store.get();
109110
const foundStored =
110111
stored &&
111112
$menu.children(`[data-panel="${stored}"]`).filter(function (this: HTMLElement) {

ui/analyse/src/start.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import makeView from './view/main';
44
import { AnalyseApi, AnalyseOpts } from './interfaces';
55
import { VNode } from 'snabbdom';
66
import type * as studyDeps from './study/studyDeps';
7+
import { trans } from 'common/trans';
78

89
export default function (
910
patch: (oldVnode: VNode | Element | DocumentFragment, vnode: VNode) => VNode,
1011
deps?: typeof studyDeps,
1112
) {
1213
return function (opts: AnalyseOpts): AnalyseApi {
1314
opts.element = document.querySelector('main.analyse') as HTMLElement;
14-
opts.trans = site.trans(opts.i18n);
15+
opts.trans = trans(opts.i18n);
1516

1617
const ctrl = (site.analysis = new makeCtrl(opts, redraw, deps?.StudyCtrl));
1718
const view = makeView(deps);

ui/analyse/src/study/commentForm.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { prop } from 'common';
22
import { onInsert } from 'common/snabbdom';
3-
import throttle from 'common/throttle';
3+
import { throttle } from 'common/timing';
44
import { h, VNode } from 'snabbdom';
55
import AnalyseCtrl from '../ctrl';
66
import { currentComments, isAuthorObj } from './studyComments';
7+
import { storage } from 'common/storage';
78

89
interface Current {
910
chapterId: string;
@@ -80,7 +81,7 @@ export function view(root: AnalyseCtrl): VNode {
8081
setupTextarea(vnode);
8182
const el = vnode.elm as HTMLInputElement;
8283
el.oninput = () => setTimeout(() => ctrl.submit(el.value), 50);
83-
const heightStore = site.storage.make('study.comment.height');
84+
const heightStore = storage.make('study.comment.height');
8485
el.onmouseup = () => heightStore.set('' + el.offsetHeight);
8586
el.style.height = parseInt(heightStore.get() || '80') + 'px';
8687

ui/analyse/src/study/gamebook/gamebookEdit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as control from '../../control';
22
import AnalyseCtrl from '../../ctrl';
33
import { requestIdleCallback } from 'common';
44
import * as licon from 'common/licon';
5-
import throttle from 'common/throttle';
5+
import { throttle } from 'common/timing';
66
import { iconTag, bind, MaybeVNodes } from 'common/snabbdom';
77
import { h, Hooks, VNode } from 'snabbdom';
88

ui/analyse/src/study/multiBoard.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import { StudyChapters, gameLinkAttrs, gameLinksListener } from './studyChapters
1212
import { playerFed } from './playerBars';
1313
import { userTitle } from 'common/userLink';
1414
import { h } from 'snabbdom';
15+
import { storage } from 'common/storage';
1516

1617
export class MultiBoardCtrl {
1718
playing: Toggle;
1819
teamSelect: Prop<string> = prop('');
1920
page: number = 1;
20-
maxPerPageStorage = site.storage.make('study.multiBoard.maxPerPage');
21+
maxPerPageStorage = storage.make('study.multiBoard.maxPerPage');
2122

2223
constructor(
2324
readonly chapters: StudyChapters,

ui/analyse/src/study/studyCtrl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Config as CgConfig } from 'chessground/config';
22
import { DrawShape } from 'chessground/draw';
33
import { prop, defined } from 'common';
4-
import throttle, { throttlePromise } from 'common/throttle';
4+
import { throttle, throttlePromise } from 'common/timing';
55
import debounce from 'common/debounce';
66
import AnalyseCtrl from '../ctrl';
77
import { StudyMemberCtrl } from './studyMembers';

ui/analyse/src/study/studyGlyph.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { prop } from 'common';
22
import { bind } from 'common/snabbdom';
3-
import throttle from 'common/throttle';
3+
import { throttle } from 'common/timing';
44
import { spinnerVdom as spinner } from 'common/spinner';
55
import { h, VNode } from 'snabbdom';
66
import AnalyseCtrl from '../ctrl';

ui/analyse/src/study/studyMembers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { StudyMember, StudyMemberMap, Tab } from './interfaces';
1010
import { textRaw as xhrTextRaw } from 'common/xhr';
1111
import { userLink } from 'common/userLink';
1212
import StudyCtrl from './studyCtrl';
13+
import { once } from 'common/storage';
1314

1415
interface Opts {
1516
initDict: StudyMemberMap;
@@ -99,7 +100,7 @@ export class StudyMemberCtrl {
99100
const wasContrib = this.myMember() && this.canContribute();
100101
this.dict(members);
101102
if (wasViewer && this.canContribute()) {
102-
if (site.once('study-tour')) this.opts.startTour();
103+
if (once('study-tour')) this.opts.startTour();
103104
this.opts.onBecomingContributor();
104105
this.opts.notif.set({
105106
text: this.opts.trans.noarg('youAreNowAContributor'),

ui/analyse/src/study/studyTags.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { onInsert } from 'common/snabbdom';
2-
import throttle from 'common/throttle';
2+
import { throttle } from 'common/timing';
33
import { h, thunk, VNode } from 'snabbdom';
44
import { option } from '../view/util';
55
import { looksLikeLichessGame } from './studyChapters';

ui/analyse/src/treeView/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import AnalyseCtrl from '../ctrl';
22
import contextMenu from './contextMenu';
3-
import throttle from 'common/throttle';
3+
import { throttle } from 'common/timing';
44
import { enrichText, innerHTML } from 'common/richText';
55
import { authorText as commentAuthorText } from '../study/studyComments';
66
import { bindMobileTapHold } from 'common/device';

ui/analyse/src/view/components.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import StudyCtrl from '../study/studyCtrl';
4040
import RelayCtrl from '../study/relay/relayCtrl';
4141
import type * as studyDeps from '../study/studyDeps';
4242
import { renderPgnError } from '../pgnImport';
43+
import { storage } from 'common/storage';
4344

4445
export interface ViewContext {
4546
ctrl: AnalyseCtrl;
@@ -146,7 +147,7 @@ export function renderBoard({ ctrl, study, playerBars, playerStrips }: ViewConte
146147
addChapterId(study, 'div.analyse__board.main-board'),
147148
{
148149
hook:
149-
'ontouchstart' in window || !site.storage.boolean('scrollMoves').getOrDefault(true)
150+
'ontouchstart' in window || !storage.boolean('scrollMoves').getOrDefault(true)
150151
? undefined
151152
: bindNonPassive(
152153
'wheel',

ui/bits/src/bits.account.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as licon from 'common/licon';
22
import * as xhr from 'common/xhr';
3+
import { storage } from 'common/storage';
34
import { addPasswordVisibilityToggleListener } from 'common/password';
45
import flairPickerLoader from './exports/flairPicker';
56

@@ -32,22 +33,19 @@ site.load.then(() => {
3233
computeBitChoices($form, 'behavior.submitMove');
3334
localPrefs.forEach(([categ, name, storeKey]) => {
3435
if (this.name == `${categ}.${name}`) {
35-
site.storage.boolean(storeKey).set(this.value == '1');
36+
storage.boolean(storeKey).set(this.value == '1');
3637
showSaved();
3738
}
3839
});
3940
xhr.formToXhr(form).then(() => {
4041
showSaved();
41-
site.storage.fire('reload-round-tabs');
42+
storage.fire('reload-round-tabs');
4243
});
4344
});
4445
});
4546

4647
localPrefs.forEach(([categ, name, storeKey, def]) =>
47-
$(`#ir${categ}_${name}_${site.storage.boolean(storeKey).getOrDefault(def) ? 1 : 0}`).prop(
48-
'checked',
49-
true,
50-
),
48+
$(`#ir${categ}_${name}_${storage.boolean(storeKey).getOrDefault(def) ? 1 : 0}`).prop('checked', true),
5149
);
5250

5351
$('form[action="/account/oauth/token/create"]').each(function (this: HTMLFormElement) {

ui/bits/src/bits.challengePage.ts

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as xhr from 'common/xhr';
2+
import StrongSocket from 'common/socket';
23

34
interface ChallengeOpts {
45
xhrUrl: string;
@@ -10,21 +11,17 @@ export function initModule(opts: ChallengeOpts): void {
1011
const selector = '.challenge-page';
1112
let accepting: boolean;
1213

13-
site.socket = new site.StrongSocket(
14-
`/challenge/${opts.data.challenge.id}/socket/v5`,
15-
opts.data.socketVersion,
16-
{
17-
events: {
18-
reload() {
19-
xhr.text(opts.xhrUrl).then(html => {
20-
$(selector).replaceWith($(html).find(selector));
21-
init();
22-
site.contentLoaded($(selector)[0]);
23-
});
24-
},
14+
site.socket = new StrongSocket(`/challenge/${opts.data.challenge.id}/socket/v5`, opts.data.socketVersion, {
15+
events: {
16+
reload() {
17+
xhr.text(opts.xhrUrl).then(html => {
18+
$(selector).replaceWith($(html).find(selector));
19+
init();
20+
site.contentLoaded($(selector)[0]);
21+
});
2522
},
2623
},
27-
);
24+
});
2825

2926
function init() {
3027
if (!accepting)

ui/bits/src/bits.cms.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as xhr from 'common/xhr';
2-
import throttle from 'common/throttle';
2+
import { throttle } from 'common/timing';
33
import Editor from '@toast-ui/editor';
44
import { currentTheme } from 'common/theme';
55
import tablesort from 'tablesort';

0 commit comments

Comments
 (0)