Skip to content

Commit 953d6ce

Browse files
committed
v1.4.1
1 parent 85c078a commit 953d6ce

6 files changed

Lines changed: 114 additions & 42 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "behaviours-js",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Behaviours framework written in JavaScript implementing Behaviour-first design and queue-map architecture.",
55
"main": "index.js",
66
"scripts": {

src/business/BusinessBehaviourCore.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,12 @@ var getNext = function () {
286286
delegateExisted,
287287
internalDelegate
288288
] = arguments;
289-
return function () {
289+
var state = this.state;
290+
return state.next = function () {
290291

292+
state.next = undefined;
293+
delete state.next;
294+
if (state.cancelled) return;
291295
if (delegateExisted && checkIf(...[
292296
operationKey,
293297
beginConditions
@@ -327,7 +331,7 @@ var BusinessBehaviourCore = function (options) {
327331
internalDelegate
328332
] = arguments;
329333
let delegateExisted = !!delegates[serviceOperation];
330-
let next = getNext(...[
334+
let next = getNext.apply(this, [
331335
operationDelegateExecutive,
332336
serviceOperation,
333337
beginConditions,
@@ -355,7 +359,7 @@ var BusinessBehaviourCore = function (options) {
355359
internalDelegate
356360
] = arguments;
357361
let delegateExisted = !!delegates[modelOperation];
358-
let next = getNext(...[
362+
let next = getNext.apply(this, [
359363
operationDelegateExecutive,
360364
modelOperation,
361365
beginConditions,
@@ -383,7 +387,7 @@ var BusinessBehaviourCore = function (options) {
383387
internalDelegate
384388
] = arguments;
385389
let delegateExisted = !!delegates[businessOperation];
386-
let next = getNext(...[
390+
let next = getNext.apply(this, [
387391
operationDelegateExecutive,
388392
businessOperation,
389393
beginConditions,

src/business/BusinessBehaviourQueue.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/*jslint node: true */
22
"use strict";
33

4+
var debug = require("debug")("backend:BusinessBehaviourQueue");
5+
46
var 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

src/business/BusinessController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ var BusinessController = function (options) {
106106

107107
throw new Error("Invalid behaviour");
108108
}
109-
var ignoring = ignoreBehaviours
109+
var ignoring = ignoreBehaviours;
110110
ignoring |= businessBehaviourQueue.isEnqueued(behaviour);
111111
if (ignoring) return () => { };
112112
behaviour.getProperty = getProperty || ((property) => property);

src/business/BusinessDelegator.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ var getFetchCallback = function () {
1313
operationCallback,
1414
callback
1515
] = arguments;
16-
return function (resource, error) {
16+
let state = currentBehaviour.state;
17+
return state.next = function (resource, error) {
1718

18-
if (resource) currentBehaviour.state.serviceObjects = [
19+
state.next = undefined;
20+
delete state.next;
21+
if (state.cancelled) return;
22+
if (resource) state.serviceObjects = [
1923
resource.data ||
2024
resource.id ||
2125
resource.path
2226
];
23-
if (error) currentBehaviour.state.error = error;
27+
if (error) state.error = error;
2428
let callingBack = typeof operationCallback === "function";
2529
if (callingBack) operationCallback({
2630

@@ -60,9 +64,12 @@ var getRequestCallback = function () {
6064
operationCallback,
6165
callback
6266
] = arguments;
63-
return function (serviceObjects, error) {
67+
let state = currentBehaviour.state;
68+
return state.next = function (serviceObjects, error) {
6469

65-
let state = currentBehaviour.state;
70+
state.next = undefined;
71+
delete state.next;
72+
if (state.cancelled) return;
6673
if (serviceObjects) {
6774

6875
if (!state.serviceObjects) state.serviceObjects = [];
@@ -93,9 +100,12 @@ var getManipulateCallback = function () {
93100
operationCallback,
94101
callback
95102
] = arguments;
96-
return function (modelObjects, error) {
103+
let state = currentBehaviour.state;
104+
return state.next = function (modelObjects, error) {
97105

98-
let state = currentBehaviour.state;
106+
state.next = undefined;
107+
delete state.next;
108+
if (state.cancelled) return;
99109
if (modelObjects) {
100110

101111
if (!state.modelObjects) state.modelObjects = [];

src/business/OperationDelegateExecutive.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,16 @@ var OperationDelegateExecutive = function (options) {
171171

172172
that.data.append = append;
173173
}
174-
var serviceContinue = getServiceContinue.apply(that, [internalDelegate]);
175-
watch(serviceOperation, that.data, 0, serviceContinue, watchers);
174+
var serviceContinue = getServiceContinue.apply(that, [
175+
internalDelegate
176+
]);
177+
watch(...[
178+
serviceOperation,
179+
that.data,
180+
0,
181+
serviceContinue,
182+
watchers
183+
]);
176184
};
177185
self.executeModelOperation = function () {
178186

@@ -185,18 +193,24 @@ var OperationDelegateExecutive = function (options) {
185193
callback,
186194
append
187195
] = arguments;
196+
var getObjectQuery = () => queryOrObjects || that.data.query;
188197
var lazyQuery = !Array.isArray(queryOrObjects);
189198
lazyQuery &= typeof that.data.query === "function";
199+
if (lazyQuery) getObjectQuery = that.data.query;
200+
var getObjectAggregate = () => that.data.aggregate;
190201
var lazyAggregate = typeof that.data.aggregate === "function";
202+
if (lazyAggregate) getObjectAggregate = that.data.aggregate;
203+
var getObjectFilter = () => that.data.filter;
191204
var lazyFilter = typeof that.data.filter === "function";
205+
if (lazyFilter) getObjectFilter = that.data.filter;
192206
if (that.data.objects) {
193207

194208
that.data.wrapper = that.data.objects;
195209
} else that.data.wrapper = {
196210

197-
getObjectQuery: lazyQuery ? that.data.query : () => queryOrObjects || that.data.query,
198-
getObjectAggregate: lazyAggregate ? that.data.aggregate : () => that.data.aggregate,
199-
getObjectFilter: lazyFilter ? that.data.filter : () => that.data.filter
211+
getObjectQuery,
212+
getObjectAggregate,
213+
getObjectFilter
200214
};
201215
if (entity) {
202216

@@ -213,8 +227,16 @@ var OperationDelegateExecutive = function (options) {
213227

214228
that.data.append = append;
215229
}
216-
var modelContinue = getModelContinue.apply(that, [internalDelegate]);
217-
watch(modelOperation, that.data, 0, modelContinue, watchers);
230+
var modelContinue = getModelContinue.apply(that, [
231+
internalDelegate
232+
]);
233+
watch(...[
234+
modelOperation,
235+
that.data,
236+
0,
237+
modelContinue,
238+
watchers
239+
]);
218240
};
219241
self.executeServiceMappingOperation = function () {
220242

@@ -228,7 +250,9 @@ var OperationDelegateExecutive = function (options) {
228250

229251
that.data.callback = callback;
230252
}
231-
var serviceMappingContinue = getServiceMappingContinue.apply(that, [internalDelegate]);
253+
var serviceMappingContinue = getServiceMappingContinue.apply(that, [
254+
internalDelegate
255+
]);
232256
watch(...[
233257
businessOperation,
234258
that.data,
@@ -254,7 +278,9 @@ var OperationDelegateExecutive = function (options) {
254278

255279
that.data.callback = callback;
256280
}
257-
var modelMappingContinue = getModelMappingContinue.apply(that, [internalDelegate]);
281+
var modelMappingContinue = getModelMappingContinue.apply(that, [
282+
internalDelegate
283+
]);
258284
watch(...[
259285
businessOperation,
260286
that.data,
@@ -275,7 +301,9 @@ var OperationDelegateExecutive = function (options) {
275301

276302
that.data.error = error;
277303
}
278-
var errorHandlingContinue = getErrorHandlingContinue.apply(that, [internalDelegate]);
304+
var errorHandlingContinue = getErrorHandlingContinue.apply(that, [
305+
internalDelegate
306+
]);
279307
watch(...[
280308
businessOperation,
281309
that.data,

0 commit comments

Comments
 (0)