use more const wherever possible

Relies on const hoisting in same cases. Checked that code already
assumes initialized variables.

Checked that initialization order in snabbdom modules remains the same,
by always using the following structure: Instanciate ctrl, initial patch,
declare redraw function.
pull/8592/head
Niklas Fiekas 2021-04-07 19:35:57 +02:00
parent 51f6736d3f
commit b2cca042d9
22 changed files with 95 additions and 135 deletions

View File

@ -1,9 +1,7 @@
import { AnalyseApi, AnalyseOpts } from './interfaces';
import { AnalyseOpts } from './interfaces';
import { start } from './main';
export default function (cfg: AnalyseOpts) {
let analyse: AnalyseApi;
lichess.socket = new lichess.StrongSocket(cfg.data.url.socket, cfg.data.player.version, {
params: {
userTv: cfg.data.userTv && cfg.data.userTv.id,
@ -15,5 +13,5 @@ export default function (cfg: AnalyseOpts) {
cfg.$side = $('.analyse__side').clone();
cfg.$underboard = $('.analyse__underboard').clone();
cfg.socketSend = lichess.socket.send;
analyse = start(cfg);
const analyse = start(cfg);
}

View File

@ -1,5 +1,4 @@
import AnalyseCtrl from './ctrl';
import { attributesModule, classModule, init, VNode } from 'snabbdom';
import { attributesModule, classModule, init } from 'snabbdom';
import boot from './boot';
import LichessChat from 'chat';
// eslint-disable-next-line no-duplicate-imports
@ -15,18 +14,16 @@ export function start(opts: AnalyseOpts): AnalyseApi {
opts.element = document.querySelector('main.analyse') as HTMLElement;
opts.trans = lichess.trans(opts.i18n);
let vnode: VNode, ctrl: AnalyseCtrl;
const ctrl = new makeCtrl(opts, redraw);
const blueprint = view(ctrl);
opts.element.innerHTML = '';
let vnode = patch(opts.element, blueprint);
function redraw() {
vnode = patch(vnode, view(ctrl));
}
ctrl = new makeCtrl(opts, redraw);
const blueprint = view(ctrl);
opts.element.innerHTML = '';
vnode = patch(opts.element, blueprint);
menuHover();
return {

View File

@ -29,20 +29,18 @@ export default function (element: HTMLElement, ctrl: AnalyseCtrl) {
}, 50);
});
lichess.pubsub.on('analysis.change', (fen: Fen, _, mainlinePly: Ply | false) => {
let chart,
point,
$chart = $('#acpl-chart');
const $chart = $('#acpl-chart');
if (fen && fen !== lastFen) {
inputFen.value = fen;
lastFen = fen;
}
if ($chart.length) {
chart = $chart[0]!['highcharts'];
const chart = $chart[0]!['highcharts'];
if (chart) {
if (mainlinePly != chart.lastPly) {
if (mainlinePly === false) unselect(chart);
else {
point = chart.series[0].data[mainlinePly - 1 - data.game.startedAtTurn];
const point = chart.series[0].data[mainlinePly - 1 - data.game.startedAtTurn];
if (defined(point)) point.select();
else unselect(chart);
}
@ -51,7 +49,7 @@ export default function (element: HTMLElement, ctrl: AnalyseCtrl) {
}
}
if ($timeChart.length) {
chart = $timeChart[0]!['highcharts'];
const chart = $timeChart[0]!['highcharts'];
if (chart) {
if (mainlinePly != chart.lastPly) {
if (mainlinePly === false) unselect(chart);
@ -59,7 +57,7 @@ export default function (element: HTMLElement, ctrl: AnalyseCtrl) {
const white = mainlinePly % 2 !== 0;
const serie = white ? 0 : 1;
const turn = Math.floor((mainlinePly - 1 - data.game.startedAtTurn) / 2);
point = chart.series[serie].data[turn];
const point = chart.series[serie].data[turn];
if (defined(point)) point.select();
else unselect(chart);
}

View File

@ -149,8 +149,8 @@ function sameLines(l1: Line, l2: Line) {
}
function selectLines(ctrl: Ctrl): Array<Line> {
let prev: Line,
ls: Array<Line> = [];
const ls: Array<Line> = [];
let prev: Line | undefined;
ctrl.data.lines.forEach(line => {
if (
!line.d &&

View File

@ -1,8 +1,8 @@
import { init, attributesModule, classModule, VNode } from 'snabbdom';
import { init, attributesModule, classModule } from 'snabbdom';
import makeCtrl from './ctrl';
import view from './view';
import { ChatOpts, Ctrl } from './interfaces';
import { ChatOpts } from './interfaces';
import { PresetCtrl } from './preset';
export { Ctrl as ChatCtrl, ChatPlugin } from './interfaces';
@ -15,17 +15,15 @@ export default function LichessChat(
} {
const patch = init([classModule, attributesModule]);
let vnode: VNode, ctrl: Ctrl;
const ctrl = makeCtrl(opts, redraw);
const blueprint = view(ctrl);
element.innerHTML = '';
let vnode = patch(element, blueprint);
function redraw() {
vnode = patch(vnode, view(ctrl));
}
ctrl = makeCtrl(opts, redraw);
const blueprint = view(ctrl);
element.innerHTML = '';
vnode = patch(element, blueprint);
return ctrl;
}

View File

@ -51,8 +51,8 @@ export default function <Result>(opts: Opts<Result>) {
if (selectedIndex !== null) $container.find('.complete-result').eq(selectedIndex).addClass('complete-selected');
};
let $container: Cash = $('<div class="complete-list none"></div>').insertAfter(opts.input),
selectedIndex: number | null = null,
const $container: Cash = $('<div class="complete-list none"></div>').insertAfter(opts.input);
let selectedIndex: number | null = null,
renderedResults: Result[] = [];
opts.input.autocomplete = 'off';

View File

@ -1,4 +1,4 @@
import { init, attributesModule, eventListenersModule, classModule, propsModule, VNode } from 'snabbdom';
import { init, attributesModule, eventListenersModule, classModule, propsModule } from 'snabbdom';
import EditorCtrl from './ctrl';
import menuHover from 'common/menuHover';
import view from './view';
@ -8,17 +8,15 @@ import { EditorConfig } from './interfaces';
const patch = init([classModule, attributesModule, propsModule, eventListenersModule]);
export default function LichessEditor(element: HTMLElement, config: EditorConfig) {
let vnode: VNode, ctrl: EditorCtrl;
const redraw = () => {
vnode = patch(vnode, view(ctrl));
};
ctrl = new EditorCtrl(config, redraw);
const ctrl = new EditorCtrl(config, redraw);
element.innerHTML = '';
const inner = document.createElement('div');
element.appendChild(inner);
vnode = patch(inner, view(ctrl));
let vnode = patch(inner, view(ctrl));
function redraw() {
vnode = patch(vnode, view(ctrl));
}
menuHover();

View File

@ -26,7 +26,6 @@ export default function LichessLobby(opts: LobbyOpts) {
const match = RegExp('[?&]' + name + '=([^&]*)').exec(location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
};
let lobby: any;
lichess.socket = new lichess.StrongSocket('/lobby/socket/v5', false, {
receive(t: string, d: any) {
lobby.socketReceive(t, d);
@ -68,7 +67,7 @@ export default function LichessLobby(opts: LobbyOpts) {
opts.blindMode = $('body').hasClass('blind-mode');
opts.trans = lichess.trans(opts.i18n);
opts.socketSend = lichess.socket.send;
lobby = main(opts);
const lobby = main(opts);
const $startButtons = $('.lobby__start'),
clickEvent = opts.blindMode ? 'click' : 'mousedown';

View File

@ -1,7 +1,6 @@
import { init, VNode, classModule, attributesModule } from 'snabbdom';
import { init, classModule, attributesModule } from 'snabbdom';
import { Chessground } from 'chessground';
import { LobbyOpts, Tab } from './interfaces';
import LobbyController from './ctrl';
export const patch = init([classModule, attributesModule]);
@ -10,18 +9,16 @@ import makeCtrl from './ctrl';
import view from './view/main';
export default function main(opts: LobbyOpts) {
let vnode: VNode, ctrl: LobbyController;
const ctrl = new makeCtrl(opts, redraw);
const blueprint = view(ctrl);
opts.element.innerHTML = '';
let vnode = patch(opts.element, blueprint);
function redraw() {
vnode = patch(vnode, view(ctrl));
}
ctrl = new makeCtrl(opts, redraw);
const blueprint = view(ctrl);
opts.element.innerHTML = '';
vnode = patch(opts.element, blueprint);
return {
socketReceive: ctrl.socket.receive,
setTab(tab: Tab) {

View File

@ -1,6 +1,6 @@
import view from './view/main';
import { init, VNode, classModule, attributesModule } from 'snabbdom';
import { init, classModule, attributesModule } from 'snabbdom';
import { MsgOpts } from './interfaces';
import { upgradeData } from './network';
@ -13,17 +13,15 @@ export default function LichessMsg(opts: MsgOpts) {
window.addEventListener('resize', appHeight);
appHeight();
let vnode: VNode, ctrl: MsgCtrl;
const ctrl = new MsgCtrl(upgradeData(opts.data), lichess.trans(opts.i18n), redraw);
const blueprint = view(ctrl);
element.innerHTML = '';
let vnode = patch(element, blueprint);
function redraw() {
vnode = patch(vnode, view(ctrl));
}
ctrl = new MsgCtrl(upgradeData(opts.data), lichess.trans(opts.i18n), redraw);
const blueprint = view(ctrl);
element.innerHTML = '';
vnode = patch(element, blueprint);
redraw();
}

View File

@ -1,21 +1,18 @@
import { init, VNode, classModule, attributesModule } from 'snabbdom';
import { init, classModule, attributesModule } from 'snabbdom';
import makeCtrl from './ctrl';
import view from './view';
import { NotifyOpts, Ctrl } from './interfaces';
import { NotifyOpts } from './interfaces';
const patch = init([classModule, attributesModule]);
export default function LichessNotify(element: Element, opts: NotifyOpts) {
let vnode: VNode, ctrl: Ctrl;
const ctrl = makeCtrl(opts, redraw);
let vnode = patch(element, view(ctrl));
function redraw() {
vnode = patch(vnode, view(ctrl));
}
ctrl = makeCtrl(opts, redraw);
vnode = patch(element, view(ctrl));
if (opts.data) ctrl.update(opts.data, opts.incoming);
else ctrl.loadPage(1);

View File

@ -1,26 +1,24 @@
import { attributesModule, classModule, init, VNode } from 'snabbdom';
import { attributesModule, classModule, init } from 'snabbdom';
import makeCtrl from './ctrl';
import menuHover from 'common/menuHover';
import view from './view/main';
import { Chessground } from 'chessground';
import { Controller, PuzzleOpts } from './interfaces';
import { PuzzleOpts } from './interfaces';
const patch = init([classModule, attributesModule]);
export default function (opts: PuzzleOpts): void {
const element = document.querySelector('main.puzzle') as HTMLElement;
let vnode: VNode, ctrl: Controller;
const ctrl = makeCtrl(opts, redraw);
const blueprint = view(ctrl);
element.innerHTML = '';
let vnode = patch(element, blueprint);
function redraw() {
vnode = patch(vnode, view(ctrl));
}
ctrl = makeCtrl(opts, redraw);
const blueprint = view(ctrl);
element.innerHTML = '';
vnode = patch(element, blueprint);
menuHover();
}

View File

@ -8,7 +8,6 @@ import { tourStandingCtrl, TourStandingCtrl } from './tourStanding';
export default function (opts: RoundOpts): void {
const element = document.querySelector('.round__app') as HTMLElement,
data: RoundData = opts.data;
let round: RoundApi;
if (data.tournament) $('body').data('tournament-id', data.tournament.id);
lichess.socket = new lichess.StrongSocket(data.url.socket, data.player.version, {
params: { userTv: data.userTv && data.userTv.id },
@ -60,7 +59,7 @@ export default function (opts: RoundOpts): void {
opts.element = element;
opts.socketSend = lichess.socket.send;
round = (window['LichessRound'] as RoundMain).app(opts);
const round: RoundApi = (window['LichessRound'] as RoundMain).app(opts);
const chatOpts = opts.chat;
if (chatOpts) {
if (data.tournament?.top) {

View File

@ -340,12 +340,12 @@ export default class RoundController {
const d = this.data;
if (game.isPlayerTurn(d))
notify(() => {
let txt = this.noarg('yourTurn'),
opponent = renderUser.userTxt(this, d.opponent);
let txt = this.noarg('yourTurn');
const opponent = renderUser.userTxt(this, d.opponent);
if (this.ply < 1) txt = `${opponent}\njoined the game.\n${txt}`;
else {
let move = d.steps[d.steps.length - 1].san,
turn = Math.floor((this.ply - 1) / 2) + 1;
let move = d.steps[d.steps.length - 1].san;
const turn = Math.floor((this.ply - 1) / 2) + 1;
move = `${turn}${this.ply % 2 === 1 ? '.' : '...'} ${move}`;
txt = `${opponent}\nplayed ${move}.\n${txt}`;
}

View File

@ -1,4 +1,4 @@
import { attributesModule, classModule, init, VNode } from 'snabbdom';
import { attributesModule, classModule, init } from 'snabbdom';
import boot from './boot';
import LichessChat from 'chat';
import menuHover from 'common/menuHover';
@ -20,18 +20,16 @@ export interface RoundMain {
const patch = init([classModule, attributesModule]);
export function app(opts: RoundOpts): RoundApi {
let vnode: VNode, ctrl: RoundController;
const ctrl = new RoundController(opts, redraw);
const blueprint = view(ctrl);
opts.element.innerHTML = '';
let vnode = patch(opts.element, blueprint);
function redraw() {
vnode = patch(vnode, view(ctrl));
}
ctrl = new RoundController(opts, redraw);
const blueprint = view(ctrl);
opts.element.innerHTML = '';
vnode = patch(opts.element, blueprint);
window.addEventListener('resize', redraw); // col1 / col2+ transition
if (ctrl.isPlaying()) menuHover();

View File

@ -52,16 +52,12 @@ export default function (publicKey: string) {
});
$checkout.find('button.paypal').on('click', function () {
let freq = getFreq(),
cents: number;
if (freq == 'lifetime') {
cents = lifetime.cents;
} else {
cents = parseInt($checkout.find('group.amount input:checked').data('amount'));
}
const freq = getFreq(),
cents =
freq == 'lifetime' ? lifetime.cents : parseInt($checkout.find('group.amount input:checked').data('amount'));
if (!cents || cents < min || cents > max) return;
var amount = cents / 100;
var $form = $checkout.find('form.paypal_checkout.' + getFreq());
var $form = $checkout.find('form.paypal_checkout.' + freq);
$form.find('input.amount').val('' + amount);
($form[0] as HTMLFormElement).submit();
$checkout.find('.service').html(lichess.spinnerHtml);
@ -74,14 +70,9 @@ export default function (publicKey: string) {
alert(error);
};
$checkout.find('button.stripe').on('click', function () {
let freq = getFreq(),
amount: number;
if (freq == 'lifetime') {
amount = lifetime.cents;
} else {
var $input = $checkout.find('group.amount input:checked');
amount = parseInt($input.data('amount'));
}
const freq = getFreq(),
amount =
freq == 'lifetime' ? lifetime.cents : parseInt($checkout.find('group.amount input:checked').data('amount'));
if (amount < min || amount > max) return;
$checkout.find('.service').html(lichess.spinnerHtml);

View File

@ -13,10 +13,9 @@ function startUserAnalysis(cfg) {
}
function startAnalyse(cfg) {
let analyse;
lichess.socket = new StrongSocket(cfg.socketUrl || '/analysis/socket/v5', cfg.socketVersion, {
receive: (t: string, d: any) => analyse.socketReceive(t, d),
});
cfg.socketSend = li.socket.send;
analyse = window.LichessAnalyse.start(cfg);
const analyse = window.LichessAnalyse.start(cfg);
}

View File

@ -1,7 +1,6 @@
import { init, VNode, classModule, attributesModule } from 'snabbdom';
import { init, classModule, attributesModule } from 'snabbdom';
import { Chessground } from 'chessground';
import { TournamentOpts } from './interfaces';
import TournamentController from './ctrl';
import LichessChat from 'chat';
const patch = init([classModule, attributesModule]);
@ -11,8 +10,6 @@ import makeCtrl from './ctrl';
import view from './view/main';
export default function (opts: TournamentOpts) {
let vnode: VNode, ctrl: TournamentController;
$('body').data('tournament-id', opts.data.id);
lichess.socket = new lichess.StrongSocket(`/tournament/${opts.data.id}/socket/v5`, opts.data.socketVersion, {
receive: (t: string, d: any) => ctrl.socket.receive(t, d),
@ -23,15 +20,15 @@ export default function (opts: TournamentOpts) {
opts.$side = $('.tour__side').clone();
opts.$faq = $('.tour__faq').clone();
function redraw() {
vnode = patch(vnode, view(ctrl));
}
ctrl = new makeCtrl(opts, redraw);
const ctrl = new makeCtrl(opts, redraw);
const blueprint = view(ctrl);
opts.element.innerHTML = '';
vnode = patch(opts.element, blueprint);
let vnode = patch(opts.element, blueprint);
function redraw() {
vnode = patch(vnode, view(ctrl));
}
}
// that's for the rest of lichess to access chessground

View File

@ -16,12 +16,12 @@ export function app(element: HTMLElement, env: any) {
};
});
let vnode: VNode,
ctrl: Ctrl = {
data: env.data,
trans: lichess.trans(env.i18n),
};
const ctrl: Ctrl = {
data: env.data,
trans: lichess.trans(env.i18n),
};
let vnode: VNode;
function redraw() {
vnode = patch(vnode || element, view(ctrl));
}

View File

@ -127,11 +127,9 @@ function timeString(hour) {
}
function makeGroups(days: Date[]): Date[][] {
const groups: Date[][] = [];
let i: number,
j: number,
const groups: Date[][] = [],
chunk = 10;
for (i = 0, j = days.length; i < j; i += chunk) groups.push(days.slice(i, i + chunk));
for (let i = 0; i < days.length; i += chunk) groups.push(days.slice(i, i + chunk));
return groups;
}

View File

@ -12,12 +12,12 @@ export default function (env: any) {
const element = document.querySelector('.tour-chart') as HTMLElement;
let vnode: VNode,
ctrl = {
data: () => env.data,
trans: lichess.trans(env.i18n),
};
const ctrl = {
data: () => env.data,
trans: lichess.trans(env.i18n),
};
let vnode: VNode;
function redraw() {
vnode = patch(vnode || element, view(ctrl));
}

View File

@ -13,8 +13,8 @@ export function findInMainline(fromNode: Tree.Node, predicate: (node: Tree.Node)
// returns a list of nodes collected from the original one
export function collect(from: Tree.Node, pickChild: (node: Tree.Node) => Tree.Node | undefined): Tree.Node[] {
let nodes = [from],
n = from,
const nodes = [from];
let n = from,
c;
while ((c = pickChild(n))) {
nodes.push(c);