Improve proxyLazy
This commit is contained in:
parent
70103e8111
commit
cfefdbc7a7
|
@ -7,15 +7,20 @@
|
||||||
export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
|
export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
|
||||||
let tries = 0;
|
let tries = 0;
|
||||||
let cache: T;
|
let cache: T;
|
||||||
return () => {
|
|
||||||
|
const getter = () => {
|
||||||
if (!cache && attempts > tries++) {
|
if (!cache && attempts > tries++) {
|
||||||
cache = factory();
|
cache = factory();
|
||||||
if (!cache && attempts === tries) {
|
if (!cache && attempts === tries) {
|
||||||
console.error(`Lazy factory failed:\n${factory}`);
|
console.error(`Lazy factory failed:\n\n${factory}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cache;
|
return cache;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getter.$$vencordLazyFailed = () => tries >= attempts;
|
||||||
|
|
||||||
|
return getter;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proxies demand that these properties be unmodified, so proxyLazy
|
// Proxies demand that these properties be unmodified, so proxyLazy
|
||||||
|
@ -54,20 +59,20 @@ const proxyLazyCache = Symbol.for("vencord.lazy.cached");
|
||||||
* @returns Result of factory function
|
* @returns Result of factory function
|
||||||
*/
|
*/
|
||||||
export function proxyLazy<T = any>(factory: () => T, attempts = 5, isChild = false): T {
|
export function proxyLazy<T = any>(factory: () => T, attempts = 5, isChild = false): T {
|
||||||
const get = makeLazy(factory, attempts);
|
const get = makeLazy(factory, attempts) as any;
|
||||||
|
|
||||||
let isSameTick = true;
|
let isSameTick = true;
|
||||||
if (!isChild) setTimeout(() => isSameTick = false, 0);
|
if (!isChild) setTimeout(() => isSameTick = false, 0);
|
||||||
|
|
||||||
let failed = false;
|
|
||||||
const proxyDummy = Object.assign(function () { }, {
|
const proxyDummy = Object.assign(function () { }, {
|
||||||
[proxyLazyGet]() {
|
[proxyLazyGet]() {
|
||||||
if (!proxyDummy[proxyLazyCache] && !failed) {
|
if (!proxyDummy[proxyLazyCache]) {
|
||||||
proxyDummy[proxyLazyCache] = get();
|
if (!get.$$vencordLazyFailed()) {
|
||||||
|
proxyDummy[proxyLazyCache] = get();
|
||||||
|
}
|
||||||
|
|
||||||
if (!proxyDummy[proxyLazyCache]) {
|
if (!proxyDummy[proxyLazyCache]) {
|
||||||
failed = true;
|
throw new Error(`proxyLazy factory failed:\n\n${factory}`);
|
||||||
throw new Error(`proxyLazy factory failed:\n${factory}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue