Skip to content

Commit

Permalink
basic setup for sync hooks #4
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed May 18, 2015
1 parent 6971088 commit 55aa081
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ Kareem.prototype.execPre = function(name, context, callback) {
next();
};

Kareem.prototype.execPreSync = function(name, context) {
var pres = this._pres[name] || [];
var numPres = pres.length;

for (var i = 0; i < numPres; ++i) {
pres[i].fn.call(context);
}
};

Kareem.prototype.execPost = function(name, context, args, callback) {
var posts = this._posts[name] || [];
var numPosts = posts.length;
Expand Down Expand Up @@ -139,6 +148,15 @@ Kareem.prototype.execPost = function(name, context, args, callback) {
next();
};

Kareem.prototype.execPostSync = function(name, context) {
var posts = this._posts[name] || [];
var numPosts = posts.length;

for (var i = 0; i < numPosts; ++i) {
posts[i].call(context);
}
};

Kareem.prototype.wrap = function(name, fn, context, args, useLegacyPost) {
var lastArg = (args.length > 0 ? args[args.length - 1] : null);
var _this = this;
Expand Down
24 changes: 24 additions & 0 deletions test/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,28 @@ describe('execPost', function() {
done();
});
});
});

describe('execPostSync', function() {
var hooks;

beforeEach(function() {
hooks = new Kareem();
});

it('executes hooks synchronously', function() {
var execed = {};

hooks.post('cook', function() {
execed.first = true;
});

hooks.post('cook', function() {
execed.second = true;
});

hooks.execPostSync('cook', null);
assert.ok(execed.first);
assert.ok(execed.second);
});
});
24 changes: 24 additions & 0 deletions test/pre.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,27 @@ describe('execPre', function() {
});
});
});

describe('execPreSync', function() {
var hooks;

beforeEach(function() {
hooks = new Kareem();
});

it('executes hooks synchronously', function() {
var execed = {};

hooks.pre('cook', function() {
execed.first = true;
});

hooks.pre('cook', function() {
execed.second = true;
});

hooks.execPreSync('cook', null);
assert.ok(execed.first);
assert.ok(execed.second);
});
});

0 comments on commit 55aa081

Please sign in to comment.