11/*jslint node: true */
22"use strict" ;
33
4+ var debug = require ( "debug" ) ( "backend:BusinessBehaviourQueue" ) ;
5+
46var getCancelFunc = function ( ) {
57
68 var self = this ;
79 var [
810 behaviour ,
911 cancelExecutingBehaviour ,
1012 behaviourQueue ,
11- executingBehaviourQueue
13+ executingBehaviours
1214 ] = arguments ;
1315 return function ( ignoreSetComplete , cancellingReason ) {
1416
@@ -20,21 +22,49 @@ var getCancelFunc = function () {
2022 queueBehaviour ,
2123 cancelExecutingBehaviour ,
2224 behaviourQueue ,
23- executingBehaviourQueue
25+ executingBehaviours
2426 ] ) ( ) ;
2527 }
2628 } ) ;
27- if ( executingBehaviourQueue . indexOf ( behaviour ) > - 1 ) {
28-
29- behaviour . state . serviceOperations = [ ] ;
30- behaviour . state . modelOperations = [ ] ;
31- behaviour . state . businessOperations = [ ] ;
29+ if ( executingBehaviours . indexOf ( behaviour ) > - 1 ) {
30+
31+ var state = behaviour . state ;
32+ var dump = JSON . stringify ( {
33+ operations : [
34+ ...state . serviceOperations ,
35+ ...state . modelOperations ,
36+ ...state . businessOperations
37+ ]
38+ } ) ;
39+ state . serviceOperations = [ ] ;
40+ state . modelOperations = [ ] ;
41+ state . businessOperations = [ ] ;
3242 if ( cancellingReason ) {
3343
34- behaviour . state . error = new Error ( cancellingReason ) ;
44+ state . error = new Error ( cancellingReason ) ;
3545 }
3646 var cancelling = typeof cancelExecutingBehaviour === "function" ;
3747 if ( cancelling ) cancelExecutingBehaviour ( behaviour ) ;
48+ if ( typeof state . next === "function" ) {
49+
50+ if ( ! behaviour . timeout ) behaviour . timeout = 60 ;
51+ setTimeout ( function ( ) {
52+
53+ if ( executingBehaviours . indexOf ( behaviour ) === - 1 ) {
54+
55+ return ;
56+ }
57+ var name = behaviour . name || "" ;
58+ debug ( "Behaviour " + name + " hangs: " + dump ) ;
59+ if ( typeof state . next === "function" ) {
60+
61+ state . next ( ) ;
62+ state . cancelled = true ;
63+ return ;
64+ }
65+ debug ( "Behaviour " + name + " failed to cancel!" ) ;
66+ } , behaviour . timeout * 1000 ) ;
67+ }
3868 } else if ( behaviourQueue . indexOf ( behaviour ) > - 1 ) {
3969
4070 self . dequeue ( ...[
@@ -98,7 +128,7 @@ var BusinessBehaviourQueue = function (setComplete, setError) {
98128
99129 var self = this ;
100130 var behaviourQueue = [ ] ;
101- var executingBehaviourQueue = [ ] ;
131+ var executingBehaviours = [ ] ;
102132 self . length = ( ) => behaviourQueue . length ;
103133 self . cancelAll = function ( cancelExecutingBehaviour ) {
104134
@@ -108,7 +138,7 @@ var BusinessBehaviourQueue = function (setComplete, setError) {
108138 behaviourQueue [ i ] ,
109139 cancelExecutingBehaviour ,
110140 behaviourQueue ,
111- executingBehaviourQueue
141+ executingBehaviours
112142 ] ) ( ) ;
113143 }
114144 } ;
@@ -122,8 +152,8 @@ var BusinessBehaviourQueue = function (setComplete, setError) {
122152 var index = behaviourQueue . indexOf ( currentBehaviour ) ;
123153 if ( index > - 1 && index !== behaviourQueue . length - 1 ) {
124154
125- index = executingBehaviourQueue . indexOf ( currentBehaviour ) ;
126- if ( index > - 1 ) executingBehaviourQueue . splice ( index , 1 ) ;
155+ index = executingBehaviours . indexOf ( currentBehaviour ) ;
156+ if ( index > - 1 ) executingBehaviours . splice ( index , 1 ) ;
127157 return true ;
128158 }
129159 return false ;
@@ -156,7 +186,7 @@ var BusinessBehaviourQueue = function (setComplete, setError) {
156186 behaviour ,
157187 cancelExecutingBehaviour ,
158188 behaviourQueue ,
159- executingBehaviourQueue
189+ executingBehaviours
160190 ] ) ;
161191 var index = behaviourQueue . indexOf ( behaviour ) ;
162192 if ( behaviour . timeout > 0 ) {
@@ -207,7 +237,7 @@ var BusinessBehaviourQueue = function (setComplete, setError) {
207237 var shouldDequeue = behaviourQueue . indexOf ( ...[
208238 executingBehaviour
209239 ] ) > - 1 ;
210- shouldDequeue &= executingBehaviourQueue . indexOf ( ...[
240+ shouldDequeue &= executingBehaviours . indexOf ( ...[
211241 executingBehaviour
212242 ] ) === - 1 ;
213243 if ( shouldDequeue ) self . dequeue ( ...[
@@ -239,28 +269,28 @@ var BusinessBehaviourQueue = function (setComplete, setError) {
239269 var currentBehaviour = null ;
240270 for ( var i = behaviourQueue . length - 1 ; i >= 0 ; i -- ) {
241271
242- if ( executingBehaviourQueue . indexOf ( ...[
272+ if ( executingBehaviours . indexOf ( ...[
243273 behaviourQueue [ i ]
244274 ] ) === - 1 ) {
245275
246276 currentBehaviour = behaviourQueue [ i ] ;
247- executingBehaviourQueue . push ( currentBehaviour ) ;
277+ executingBehaviours . push ( currentBehaviour ) ;
248278 break ;
249279 }
250280 }
251281 return currentBehaviour ;
252282 } ;
253283 self . finish = function ( currentBehaviour , next ) {
254284
255- if ( executingBehaviourQueue . every ( function ( ) {
285+ if ( executingBehaviours . every ( function ( ) {
256286
257287 var [ executingBehaviour ] = arguments ;
258288 return ! executingBehaviour . hasMandatoryBehaviour ( ...[
259289 currentBehaviour
260290 ] ) ;
261291 } ) ) next ( ) ;
262- var index = executingBehaviourQueue . indexOf ( currentBehaviour ) ;
263- if ( index > - 1 ) executingBehaviourQueue . splice ( index , 1 ) ;
292+ var index = executingBehaviours . indexOf ( currentBehaviour ) ;
293+ if ( index > - 1 ) executingBehaviours . splice ( index , 1 ) ;
264294 } ;
265295} ;
266296
0 commit comments