From d4a50c65c1cec8d283d03c61a668b7d3f6ac2d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=85kre=20Solberg?= <andreas.solberg@uninett.no> Date: Tue, 20 Sep 2011 18:41:54 +0000 Subject: [PATCH] adding a helper function for parallell processing git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2915 44740490-163a-0410-bde0-09ae8108e29a --- .../www/discojuice/discojuice.misc.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/modules/discojuice/www/discojuice/discojuice.misc.js b/modules/discojuice/www/discojuice/discojuice.misc.js index e6eba1125..fb5bc03c9 100644 --- a/modules/discojuice/www/discojuice/discojuice.misc.js +++ b/modules/discojuice/www/discojuice/discojuice.misc.js @@ -168,6 +168,65 @@ DiscoJuice.Utils = { "toRad": function (deg) { return deg * Math.PI/180; + }, + + + "waiter": function (completed, waitSeconds) { + + var + my = {}, + parallellActions = [], + executed = false; + + + function execute () { + + if (executed) { + console.log('Execution cancelled. Already performed.'); + return; + } + + executed = true; + completed(my); + } + + function runAction (act, tooLate) { + var + thisAction = {completed: false}; + + parallellActions.push(thisAction); + console.log('Running action ' + parallellActions.length); + act(function () { + var i; + thisAction.completed = true; + for (i = 0; i < parallellActions.length; i++) { + if (!parallellActions[i].completed) { + console.log('Cannot execute because we are waiting for another action to complete.'); + return; + } + } + if (executed) { + console.log('All actions completed. Too late for executing...'); + tooLate(); + return; + } + console.log('All actions completed. Executing!'); + execute(); + }); + + } + + function startTimer() { + setTimeout(function() { + console.log('Action timeout!'); + if (!executed) execute(); + }, waitSeconds); + } + + + my.startTimer = startTimer; + my.runAction = runAction; + return my; } -- GitLab