Skip to content

Commit 2619cb3

Browse files
Pistchpistch-yandex-team-ru
andauthored
chore: Make library more SSR-friendly (#90)
* chore: make library more SSR-friendly, get rid on inline 'window' calls * chore: make library more SSR-friendly, get rid on inline 'document' calls --------- Co-authored-by: pistch-yandex-team-ru <[email protected]>
1 parent b52d056 commit 2619cb3

File tree

5 files changed

+22
-7
lines changed

5 files changed

+22
-7
lines changed

src/graphConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export type TGraphConstants = {
129129
export const initGraphConstants: TGraphConstants = {
130130
system: {
131131
GRID_SIZE: 16,
132-
PIXEL_RATIO: window.devicePixelRatio || 1,
132+
PIXEL_RATIO: typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1,
133133
USABLE_RECT_GAP: 400,
134134
CAMERA_VIEWPORT_TRESHOLD: 0.5,
135135
},

src/services/optimizations/fpsManager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ class FpsManager {
3030

3131
this.averageFPS = Math.floor(sum / 10);
3232

33-
window.requestAnimationFrame(refreshLoop);
33+
if (typeof window === "undefined") {
34+
global.setTimeout(refreshLoop, 16);
35+
} else {
36+
window.requestAnimationFrame(refreshLoop);
37+
}
3438
};
3539

3640
refreshLoop();

src/services/optimizations/frameDebouncer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { scheduler } from "../../lib/Scheduler";
44

5-
const getNowTime = window.performance.now.bind(performance);
5+
const getNowTime = () => performance.now();
66

77
type Options = {
88
delay?: number;

src/utils/Emitter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { noop } from "./functions";
22

33
const TIME_PER_FRAME_FOR_GC = 5; // 5 mc
4-
const rIC = window.requestIdleCallback || window.setTimeout;
4+
const globalObject = typeof window === "undefined" ? global : window;
5+
const rIC = globalObject.requestIdleCallback || globalObject.setTimeout;
56

6-
const getTime = () => window.performance.now();
7+
const getTime = () => performance.now();
78
type EmitterEventsDefinition = Record<string, (...args: unknown[]) => void>;
89
export class Emitter<T extends EmitterEventsDefinition = EmitterEventsDefinition> {
910
private destroyed: boolean;

src/utils/functions/text.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
/* eslint-disable no-unmodified-loop-condition */
2+
import memoize from "lodash/memoize";
3+
24
export function getFontSize(fontSize, scale) {
35
return (fontSize / scale) | 0;
46
}
57

6-
const canvas: HTMLCanvasElement = document.createElement("canvas");
7-
const context: CanvasRenderingContext2D = canvas.getContext("2d");
8+
function canvasContextGetter() {
9+
const canvas: HTMLCanvasElement = document.createElement("canvas");
10+
11+
return canvas.getContext("2d");
12+
}
13+
14+
const getCanvasContext = memoize(canvasContextGetter, () => "canvasContext");
15+
816
const mapTextToMeasures: Map<string, number> = new Map();
917

1018
export function measureText(text, font, approximate = true): number {
19+
const context = getCanvasContext();
20+
1121
context.font = font;
1222

1323
if (!approximate) {

0 commit comments

Comments
 (0)