From dea496018c2cd8a98349fb6cb70f3f8eba11d6ad Mon Sep 17 00:00:00 2001 From: Azgaar Date: Mon, 27 Jun 2022 02:21:15 +0300 Subject: [PATCH] refactor(es modules): restore old functions with TS --- src/main.js | 23 ++++++----- src/modules/religions-generator.js | 2 +- src/modules/river-generator.js | 3 +- src/modules/zoom.js | 1 + src/utils/functionUtils.ts | 62 +++++++++++++++--------------- src/utils/lineUtils.ts | 6 +-- src/utils/stringUtils.ts | 5 +++ 7 files changed, 55 insertions(+), 47 deletions(-) diff --git a/src/main.js b/src/main.js index 68ea8f58..825d00f6 100644 --- a/src/main.js +++ b/src/main.js @@ -3,18 +3,22 @@ console.log("Hello World"); -import {invokeActiveZooming} from "./modules/activeZooming"; -import {applyPreset, drawBorders, drawRivers, drawStates} from "./modules/ui/layers"; -import {applyMapSize, applyStoredOptions, randomizeOptions} from "./modules/ui/options"; +import "./components"; import {ERROR, INFO, TIME, WARN} from "./config/logging"; import {UINT16_MAX} from "./constants"; +import {invokeActiveZooming} from "./modules/activeZooming"; import {clearLegend} from "./modules/legend"; import {drawScaleBar, Ruler, Rulers} from "./modules/measurers"; +import {applyPreset, drawBorders, drawRivers, drawStates} from "./modules/ui/layers"; +import {applyMapSize, applyStoredOptions, randomizeOptions} from "./modules/ui/options"; import {applyStyleOnLoad} from "./modules/ui/stylePresets"; import {restoreDefaultEvents} from "./scripts/events"; import {addGlobalListeners} from "./scripts/listeners"; import {locked} from "./scripts/options/lock"; import {clearMainTip, tip} from "./scripts/tooltips"; +import {createTypedArray} from "./utils/arrayUtils"; +import {parseError} from "./utils/errorUtils"; +import {debounce} from "./utils/functionUtils"; import { calculateVoronoi, findCell, @@ -23,15 +27,12 @@ import { isLand, shouldRegenerateGrid } from "./utils/graphUtils"; -import {parseError} from "./utils/errorUtils"; -import {rn, minmax, normalize} from "./utils/numberUtils"; -import {createTypedArray} from "./utils/arrayUtils"; -import {clipPoly} from "./utils/lineUtils"; -import {rand, P, gauss, ra, rw, generateSeed} from "./utils/probabilityUtils"; import {getAdjective} from "./utils/languageUtils"; -import {debounce} from "./utils/functionUtils"; +import {clipPoly} from "./utils/lineUtils"; +import {minmax, normalize, rn} from "./utils/numberUtils"; +import {gauss, generateSeed, P, ra, rand, rw} from "./utils/probabilityUtils"; import {byId} from "./utils/shorthands"; -import "./components"; +import {round} from "./utils/stringUtils"; addGlobalListeners(); @@ -363,8 +364,6 @@ async function generate(options) { applyMapSize(); randomizeOptions(); - debugger; - if (shouldRegenerateGrid(grid)) grid = precreatedGraph || generateGrid(); else delete grid.cells.h; grid.cells.h = await HeightmapGenerator.generate(grid); diff --git a/src/modules/religions-generator.js b/src/modules/religions-generator.js index c48e3408..a7b03d55 100644 --- a/src/modules/religions-generator.js +++ b/src/modules/religions-generator.js @@ -3,7 +3,7 @@ import {findAll} from "/src/utils/graphUtils"; import {unique} from "/src/utils/arrayUtils"; import {getRandomColor, getMixedColor} from "/src/utils/colorUtils"; import {rn} from "/src/utils/numberUtils"; -import {rand, P, ra, rw, biased} from "/src/utils/probabilityUtils"; +import {rand, P, ra, rw, biased, gauss} from "/src/utils/probabilityUtils"; import {trimVowels, getAdjective, abbreviate} from "/src/utils/languageUtils"; window.Religions = (function () { diff --git a/src/modules/river-generator.js b/src/modules/river-generator.js index dfab658b..34913653 100644 --- a/src/modules/river-generator.js +++ b/src/modules/river-generator.js @@ -1,7 +1,8 @@ -import {TIME} from "/src/config/logging"; +import {TIME, WARN} from "/src/config/logging"; import {last} from "/src/utils/arrayUtils"; import {rn} from "/src/utils/numberUtils"; import {round} from "/src/utils/stringUtils"; +import {rw, each} from "/src/utils/probabilityUtils"; window.Rivers = (function () { const generate = function (allowErosion = true) { diff --git a/src/modules/zoom.js b/src/modules/zoom.js index 4e7f2b73..990b08c5 100644 --- a/src/modules/zoom.js +++ b/src/modules/zoom.js @@ -7,6 +7,7 @@ window.viewY = 0; window.Zoom = (function () { function onZoom() { + if (!d3.event?.transform) return; const {k, x, y} = d3.event.transform; const isScaleChanged = Boolean(scale - k); diff --git a/src/utils/functionUtils.ts b/src/utils/functionUtils.ts index 909aedf5..6a5e6b0a 100644 --- a/src/utils/functionUtils.ts +++ b/src/utils/functionUtils.ts @@ -32,41 +32,43 @@ export function rollups( return nest(values, Array.from, reduce, keys); } -export function debounce any>(func: T, waitFor: number) { - let timeout: ReturnType; - return (...args: Parameters): ReturnType => { - let result: any; - timeout && clearTimeout(timeout); - timeout = setTimeout(() => { - result = func(...args); - }, waitFor); - return result; +export function debounce(func: Function, ms: number) { + let isCooldown = false; + + return function (this: unknown, ...args: unknown[]) { + if (isCooldown) return; + func.apply(this, args); + isCooldown = true; + setTimeout(() => (isCooldown = false), ms); }; } -export function throttle(func: Function, waitFor: number = 300) { - let inThrottle: boolean; - let lastFn: ReturnType; - let lastTime: number; +export function throttle(func: Function, ms: number) { + let isThrottled = false; + let savedArgs: unknown[]; + let savedThis: unknown; - return function (this: any) { - const context = this; - const args = arguments; - - if (!inThrottle) { - func.apply(context, args); - lastTime = Date.now(); - inThrottle = true; - } else { - clearTimeout(lastFn); - lastFn = setTimeout(() => { - if (Date.now() - lastTime >= waitFor) { - func.apply(context, args); - lastTime = Date.now(); - } - }, Math.max(waitFor - (Date.now() - lastTime), 0)); + function wrapper(this: unknown, ...args: unknown[]) { + if (isThrottled) { + savedArgs = args; + savedThis = this; + return; } - }; + + func.apply(this, args); + isThrottled = true; + + setTimeout(function () { + isThrottled = false; + if (savedArgs) { + wrapper.apply(savedThis, savedArgs); + savedArgs = []; + savedThis = null; + } + }, ms); + } + + return wrapper; } export function getBase64(url: string, callback: (base64: string | ArrayBuffer | null) => void) { diff --git a/src/utils/lineUtils.ts b/src/utils/lineUtils.ts index 99168c92..50078159 100644 --- a/src/utils/lineUtils.ts +++ b/src/utils/lineUtils.ts @@ -1,10 +1,9 @@ import {polygon} from "lineclip"; -const {graphWidth, graphHeight, pack} = window; - // clip polygon by graph bbox export function clipPoly(points: TPoints) { - return polygon(points, [0, 0, graphWidth, graphHeight]); + // @ts-expect-error graphWidth/graphWidth are global variables + return polygon(points, [0, 0, graphWidth, graphWidth]); } // get segment of any point on polyline @@ -40,6 +39,7 @@ export function getSegmentId(points: TPoints, point: TPoint, step = 10) { // return center point of common edge of 2 pack cells export function getMiddlePoint(cell1: number, cell2: number) { + // @ts-expect-error pack is global variable const {cells, vertices} = pack; const commonVertices = cells.v[cell1].filter((vertex: number) => diff --git a/src/utils/stringUtils.ts b/src/utils/stringUtils.ts index b8275d8d..ffb5080b 100644 --- a/src/utils/stringUtils.ts +++ b/src/utils/stringUtils.ts @@ -1,7 +1,12 @@ +import {ERROR} from "../config/logging"; import {rn} from "./numberUtils"; // round numbers in string to d decimals export function round(str: string, d = 1) { + if (!str) { + ERROR && console.error("Path is empty", str); + return ""; + } return str.replace(/[\d\.-][\d\.e-]*/g, n => String(rn(+n, d))); }