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';
/**
@@ -36,7 +36,7 @@ const createAutoUpdateLoop = (): AutoUpdateLoop => {
loopId = rAF!(loop);
const [value, changed, previous] = updateTimeCache(newTime || performance.now());
if (changed) {
runEach(loopFunctions, value - previous!);
runEachAndClear(loopFunctions, value - previous!);
}
}
};
@@ -8,7 +8,7 @@ import {
offsetSize,
scrollLeft,
scrollTop,
runEach,
runEachAndClear,
prependChildren,
removeElements,
on,
@@ -267,7 +267,7 @@ export const createSizeObserver = (
prependChildren(target, sizeObserver);
return () => {
runEach(offListeners);
runEachAndClear(offListeners);
removeElements(sizeObserver);
};
};
@@ -3,7 +3,7 @@ import {
CacheValues,
createDiv,
offsetSize,
runEach,
runEachAndClear,
prependChildren,
removeElements,
createCache,
@@ -74,7 +74,7 @@ export const createTrinsicObserver = (
prependChildren(target, trinsicObserver);
return () => {
runEach(offListeners);
runEachAndClear(offListeners);
removeElements(trinsicObserver);
};
};
@@ -12,7 +12,7 @@ import {
removeClass,
hasClass,
push,
runEach,
runEachAndClear,
insertBefore,
attr,
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 { each, push, runEach } from 'support/utils/array';
import { each, push, runEachAndClear } from 'support/utils/array';
let passiveEventsSupport: boolean;
const supportPassiveEvents = (): boolean => {
@@ -74,18 +74,20 @@ export const on = <T extends Event = Event>(
: capture;
each(splitEventNames(eventNames), (eventName) => {
const finalListener = (once
? (evt: T) => {
target.removeEventListener(eventName, finalListener, capture);
listener && listener(evt);
}
: listener) as EventListener;
const finalListener = (
once
? (evt: T) => {
target.removeEventListener(eventName, finalListener, capture);
listener && listener(evt);
}
: listener
) as EventListener;
push(offListeners, off.bind(null, target, eventName, finalListener, capture));
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.
* @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
const runFn = (fn: RunEachItem) => fn && fn.apply(undefined, args || []);
if (arr instanceof Set) {
arr.forEach(runFn);
!keep && arr.clear();
} else {
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('push', () => {
@@ -289,25 +289,29 @@ describe('array utilities', () => {
});
});
describe('runEach', () => {
describe('runEachAndClear', () => {
test('array', () => {
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) => {
if (fn) {
expect(fn).toHaveBeenCalledWith('a', 'b', 'c', 'd');
}
});
runEachAndClear(arr, ['a', 'b', 'c', 'd']);
expect(arr.length).toBe(0);
});
test('set', () => {
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) => {
if (fn) {
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;
OverlayScrollbars.env().setDefaultOptions({
nativeScrollbarsOverlaid: { initialize: false },
nativeScrollbarsOverlaid: { initialize: true },
});
const startBtn: HTMLButtonElement | null = document.querySelector('#start');