remove dropThrottle

pull/3820/head
Niklas Fiekas 2017-11-24 01:25:40 +01:00
parent 30385b6c41
commit 7e16cdc319
4 changed files with 33 additions and 63 deletions

View File

@ -10,7 +10,7 @@ import { enrichText, innerHTML } from '../util';
import { path as treePath } from 'tree';
import column from './columnView';
import inline from './inlineView';
import { empty, defined, dropThrottle, storedProp, StoredProp } from 'common';
import { empty, defined, throttle, storedProp, StoredProp } from 'common';
export interface Ctx {
ctrl: AnalyseCtrl;
@ -142,20 +142,16 @@ function eventPath(e: MouseEvent): Tree.Path | null {
((e.target as HTMLElement).parentNode as HTMLElement).getAttribute('p');
}
const scrollThrottle = dropThrottle(200);
export function autoScroll(ctrl: AnalyseCtrl, el: HTMLElement): void {
scrollThrottle(() => {
const cont = el.parentNode as HTMLElement;
if (!cont) return;
const target = el.querySelector('.active') as HTMLElement;
if (!target) {
cont.scrollTop = ctrl.path ? 99999 : 0;
return;
}
cont.scrollTop = target.offsetTop - cont.offsetHeight / 2 + target.offsetHeight;
});
}
export const autoScroll = throttle(200, (ctrl: AnalyseCtrl, el: HTMLElement) => {
const cont = el.parentNode as HTMLElement;
if (!cont) return;
const target = el.querySelector('.active') as HTMLElement;
if (!target) {
cont.scrollTop = ctrl.path ? 99999 : 0;
return;
}
cont.scrollTop = target.offsetTop - cont.offsetHeight / 2 + target.offsetHeight;
});
export function nonEmpty(x: any): boolean {
return !!x;

View File

@ -107,21 +107,3 @@ export function throttle(delay: number, callback: (...args: any[]) => void): (..
else timer = setTimeout(exec, delay - elapsed);
};
}
export type F = () => void;
export function dropThrottle(delay: number): (f: F) => void {
let task: F | undefined;
const run = function(f: F) {
task = f;
f();
setTimeout(function() {
if (task !== f) run(task!);
else task = undefined;
}, delay);
};
return function(f) {
if (task) task = f;
else run(f);
};
}

View File

@ -1,23 +1,19 @@
import { h } from 'snabbdom'
import { VNode } from 'snabbdom/vnode'
import { defined, dropThrottle } from 'common';
import { defined, throttle } from 'common';
import { renderEval as normalizeEval } from 'chess';
import { path as treePath } from 'tree';
import { MaybeVNodes } from '../interfaces';
const scrollThrottle = dropThrottle(150);
function autoScroll(ctrl, el) {
scrollThrottle(function() {
var cont = el.parentNode;
var target = el.querySelector('.active');
if (!target) {
cont.scrollTop = ctrl.vm.path === treePath.root ? 0 : 99999;
return;
}
cont.scrollTop = target.offsetTop - cont.offsetHeight / 2 + target.offsetHeight;
});
}
const autoScroll = throttle(150, (ctrl, el) => {
var cont = el.parentNode;
var target = el.querySelector('.active');
if (!target) {
cont.scrollTop = ctrl.vm.path === treePath.root ? 0 : 99999;
return;
}
cont.scrollTop = target.offsetTop - cont.offsetHeight / 2 + target.offsetHeight;
});
function pathContains(ctx, path) {
return treePath.contains(ctx.ctrl.vm.path, path);

View File

@ -1,7 +1,7 @@
import { h } from 'snabbdom'
import { VNode, VNodeData } from 'snabbdom/vnode'
import * as round from '../round';
import { dropThrottle } from 'common';
import { throttle } from 'common';
import { game, status, router, view as gameView } from 'game';
import * as util from '../util';
import RoundController from '../ctrl';
@ -14,21 +14,17 @@ function nullMove() {
return h('move.empty', '');
}
const scrollThrottle = dropThrottle(100);
function autoScroll(el: HTMLElement, ctrl: RoundController) {
scrollThrottle(function() {
if (ctrl.data.steps.length < 7) return;
let st: number | undefined = undefined;
if (ctrl.ply < 3) st = 0;
else if (ctrl.ply >= round.lastPly(ctrl.data) - 1) st = 9999;
else {
const plyEl = el.querySelector('.active') as HTMLElement | undefined;
if (plyEl) st = plyEl.offsetTop - el.offsetHeight / 2 + plyEl.offsetHeight / 2;
}
if (st !== undefined) el.scrollTop = st;
});
}
const autoScroll = throttle(100, (el: HTMLElement, ctrl: RoundController) => {
if (ctrl.data.steps.length < 7) return;
let st: number | undefined = undefined;
if (ctrl.ply < 3) st = 0;
else if (ctrl.ply >= round.lastPly(ctrl.data) - 1) st = 9999;
else {
const plyEl = el.querySelector('.active') as HTMLElement | undefined;
if (plyEl) st = plyEl.offsetTop - el.offsetHeight / 2 + plyEl.offsetHeight / 2;
}
if (st !== undefined) el.scrollTop = st;
});
function renderMove(step: Step, curPly: number, orEmpty?: boolean) {
if (!step) return orEmpty ? emptyMove() : nullMove();