operations-angular
v0.1.0
Published
Management of complex chains of asynchronous operations in Javascript for angular.
Downloads
3
Readme
operations.angular.js
operations.js is a library for creating, composing, cancelling and queueing asynchronous operations in Javascript and expressing complex dependencies between them.
This is a wrapper around operations.js for angular, ensuring that digests occur after callbacks.
Installation
Bower
bower install operations-angular --save-dev
Npm
npm install operations-angular --save-dev
Then include src/operations.angular.js
in your project.
Example
See the original repo for documentation but here's a dumb example that shows how to get started with angular and operations:
angular.module('myModule', ['operations'])
.factory('myQueue', function (OperationQueue) {
// Max 2 concurrent operations.
return new OperationQueue('My Awesome Queue', 2);
})
.factory('MyOperationsService', function (Operation) {
return {
uselessOperation: function () {
return new Operation('My Useless Operation', function (done) {
var finished = false;
var self = this;
var interval = setInterval(function () {
if (self.cancelled || finished) {
clearInterval(interval);
if (finished) done();
}
}, 10);
setTimeout(function () {
finished = true;
}, 100);
});
},
logOperation: function () {
return new Operation('My Logger Operation', function (done) {
console.log('Finished!');
done();
});
}
}
})
;
And then we can use our services as follows:
var logOp = MyOperationsService.logOperation();
for (var i = 0; i < 10; i++) {
var uselessOp = MyOperationsService.uselessOperation();
myQueue.addOperation(uselessOp);
logOp.addDependency(uselessOp);
}
logOp.onCompletion(done);
myQueue.addOperation(logOp);
myQueue.start();