Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
angular-sync
Advanced tools
Simple angular.js module that will prevent your application with parallel and undesirable requests.
Current version is 0.1.3.
Using npm: npm install angular-sync
Using bower: bower install angular-sync
Suppose you have a simple controller:
angular.module('myApp')
.controller('MyController', ['$scope', '$http', function($scope, $http) {
$scope.submit = function() {
$http.post('/foo', $scope.myForm)
.success(function() {
console.log('it works !');
});
};
}]);
With a simple form:
<div ng-controller="MyController">
<form name="myForm">
<button ng-click="submit()"></button>
</form>
</div>
If you don't do anything, user will be able to submit form each time they click on your button. This is probably not what you want, right ?
A very simple solution would be to define a flag in your controller and use ng-disabled
directive to prevent multiple clicks. This is a lot of boilerplate code, and not really DRY. Using angular-sync, parallel submission are automatically cancelled before being triggered.
Just import module to your application:
angular.module('myApp', [
'AngularSync'
]);
By default:
POST, PUT, PATCH and DELETE requests are cancelled before being triggered if a pending request with the same url already exist. This will prevent parallel submission for these type of requests. A request is seen as "pending" until response has not been received but you can define a timeout to allow parallel with a pre-defined delay.
GET requests are triggered but previous request is automatically aborted.
As stated before, by default, a request is pending until response is received (success or error), but you can define a timeout value and request will be automatically seen as "received" when timeout is reached.
You can change default settings in the config
function of your module:
angular.module('myApp', ['AngularSync'])
.config(['AngularSyncProvider', 'AngularSyncMode', function(AngularSyncProvider, SyncMode) {
AngularSyncProvider.timeout(3000) // Override timeout (in millis).
.mode('GET', SyncMode.PREVENT) // Prevent parallel requests with same URL.
.mode('POST', SyncMode.ABORT) // Abort previous request by default.
.mode('DELETE', SyncMode.FORCE); // Allow parallel submissions.
}]);
You can also override default mode for a specific request:
angular.module('myApp')
.controller('MyController', ['$scope', '$http', 'AngularSyncMode', function($scope, $http, SyncMode) {
$scope.submit = function() {
var config = {
ngSync: {
mode: SyncMode.FORCE
}
};
$http.post('/foo', $scope.myForm, config)
.success(function() {
console.log('it works !');
});
};
}]);
Important:
Note that error callback will not be executed if request is not being triggered because another request is still pending. This is the same for aborted request (suppose that a request is aborted because a new incoming request is triggered, error callback will not be executed).
If you think this is a mistake, do not hesitate to submit an issue or a pull request.
MIT License (MIT)
If you find a bug or think about enhancement, feel free to contribute and submit an issue or a pull request.
FAQs
Angular services that prevent from parallel same requests
The npm package angular-sync receives a total of 4 weekly downloads. As such, angular-sync popularity was classified as not popular.
We found that angular-sync demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.