Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor when.guard to be more flexible. (experimental) #439

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
build/when.js
test/browser/*.js
bower_components/
*.swp
42 changes: 23 additions & 19 deletions guard.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ define(function(require) {
return function() {
var args = slice.call(arguments);

return when(condition()).withThis(this).then(function(exit) {
return when(f.apply(this, args))['finally'](exit);
});
return when(condition.enter()).withThis(this).then(function () {
return when(f.apply(this, args));
}).tap(condition.resolved, condition.rejected).tap(condition.exit);
};
}

Expand All @@ -48,24 +48,28 @@ define(function(require) {
function n(allowed) {
var count = 0;
var waiting = [];

return function enter() {
return when.promise(function(resolve) {
if(count < allowed) {
resolve(exit);
} else {
waiting.push(resolve);
return {
enter: function () {
return when.promise(function(resolve) {
if(count < allowed) {
resolve();
} else {
waiting.push(resolve);
}
count += 1;
});
},
resolved: function () {
},
rejected: function () {
},
exit: function () {
count = Math.max(count - 1, 0);
if(waiting.length > 0) {
waiting.shift()();
}
count += 1;
});
};

function exit() {
count = Math.max(count - 1, 0);
if(waiting.length > 0) {
waiting.shift()(exit);
}
}
};
}

});
Expand Down
82 changes: 37 additions & 45 deletions test/guard-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@ buster.testCase('when/guard', {
assert.isFunction(guard());
},

'should invoke condition': function() {
'should invoke condition': function(done) {
var condition, guarded;

condition = this.spy();
condition = {
enter: this.spy(),
exit: this.spy(),
};
guarded = guard(condition, noop);

guarded();

assert.called(condition);
when(guarded()).then(function () {
assert.called(condition.enter);
assert.called(condition.exit);
done();
});
},

'should invoke guarded function after condition promise fulfills': function(done) {
var condition, f, guarded;

condition = function() { return noop; };
condition = {
enter: function() { return noop; }
};
f = this.spy();
guarded = guard(condition, f);

Expand All @@ -44,15 +51,16 @@ buster.testCase('when/guard', {
},

'should notify condition once guarded function settles': function(done) {
var condition, notify, guarded;
var condition, guarded;

notify = this.spy();
condition = function() { return notify; };
condition = {
enter: this.spy()
};
guarded = guard(condition, noop);

guarded().then(
function() {
assert.calledOnce(notify);
assert.calledOnce(condition.enter);
},
fail
).ensure(done);
Expand All @@ -62,7 +70,9 @@ buster.testCase('when/guard', {
var condition, f, guarded;

f = this.spy();
condition = function() { return noop; };
condition = {
enter: function() { return noop; }
};
guarded = guard(condition, f);

guarded(other).then(
Expand All @@ -78,47 +88,33 @@ buster.testCase('when/guard', {
},

'n': {
'should create a function': function() {
assert.isFunction(guard.n(1));
'should create an object': function() {
assert.isObject(guard.n(1));
},

'should return a promise': function() {
var c = guard.n(1);
assert.isFunction(c().then);
},

'returned promise should resolve to a function': function(done) {
var enter = guard.n(1);
enter().then(
function(exit) {
assert.isFunction(exit);
},
fail
).ensure(done);
assert.isFunction(c.enter().then);
},

'should allow one execution': function(done) {
var enter, value, first, second;
var c, value, first, second;

enter = guard.n(1);
c = guard.n(1);
value = sentinel;

first = enter();
second = enter();
first = c.enter();
second = c.enter();

first.then(
function(exit) {
return when().delay(100).then(function() {
function() {
return when().delay(5).then(function() {
assert.same(value, sentinel);
exit();
done();
});
},
fail
);

second.then(
function() { value = other; }
).ensure(done);
},

'should allow two executions': function(done) {
Expand All @@ -127,9 +123,9 @@ buster.testCase('when/guard', {
one = guard.n(2);
value = sentinel;

first = one();
second = one();
third = one();
first = one.enter();
second = one.enter();
third = one.enter();

first.then(
function() {
Expand All @@ -139,18 +135,14 @@ buster.testCase('when/guard', {
);

second.then(
function(exit) {
return when().delay(100).then(function() {
function() {
return when().delay(5).then(function() {
assert.same(value, sentinel);
exit();
done();
});
},
fail
);

third.then(
function() { value = other; }
).ensure(done);
}
}

Expand Down