refactor/code-splitting #31
@@ -1,4 +1,5 @@
|
||||
import { SvelteMap } from 'svelte/reactivity';
|
||||
import { getEffectiveConcurrency } from './utils/getEffectiveConcurrency/getEffectiveConcurrency';
|
||||
|
||||
/** Loading state of a font. Failed loads may be retried up to MAX_RETRIES. */
|
||||
export type FontStatus = 'loading' | 'loaded' | 'error';
|
||||
@@ -200,7 +201,7 @@ export class AppliedFontsManager {
|
||||
}
|
||||
|
||||
// Phase 1: Concurrent fetching (I/O bound, non-blocking)
|
||||
const concurrency = this.#getEffectiveConcurrency();
|
||||
const concurrency = getEffectiveConcurrency();
|
||||
const buffers = new Map<string, ArrayBuffer>();
|
||||
|
||||
for (let i = 0; i < entries.length; i += concurrency) {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import {
|
||||
beforeEach,
|
||||
describe,
|
||||
expect,
|
||||
it,
|
||||
} from 'vitest';
|
||||
import {
|
||||
Concurrency,
|
||||
getEffectiveConcurrency,
|
||||
} from './getEffectiveConcurrency';
|
||||
|
||||
describe('getEffectiveConcurrency', () => {
|
||||
beforeEach(() => {
|
||||
const nav = navigator as any;
|
||||
nav.connection = null;
|
||||
});
|
||||
|
||||
it('should return MAX when connection is not available', () => {
|
||||
const nav = navigator as any;
|
||||
nav.connection = null;
|
||||
expect(getEffectiveConcurrency()).toBe(Concurrency.MAX);
|
||||
});
|
||||
|
||||
it('should return MIN for slow-2g or 2g connection', () => {
|
||||
const nav = navigator as any;
|
||||
nav.connection = { effectiveType: 'slow-2g' };
|
||||
expect(getEffectiveConcurrency()).toBe(Concurrency.MIN);
|
||||
});
|
||||
|
||||
it('should return AVERAGE for 3g connection', () => {
|
||||
const nav = navigator as any;
|
||||
nav.connection = { effectiveType: '3g' };
|
||||
expect(getEffectiveConcurrency()).toBe(Concurrency.AVERAGE);
|
||||
});
|
||||
|
||||
it('should return MAX for other connection types', () => {
|
||||
const nav = navigator as any;
|
||||
nav.connection = { effectiveType: '4g' };
|
||||
expect(getEffectiveConcurrency()).toBe(Concurrency.MAX);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
export enum Concurrency {
|
||||
MIN = 1,
|
||||
AVERAGE = 2,
|
||||
MAX = 4,
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the amount of fonts for concurrent download based on the user internet connection
|
||||
*/
|
||||
export function getEffectiveConcurrency(): number {
|
||||
const nav = navigator as any;
|
||||
const connection = nav.connection;
|
||||
if (!connection) {
|
||||
return Concurrency.MAX;
|
||||
}
|
||||
|
||||
switch (connection.effectiveType) {
|
||||
case 'slow-2g':
|
||||
case '2g':
|
||||
return Concurrency.MIN;
|
||||
case '3g':
|
||||
return Concurrency.AVERAGE;
|
||||
default:
|
||||
return Concurrency.MAX;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user