1 /**
  2  * Copyright (C) 2012 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global core*/
 26 
 27 (function() {
 28     "use strict";
 29 
 30     /**
 31      * @return {!{forEach:!function(!Array.<*>,!function(*, !function(!string):undefined):undefined,!function(?string)):undefined, destroyAll:function(!Array.<!function(!function(!Error=))>,!function(!Error=)):undefined}}
 32      */
 33     function createASyncSingleton() {
 34         /**
 35          * @param {!Array.<*>} items
 36          * @param {!function(*, !function(!string):undefined):undefined} f
 37          * @param {!function(?string)} callback
 38          * @return {undefined}
 39          */
 40         function forEach(items, f, callback) {
 41             var i, l = items.length,
 42                 /**@type{!number}*/
 43                 itemsDone = 0;
 44             /**
 45              * @param {?string} err
 46              * @return {undefined}
 47              */
 48             function end(err) {
 49                 if (itemsDone !== l) {
 50                     if (err) {
 51                         itemsDone = l;
 52                         callback(err);
 53                     } else {
 54                         itemsDone += 1;
 55                         if (itemsDone === l) {
 56                             callback(null);
 57                         }
 58                     }
 59                 }
 60             }
 61             for (i = 0; i < l; i += 1) {
 62                 f(items[i], end);
 63             }
 64         }
 65 
 66         /**
 67          * @param {!Array.<!function(!function(!Error=))>} items
 68          * @param {!function(!Error=)} callback
 69          * @return {undefined}
 70          */
 71         function destroyAll(items, callback) {
 72             /**
 73              * @param {!number} itemIndex
 74              * @param {!Error|undefined} err
 75              * @return {undefined}
 76              */
 77             function destroy(itemIndex, err) {
 78                 if (err) {
 79                     callback(err);
 80                 } else {
 81                     if (itemIndex < items.length) {
 82                         items[itemIndex](function (err) { destroy(itemIndex + 1, err); });
 83                     } else {
 84                         callback();
 85                     }
 86                 }
 87             }
 88             destroy(0, undefined);
 89         }
 90 
 91         return {
 92             forEach: forEach,
 93             destroyAll: destroyAll
 94         };
 95     }
 96 
 97     /**
 98      * Wrapper for Async functions
 99      * @const
100      * @type {!{forEach:!function(!Array.<*>,!function(*, !function(!string):undefined):undefined,!function(?string)):undefined, destroyAll:function(!Array.<!function(!function(!Error=))>,!function(!Error=)):undefined}}
101      */
102     core.Async = createASyncSingleton();
103 }());
104