add waitUntil time util function (thanks Doggybootsy)

pull/95/head
dperolio 2 years ago
parent fa6527eff9
commit 7a7e05e82b
No known key found for this signature in database
GPG Key ID: 4191689562D51409

@ -4,23 +4,16 @@
* @memberof Builtin.Attributes.Components.Modals
*/
import { getModule } from '@vizality/webpack';
import { patch, unpatch } from '@vizality/patcher';
import { findInTree } from '@vizality/util/react';
import { waitUntil } from '@vizality/util/time';
import { getModule } from '@vizality/webpack';
export const labels = [ 'Components', 'Modals', 'User' ];
const sleep = (time) => new Promise(resolve => setTimeout(resolve, time))
async function waitUntil(condition) {
let item
while (!(item = condition())) await sleep(1)
return item
}
export default builtin => {
(async () => {
const UserProfile = await waitUntil(() => getModule(m => m.default?.displayName === 'UserProfileModal'));
console.log(UserProfile);
patch('vz-attributes-modal-user', UserProfile, 'default', ([ props ], res) => {
try {
res.ref = elem => {
@ -35,6 +28,6 @@ export default builtin => {
return builtin.error(builtin._labels.concat(labels), err);
}
});
})()
})();
return () => unpatch('vz-attributes-modal-user');
};

@ -17,9 +17,9 @@ const _warn = (labels, ...message) => warn({ labels, message });
const _error = (labels, ...message) => error({ labels, message });
/**
*
* @param {*} ms
* @returns
*
* @param {*} ms
* @returns
*/
export const millisecondsToTime = ms => {
try {
@ -72,11 +72,11 @@ export const assertDate = input => {
};
/**
*
* @param {*} time
* @returns
*
* @param {*} time
* @returns
*/
export const sleep = async time => {
export async function sleep (time) {
try {
return new Promise(resolve =>
setTimeout(resolve, time)
@ -84,5 +84,18 @@ export const sleep = async time => {
} catch (err) {
return _error(_labels.concat('sleep'), err);
}
};
}
/**
*
* @param {*} condition
* @param {*} delay
* @returns
*/
export async function waitUntil (condition, delay = 1) {
let item;
while (!(item = condition())) {
await sleep(delay);
}
return item;
}

Loading…
Cancel
Save