frdl.when

- Asynchronous Promise
Aus der Kategorie: Resources

window.frdl.when

function(condition, _then, withMe, _or, delayed, initFn)

Checks a given scope for a specific condition (by condition || function) repeatedly within a configurable interval and triggers a thenable if the condition evaluates to true by calling a given callback function applying a given scope variable as argument. The returned Promise provides a ["magic function"].__call(name, args) to provide some kind of "overloading".

@returns instanceof Promise




Example

The following snippet triggers an alert and resolves to true if the name of the window changes (checks for the change every 1 microsecond and should trigger after 5 seconds).


var NumChecks = -1;
var oldWindowName = window.name;
var AlteredWindowName = frdl.when(function(wn){
        NumChecks++;
        return (wn !== oldWindowName || oldWindowName !== window.name) ? true : false;
    }, function(wn){
       alert('The window.name changed from "'+oldWindowName+'" to "'+window.name+'"!');
       return true;   
    }, window.name, function(wn){
        console.warn('Error oberving window.name.onchange thenable in frdl.when! '
        + oldWindowName + ' to '+wn);   
      return false; 
    }, 1, function(wn){
        NumChecks++;
        setTimeout(function(){
            /**
            * Our init function changes the window.name
            * after some seconds to make the when happen for our test!
            */
            window.name = oldWindowName + ' checked ' + NumChecks + ' times';
        },5000);
});

 

Core

/**
*  if delayed is a function, delayed is the initial Function
*  if delayed AND initFn are functions, delayed is the initial function and initFn is called each iteration of check(condition) function
*  if initFn is a function and delayed is NOT a function, initFn is the initial Function
*  otherwise there is no initial function or additional check(condition) function
*
*  promise resolves if condition is true
*  promise rejects if condition is undefined || null
*
*  condition should be either a function or boolean expression
*/
_.when=function(condition, _then, withMe, _or, delayed /* !isNaN || typeof function */, initFn /* typeof function || undefined */){


    if('function'===typeof delayed && 'function'!==typeof initFn){
        var init = delayed;
        delayed=(isNaN(initFn)) ? 1 : initFn;
    } else if ('function'===typeof delayed && 'function'===typeof initFn){
        var fN = initFn;
        var init =  new delayed;
        delayed = 1;
    } else if( 'function'===typeof initFn){
        var init = initFn;
    }



     var check = function(condition, me){
         if('function'===typeof fN)fN(me);
         return (    ( 'function'===typeof condition && true === condition(me) )
                 || (true === (condition))
               ) ? true : condition;
     };

    if('function'===typeof init){
        init(withMe);
    }
   
    delayed=(isNaN(delayed)) ? 1 : delayed;
        
    if(true===check(condition, withMe))return _then(withMe);

  var p = new Promise(function(resolve, reject){
          function h() {
              var c = check(condition, withMe);
            if (true===c) {
                resolve(withMe);
            /* } else if (false!==c && true!==c) { */
             } else if (undefined===c || null ===c) {
                reject(withMe);
              }else{
                   setTimeout(function(){
                         h();
                   },(delayed || 1));                 
              }
          }
         
          h();
         
  });
 
  p.then(function(withMe) {
           return _then(withMe);
      }, function(withMe) {
          return _or(withMe);
  });   

   var rProxy = _.overload(p, function(name, args){
          if('function'===typeof withMe[name]){
             return withMe[name](Array.prototype.slice.call(args));
          }else{
             setTimeout(function(){
                   rProxy.__call(name, args);
             },1000);
       }
   });
  
   return rProxy;   
};






Erstellt von WEBFAN (Monday 1st of August 2016 09:04:07 AM - vor 238,39 Tagen)
in der Kategorie Resources als statische Seite
Zuletzt geändert: Monday 1st of August 2016 10:01:30 AM von WEBFAN
Der Beitrag wurde insgesamt 446 mal gelesen (durchschnittlich 1,87 mal am Tag)

Bewertung des Beitrages: - Noch keine Bewertung - von 10 Punkten (bei 0 Stimmen)

Für Benachrichtigung über neue Beiträge aus der Kategorie Resources:
Jetzt kostenlos als Benutzer von "frdl" registrieren...!

Kommentar zu diesem Beitrag verfassen:

Dein Name (* Pflichtfeld):


Deine Website:
(mit http://)


Deine E-Mail Adresse:
(Nicht öffentlich, wird intern bei Webfan gespeichert, aber niemandem angezeigt.)


Track Back Url:
(mit http:// - Auf dieser Url hast Du auf den vorliegenden Artikel verlinkt)


Bewertung des Artikels abgeben:
(10=besonders gut | 0=besonders schlecht)


Dein Kommentar zu diesem Beitrag (* Pflichtfeld):

Html erlaubt: a> <b> <blockquote> <br> <center> <div> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr> <i> <img> <ul> <li> <p> <pre> <small> <sub> <sup> <table> <td> <tr> <u> <strong> <span> <nodocu> <docu> <wemc> <dl> <dt> <dd> <abbr> <em> <tbody


Bitte mit TAN C3 bestätigen:



Bewertung des Beitrages: - Noch keine Bewertung - von 10 Punkten (bei 0 Stimmen)

Kommentare zu diesem Beitrag:


- keine Kommentare zu diesem Beitrag vorhanden -