Skip to content

Commit

Permalink
use async for allowed handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
iofjuupasli committed Nov 13, 2014
1 parent 3b420ac commit 3ed663b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 194 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bower i -S guard
npm i -S guard-bit
```
### File
Save [guard.min.js](https://raw.githubusercontent.com/iofjuupasli/guard/v1.0.3/guard.min.js) or [guard.js](https://raw.githubusercontent.com/iofjuupasli/guard/v1.0.3/guard.js) to your assets folder
Save [guard.min.js](https://raw.githubusercontent.com/iofjuupasli/guard/v1.0.4/guard.min.js) or [guard.js](https://raw.githubusercontent.com/iofjuupasli/guard/v1.0.4/guard.js) to your assets folder

Examples
=============
Expand Down Expand Up @@ -151,7 +151,7 @@ guard(function (message) {
// level === 1
guard('@:profile:read', function () {
console.log('My Profile');
})(); // 'My Profile' immediately
})(); // 'My Profile' immediately (but async)

guard('adminFeature', function () {
console.log('called!');
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "guard",
"description": "Utility to split opportunities by the account level (free\\pro\\enterprise\\etc.) for frontend and node",
"main": "guard.js",
"version": "1.0.3",
"version": "1.0.4",
"homepage": "https://github.com/iofjuupasli/guard",
"authors": [
"iofjuupasli <[email protected]>"
Expand Down
38 changes: 18 additions & 20 deletions guard.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,38 @@

function reqThenCallback(feature, callback) {
return function () {
if (!config[level].request) {
var args = arguments;
if (checkAccess(feature)) {
setTimeout(function () {
callback.apply(null, args);
}, 0);
return;
}
var args = arguments;
requestQueue.push(callback);
if (isRequesting) {
return;
} else {
isRequesting = true;
}
config[level].request(function (err) {
isRequesting = true;
if (config[level].request) {
config[level].request(requestHandler);
}

function requestHandler(err) {
isRequesting = false;
if (err) {
requestQueue = [];
return;
}
guard.setLevel(level + 1);
while (requestQueue.length) {
if (feature) {
guard(feature, requestQueue.shift())
.apply(null, args);
var nextRequest = requestQueue.shift();
if (feature != null) {
guard(feature, nextRequest).apply(null, args);
} else {
guard(requestQueue.shift()).apply(null, args);
guard(nextRequest).apply(null, args);
}
}
});
}
};
}

Expand All @@ -87,23 +93,15 @@
}
if (arguments.length === 1) {
if (_.isFunction(arguments[0])) {
if (checkAccess()) {
return arguments[0];
} else {
return reqThenCallback(null, arguments[0]);
}
return reqThenCallback(null, arguments[0]);
} else {
return checkAccess(arguments[0]);
}
}
if (!_.isFunction(arguments[1])) {
throw new TypeError();
}
if (checkAccess(arguments[0])) {
return arguments[1];
} else {
return reqThenCallback(arguments[0], arguments[1]);
}
return reqThenCallback(arguments[0], arguments[1]);
};

guard.request = function (callback) {
Expand Down
2 changes: 1 addition & 1 deletion guard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "guard-bit",
"version": "1.0.3",
"version": "1.0.4",
"description": "Utility to split opportunities by the account level (free\\pro\\enterprise\\etc.) for frontend and node",
"main": "guard.js",
"keywords": [
Expand Down
Loading

0 comments on commit 3ed663b

Please sign in to comment.