improve memory mgmt

This commit is contained in:
Rene
2022-07-12 13:12:45 +02:00
parent a4482f96d6
commit fe52903ad8
14 changed files with 987 additions and 1067 deletions
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
import { rAF, cAF, isEmptyArray, indexOf, createCache, runEach, push } from 'support'; import { rAF, cAF, isEmptyArray, indexOf, createCache, runEachAndClear, push } from 'support';
// import { getEnvironment } from 'environment'; // import { getEnvironment } from 'environment';
/** /**
@@ -36,7 +36,7 @@ const createAutoUpdateLoop = (): AutoUpdateLoop => {
loopId = rAF!(loop); loopId = rAF!(loop);
const [value, changed, previous] = updateTimeCache(newTime || performance.now()); const [value, changed, previous] = updateTimeCache(newTime || performance.now());
if (changed) { if (changed) {
runEach(loopFunctions, value - previous!); runEachAndClear(loopFunctions, value - previous!);
} }
} }
}; };
@@ -8,7 +8,7 @@ import {
offsetSize, offsetSize,
scrollLeft, scrollLeft,
scrollTop, scrollTop,
runEach, runEachAndClear,
prependChildren, prependChildren,
removeElements, removeElements,
on, on,
@@ -267,7 +267,7 @@ export const createSizeObserver = (
prependChildren(target, sizeObserver); prependChildren(target, sizeObserver);
return () => { return () => {
runEach(offListeners); runEachAndClear(offListeners);
removeElements(sizeObserver); removeElements(sizeObserver);
}; };
}; };
@@ -3,7 +3,7 @@ import {
CacheValues, CacheValues,
createDiv, createDiv,
offsetSize, offsetSize,
runEach, runEachAndClear,
prependChildren, prependChildren,
removeElements, removeElements,
createCache, createCache,
@@ -74,7 +74,7 @@ export const createTrinsicObserver = (
prependChildren(target, trinsicObserver); prependChildren(target, trinsicObserver);
return () => { return () => {
runEach(offListeners); runEachAndClear(offListeners);
removeElements(trinsicObserver); removeElements(trinsicObserver);
}; };
}; };
@@ -12,7 +12,7 @@ import {
removeClass, removeClass,
hasClass, hasClass,
push, push,
runEach, runEachAndClear,
insertBefore, insertBefore,
attr, attr,
keys, keys,
@@ -250,5 +250,5 @@ export const createStructureSetupElements = (
} }
}; };
return [evaluatedTargetObj, appendElements, runEach.bind(0, destroyFns)]; return [evaluatedTargetObj, appendElements, runEachAndClear.bind(0, destroyFns)];
}; };
@@ -1,5 +1,5 @@
import { isUndefined } from 'support/utils/types'; import { isUndefined } from 'support/utils/types';
import { each, push, runEach } from 'support/utils/array'; import { each, push, runEachAndClear } from 'support/utils/array';
let passiveEventsSupport: boolean; let passiveEventsSupport: boolean;
const supportPassiveEvents = (): boolean => { const supportPassiveEvents = (): boolean => {
@@ -74,18 +74,20 @@ export const on = <T extends Event = Event>(
: capture; : capture;
each(splitEventNames(eventNames), (eventName) => { each(splitEventNames(eventNames), (eventName) => {
const finalListener = (once const finalListener = (
? (evt: T) => { once
target.removeEventListener(eventName, finalListener, capture); ? (evt: T) => {
listener && listener(evt); target.removeEventListener(eventName, finalListener, capture);
} listener && listener(evt);
: listener) as EventListener; }
: listener
) as EventListener;
push(offListeners, off.bind(null, target, eventName, finalListener, capture)); push(offListeners, off.bind(null, target, eventName, finalListener, capture));
target.addEventListener(eventName, finalListener, nativeOptions); target.addEventListener(eventName, finalListener, nativeOptions);
}); });
return runEach.bind(0, offListeners); return runEachAndClear.bind(0, offListeners);
}; };
/** /**
@@ -104,14 +104,21 @@ export const isEmptyArray = (array: any[] | null | undefined): boolean =>
/** /**
* Calls all functions in the passed array/set of functions. * Calls all functions in the passed array/set of functions.
* @param arr The array filled with function which shall be called. * @param arr The array filled with function which shall be called.
* @param p1 The first param. * @param args The args with which each function is called.
* @param keep True when the Set / array should not be cleared afterwards, false otherwise.
*/ */
export const runEach = (arr: ArrayLike<RunEachItem> | Set<RunEachItem>, args?: any[]): void => { export const runEachAndClear = (
arr: ArrayLike<RunEachItem> | Set<RunEachItem>,
args?: any[],
keep?: boolean
): void => {
// eslint-disable-next-line prefer-spread // eslint-disable-next-line prefer-spread
const runFn = (fn: RunEachItem) => fn && fn.apply(undefined, args || []); const runFn = (fn: RunEachItem) => fn && fn.apply(undefined, args || []);
if (arr instanceof Set) { if (arr instanceof Set) {
arr.forEach(runFn); arr.forEach(runFn);
!keep && arr.clear();
} else { } else {
each(arr, runFn); each(arr, runFn);
!keep && (arr as any[]).splice && (arr as any[]).splice(0, arr.length);
} }
}; };
@@ -1,4 +1,4 @@
import { push, each, from, indexOf, runEach, isEmptyArray } from 'support/utils/array'; import { push, each, from, indexOf, runEachAndClear, isEmptyArray } from 'support/utils/array';
describe('array utilities', () => { describe('array utilities', () => {
describe('push', () => { describe('push', () => {
@@ -289,25 +289,29 @@ describe('array utilities', () => {
}); });
}); });
describe('runEach', () => { describe('runEachAndClear', () => {
test('array', () => { test('array', () => {
const arr = [jest.fn(), null, jest.fn(), undefined, jest.fn()]; const arr = [jest.fn(), null, jest.fn(), undefined, jest.fn()];
runEach(arr, ['a', 'b', 'c', 'd']); runEachAndClear(arr, ['a', 'b', 'c', 'd'], true);
arr.forEach((fn) => { arr.forEach((fn) => {
if (fn) { if (fn) {
expect(fn).toHaveBeenCalledWith('a', 'b', 'c', 'd'); expect(fn).toHaveBeenCalledWith('a', 'b', 'c', 'd');
} }
}); });
runEachAndClear(arr, ['a', 'b', 'c', 'd']);
expect(arr.length).toBe(0);
}); });
test('set', () => { test('set', () => {
const set = new Set([jest.fn(), null, jest.fn(), undefined, jest.fn()]); const set = new Set([jest.fn(), null, jest.fn(), undefined, jest.fn()]);
runEach(set, [1, 2, 3, 4]); runEachAndClear(set, [1, 2, 3, 4], true);
set.forEach((fn) => { set.forEach((fn) => {
if (fn) { if (fn) {
expect(fn).toHaveBeenCalledWith(1, 2, 3, 4); expect(fn).toHaveBeenCalledWith(1, 2, 3, 4);
} }
}); });
runEachAndClear(set, [1, 2, 3, 4]);
expect(set.size).toBe(0);
}); });
}); });
@@ -11,7 +11,7 @@ import { addClass, each, isArray, removeAttr, style } from 'support';
window.OverlayScrollbars = OverlayScrollbars; window.OverlayScrollbars = OverlayScrollbars;
OverlayScrollbars.env().setDefaultOptions({ OverlayScrollbars.env().setDefaultOptions({
nativeScrollbarsOverlaid: { initialize: false }, nativeScrollbarsOverlaid: { initialize: true },
}); });
const startBtn: HTMLButtonElement | null = document.querySelector('#start'); const startBtn: HTMLButtonElement | null = document.querySelector('#start');