npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-sync

v0.3.0

Published

Angular services that prevent from parallel same requests

Downloads

10

Readme

angular-sync

Introduction

Simple angular.js module that will prevent your application with parallel and undesirable requests.

Current version is 0.1.3.

Installation

Using npm: npm install angular-sync

Using bower: bower install angular-sync

Why ?

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.

How ?

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.

Licence

MIT License (MIT)

Contributing

If you find a bug or think about enhancement, feel free to contribute and submit an issue or a pull request.