Debian Codegraph @

dojox/store/priority.js

raw symbols SCIP
1 define(['dojo/_base/lang', 'dojo/Deferred', 'dojo/when'], function(lang, Deferred, when){
2 // summary:
3 // This is a store wrapper that provides prioritized operations. One can include a "priority" property in the options
4 // property that is a number representing the priority, for get, put, add, remove, or query calls. The associated
5 // action will execute before any other actions in the queue that have a lower priority. The priority queue will also
6 // throttle the execution of actions to limit the number of concurrent actions to the priority number. If you submit
7 // an action with a priority of 3, it will not be executed until there are at most 2 other concurrent actions.
8
9 var queue = []; // the main queue
10 var running = 0; // how many actions are running
11 function processQueue(){
12 // here we pull actions of the queue and run them
13 // each priority level has it's own sub-queue, we go
14 // from highest to lowest, stopping if we hit the minimum
15 // priority for the currently running operations
16 for(var priority = queue.length - 1; priority >= running; priority--){
17 // get the sub-queue for this priority
18 var queueForPriorityLevel = queue[priority];
19 var action = queueForPriorityLevel && queueForPriorityLevel[queueForPriorityLevel.length - 1];
20 if(action){
21 queueForPriorityLevel.pop();
22 // track the number currently running
23 running++;
24 try{
25 action.executor(function(){
26 running--;
27 // once finished, process the next one in the queue
28 processQueue();
29 });
30 }catch(e){
31 action.def.reject(e);
32 processQueue();
33 }
34 }
35 }
36 }
37 function deferredResults(){
38 // because a query result set is not merely a promise that can be piped through, we have
39 // to recreate the full result set API to defer the execution of query, within each of the
40 // methods we wait for the promise for the query result to finish. We have to wrap this
41 // in an object wrapper so that the returned promise doesn't further defer access to the
42 // query result set
43 var deferred = new Deferred();
44 return {
45 promise: {
46 total: {
47 // allow for lazy access to the total
48 then: function(callback, errback){
49 return deferred.then(function(wrapper){
50 return wrapper.results.total;
51 }).then(callback, errback);
52 }
53 },
54 forEach: function(callback, thisObj){
55 // wait for the action to be executed
56 return deferred.then(function(wrapper){
57 // now run the forEach
58 return wrapper.results.forEach(callback, thisObj);
59 });
60 },
61 map: function(callback, thisObj){
62 return deferred.then(function(wrapper){
63 return wrapper.results.map(callback, thisObj);
64 });
65 },
66 filter: function(callback, thisObj){
67 return deferred.then(function(wrapper){
68 return wrapper.results.filter(callback, thisObj);
69 });
70 },
71 then: function(callback, errback){
72 return deferred.then(function(wrapper){
73 return when(wrapper.results, callback, errback);
74 });
75 }
76 },
77 resolve: deferred.resolve,
78 reject: deferred.reject
79 };
80 }
81 return function(store, config){
82 config = config || {};
83 var priorityStore = lang.delegate(store);
84 // setup the handling for each of these methods
85 ['add', 'put', 'query', 'remove', 'get'].forEach(function(method){
86 var original = store[method];
87 if(original){
88 priorityStore[method] = function(first, options){
89 options = options || {};
90 var def, executor;
91 if(options.immediate){
92 // if immediate is set, skip the queue
93 return original.call(store, first, options);
94 }
95 // just in case a method calls another method with the same args,
96 // default to doing the second call immediately.
97 options.immediate = true;
98 if(method === 'query'){
99 // use the special query result deferral
100 executor = function(callback){
101 // execute and resolve the wrapper
102 var queryResults = original.call(store, first, options);
103 def.resolve({results: queryResults});
104 // wait until the query results are done before performing the next action
105 when(queryResults, callback, callback);
106 };
107 // the special query deferred
108 def = deferredResults();
109 }else{
110 executor = function(callback){
111 // execute the main action (for get, put, add, remove)
112 when(original.call(store, first, options), function(value){
113 def.resolve(value);
114 callback(); // done
115 },
116 function(error){
117 def.reject(error);
118 callback();
119 });
120
121 };
122 // can use a standard deferred
123 def = new Deferred();
124 }
125 // add to the queue
126 var priority = options.priority > -1 ?
127 options.priority :
128 config.priority > -1 ?
129 config.priority : 4;
130 (queue[priority] || (queue[priority] = [])).push(
131 {executor: executor, def: def});
132 // get the next one off the queue
133 processQueue();
134 return def.promise;
135 };
136 }
137 });
138 return priorityStore;
139 };
140 });