-1

In my AngularJS app I have a Service and multiple controllers running at a time. App receives data updates from server (in form of JSON) in the Angular Service. I need to queue this data in the server (as data is vast in amount) and then after some processing service needs to send this data to all the controllers using the controller callbacks registered with service. Controllers further process this data.

I have below queue of JSON Objects:

In ----> [Obj1] - [Obj2] - [Obj3] - [Obj4] ---> OUT

The Input operation is asynchronous and there are 'n' number of objects getting added in the queue every second (queue can also become empty).

I need to pop one item at a time and then process the received JSON data, this process will go on till last item in the queue. also as new items are added in the queue they will be processed.

From some of the posts I got to know that this can be done using AngularJS Promise, but as I am completely new to Angular I am not able to understand how it can be implemented.

Also in some examples it is mentioned to use var deferred = $q.defer();, so how to use $q and what is the difference in $q and Promise?

Also is there other "simple" way of implementing this functionality?

Example code:

angular.module('myApp')
.service('DataService', ['$rootScope', 'UpdateService', function($rootScope, UpdateService)
{
    // To store array of JSON updates    
    var arrayOfUpdates = [];

    // To store callbacks to which we need to send updates after processing 
    var arrayofCallbacks = [];

    // Method is getting called when there are updates from "UpdateService" server 
    this.onDataUpdated = function (jsonObj)
    {
        // This will add this object in the array
        arrayOfUpdates.unshift(jsonObj);
    }

    // This method will process data and send it further 
    function processData()
    {
         // Get the last element from the array 
         var jsonObj = arrayOfUpdates.pop();

         // Now process this JSON data - this method takes time 400-500 MS
         var data = doSomeCalculations(jsonObj); 

         // There can be 10-20 callbacks at a time -- This also takes time
         for(var index=0;index<arrayofCallbacks.length;index++)
         {
               var object = arrayofCallbacks[index]; 
               object.onUpdatedData(data);
         }       

         // After processing this item 
         // Pop next element and process and send to registered callbacks 
         // I can not call "processData()" again as it can create loop 
         // Also calling it after interval (using $interval) might call it when it has not completed processing 
    }
});
User7723337
  • 11,857
  • 27
  • 101
  • 182

1 Answers1

1

You could look into angular events. Working with $scope.$emit and $scope.$on.

Subscribe in every controller and after processing of the data publish an event that all controllers then will handle.

Community
  • 1
  • 1
Bob Brinks
  • 1,372
  • 1
  • 10
  • 19
  • Yes I can use $emit and $on to send and receive events from service to controller. But is there better way of implementing? also how efficient emit is will there be chance of missing/skipping and event as there are lot of events every second service will need to send. – User7723337 Sep 28 '16 at 10:45
  • There's no realistic change of missing or skipping. And there's no way to say what is a better way because i don't know your requirements. Do you mean faster, less complex/lines of codes, more maintainable/readable. Also you probably won't get an answer as to what is better as that's primarily opinion based which is not allowed on stackoverflow. – Bob Brinks Sep 28 '16 at 12:20
  • In my oppinion using events is better because you don't have to keep a list of callback yourself. That will be handled by angular. – Bob Brinks Sep 28 '16 at 12:22
  • Currently I don't see any way other than implementing it using emit. But my question is if we send 2-3 broadcasts/emit in 1 seconds is it acceptable implementation? Or it is bad to use emit if app is sending more number (2-3) of events per second. what is your opinion? – User7723337 Sep 28 '16 at 12:30
  • Should be fine. – Bob Brinks Sep 28 '16 at 12:43