Patch When Available Library

Calls a function (getExpectedFnc()) repeatedly until it gives an expected result (confirmIsAvailableFnc()). Forwards it to (doPatchFnc()).

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/428034/941202/Patch%20When%20Available%20Library.js

// ==UserScript==
// @name         Patch When Available Library
// @namespace    hoehleg.userscripts.private
// @version      0.1
// @description  Calls a function (getExpectedFnc()) repeatedly until it gives an expected result (confirmIsAvailableFnc()). Forwards it to (doPatchFnc()).
// @author       Gerrit Höhle
// @grant        none
// ==/UserScript==
 
/* jslint esnext: true */
const patchWhenAvailable = ({ getExpectedFnc, doPatchFnc, confirmIsAvailableFnc = null, timeOutRetryMillis = 200, maxPeriodTryMillis = 5000 }) => {
    const valueOrObject = getExpectedFnc();
    const isAvailable = confirmIsAvailableFnc ? confirmIsAvailableFnc(valueOrObject) : !!valueOrObject;
    
    if (!isAvailable) {
        if (timeOutRetryMillis <= maxPeriodTryMillis) {

            setTimeout(() => {
                maxPeriodTryMillis -= timeOutRetryMillis;
                patchWhenAvailable({ getExpectedFnc, doPatchFnc, confirmIsAvailableFnc, timeOutRetryMillis, maxPeriodTryMillis });

            }, timeOutRetryMillis);
        }

        return;
    }

    doPatchFnc(valueOrObject);
};