Skip to content

Commit

Permalink
update intercal.require.js
Browse files Browse the repository at this point in the history
  • Loading branch information
marianoguerra committed Mar 27, 2012
1 parent 22394d9 commit 46fbb4a
Showing 1 changed file with 45 additions and 30 deletions.
75 changes: 45 additions & 30 deletions intercal.require.js
Expand Up @@ -396,6 +396,8 @@ define(["path/to/jquery"], function ($) {
locked = false,
// items to wait for
items = [],
// items that will be removed once they are completed
volatileItems = [],
// total number of items after it's locked
total = 0,
// number of completed items
Expand Down Expand Up @@ -429,21 +431,52 @@ define(["path/to/jquery"], function ($) {
barrier.reject('timeout');
}

function checkCompletion() {
if (timedOut) {
return;
}

// if all completed
if (waitCompletedCount === (total - volatileItems.length)) {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}

endTime = intercal.now();

if (failedArgs.length === 0) {
barrier.resolve();
} else {
barrier.reject(failedArgs);
}
}
}

function lock() {

if (items.length === 0) {
throw new intercal.Error("trying to lock a barrier with no actions");
} else if (itemCount > 0 && items.length !== itemCount) {
if (locked) {
return;
}

if (itemCount > 0 && items.length !== itemCount) {
throw new intercal.Error("trying to lock a barrier before reaching itemCount");
}

locked = true;
total = items.length;
startTime = intercal.now();

if (waitCompletedCount === 0) {
waitCompletedCount = total;
}

if (timeout > 0) {
timeoutId = setTimeout(fireTimeout, timeout);
}

checkCompletion();

return obj;
}

function arrayRemove(arr, element) {
Expand All @@ -458,42 +491,22 @@ define(["path/to/jquery"], function ($) {
return arr;
}

function checkCompletion() {
if (timedOut) {
return;
}

// if all completed
if (waitCompletedCount === (total - items.length)) {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}

endTime = intercal.now();

if (failedArgs.length === 0) {
barrier.resolve();
} else {
barrier.reject(failedArgs);
}
}
}

function listenForCompletion(action) {

function actionFired() {
arrayRemove(items, action);
arrayRemove(volatileItems, action);
// unsubscribe if it's a callback
if (action.add && action.has) {
action.remove(actionFired);
}

checkCompletion();
if (locked) {
checkCompletion();
}
}

if (action.add && action.has) {
// if is a callbacks object

action.add(actionFired);
} else {
// it must be a deferred/promise
Expand All @@ -516,6 +529,7 @@ define(["path/to/jquery"], function ($) {
for (i = 0; i < item.length; i += 1) {
action = item[i];
items.push(action);
volatileItems.push(action);

listenForCompletion(action);

Expand All @@ -524,8 +538,9 @@ define(["path/to/jquery"], function ($) {
break;
}
}

}

return obj;
}

// if the first parameter is an array, add the items and lock
Expand Down Expand Up @@ -559,7 +574,7 @@ define(["path/to/jquery"], function ($) {
timeout: timeoutList.add,

status: function () {
var completed = total - items.length,
var completed = total - volatileItems.length,
totalTime = -1,
remainingTime = -1;

Expand All @@ -585,7 +600,7 @@ define(["path/to/jquery"], function ($) {
// time remaining before timeout or -1 if already finished or timed out
remainingTime: (endTime < 1 && timeout > 0) ? intercal.now() - (startTime + timeout) : -1,
// true if the barrier finished in some way
finished: locked && itemCount > 0 && items.length === 0,
finished: locked && itemCount > 0 && volatileItems.length === 0,
// true if the barrier timedOut
timedOut: timedOut
};
Expand Down

0 comments on commit 46fbb4a

Please sign in to comment.