bpmn-elements 13.2.0 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,6 +14,7 @@ var _Errors = require("../error/Errors.js");
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
15
  const kActivityDef = Symbol.for('activityDefinition');
16
16
  const kConsuming = Symbol.for('consuming');
17
+ const kConsumingRunQ = Symbol.for('run queue consumer');
17
18
  const kCounters = Symbol.for('counters');
18
19
  const kEventDefinitions = Symbol.for('eventDefinitions');
19
20
  const kExec = Symbol.for('exec');
@@ -113,6 +114,8 @@ function Activity(Behaviour, activityDef, context) {
113
114
  };
114
115
  this[kEventDefinitions] = eventDefinitions && eventDefinitions.map(ed => new ed.Behaviour(this, ed, this.context));
115
116
  this[kExtensions] = context.loadExtensions(this);
117
+ this[kConsuming] = false;
118
+ this[kConsumingRunQ] = undefined;
116
119
  }
117
120
  Object.defineProperties(Activity.prototype, {
118
121
  counters: {
@@ -239,6 +242,7 @@ Object.defineProperties(Activity.prototype, {
239
242
  }
240
243
  });
241
244
  Activity.prototype.activate = function activate() {
245
+ if (this[kActivated]) return;
242
246
  this[kActivated] = true;
243
247
  this.addInboundListeners();
244
248
  return this._consumeInbound();
@@ -274,6 +278,7 @@ Activity.prototype.run = function run(runContent) {
274
278
  const broker = this.broker;
275
279
  broker.publish('run', 'run.enter', content);
276
280
  broker.publish('run', 'run.start', (0, _messageHelper.cloneContent)(content));
281
+ this[kConsuming] = true;
277
282
  this._consumeRunQ();
278
283
  };
279
284
  Activity.prototype.getState = function getState() {
@@ -322,6 +327,7 @@ Activity.prototype.resume = function resume() {
322
327
  this.broker.publish('run', 'run.resume', content, {
323
328
  persistent: false
324
329
  });
330
+ this[kConsuming] = true;
325
331
  this._consumeRunQ();
326
332
  };
327
333
  Activity.prototype.discard = function discard(discardContent) {
@@ -332,6 +338,7 @@ Activity.prototype.discard = function discard(discardContent) {
332
338
  const broker = this.broker;
333
339
  broker.getQueue('run-q').purge();
334
340
  broker.publish('run', 'run.discard', (0, _messageHelper.cloneContent)(this[kStateMessage].content));
341
+ this[kConsuming] = true;
335
342
  this._consumeRunQ();
336
343
  };
337
344
  Activity.prototype.addInboundListeners = function addInboundListeners() {
@@ -396,6 +403,7 @@ Activity.prototype._runDiscard = function runDiscard(discardContent) {
396
403
  executionId
397
404
  });
398
405
  this.broker.publish('run', 'run.discard', content);
406
+ this[kConsuming] = true;
399
407
  this._consumeRunQ();
400
408
  };
401
409
  Activity.prototype._discardRun = function discardRun() {
@@ -404,17 +412,19 @@ Activity.prototype._discardRun = function discardRun() {
404
412
  const execution = this[kExec].execution;
405
413
  if (execution && !execution.completed) return;
406
414
  switch (status) {
415
+ case 'end':
407
416
  case 'executing':
408
417
  case 'error':
409
418
  case 'discarded':
410
419
  return;
411
420
  }
412
421
  this._deactivateRunConsumers();
413
- const message = this[kStateMessage];
414
- if (this.extensions) this.extensions.deactivate((0, _messageHelper.cloneMessage)(message));
422
+ const stateMessage = this[kStateMessage];
423
+ if (this.extensions) this.extensions.deactivate((0, _messageHelper.cloneMessage)(stateMessage));
415
424
  const broker = this.broker;
416
425
  broker.getQueue('run-q').purge();
417
- broker.publish('run', 'run.discard', (0, _messageHelper.cloneContent)(message.content));
426
+ broker.publish('run', 'run.discard', (0, _messageHelper.cloneContent)(stateMessage.content));
427
+ this[kConsuming] = true;
418
428
  this._consumeRunQ();
419
429
  };
420
430
  Activity.prototype._shakeOutbound = function shakeOutbound(sourceMessage) {
@@ -441,13 +451,14 @@ Activity.prototype._consumeInbound = function consumeInbound() {
441
451
  if (!this[kActivated]) return;
442
452
  if (this.status) return;
443
453
  const inboundQ = this.broker.getQueue('inbound-q');
454
+ const onInbound = this[kMessageHandlers].onInbound;
444
455
  if (this[kFlags].isParallelJoin) {
445
- return inboundQ.consume(this[kMessageHandlers].onInbound, {
456
+ return inboundQ.consume(onInbound, {
446
457
  consumerTag: '_run-on-inbound',
447
458
  prefetch: 1000
448
459
  });
449
460
  }
450
- return inboundQ.consume(this[kMessageHandlers].onInbound, {
461
+ return inboundQ.consume(onInbound, {
451
462
  consumerTag: '_run-on-inbound'
452
463
  });
453
464
  };
@@ -548,13 +559,17 @@ Activity.prototype._onInboundEvent = function onInboundEvent(routingKey, message
548
559
  }
549
560
  };
550
561
  Activity.prototype._consumeRunQ = function consumeRunQ() {
551
- if (this[kConsuming]) return;
552
- this[kConsuming] = true;
562
+ this[kConsumingRunQ] = true;
553
563
  this.broker.getQueue('run-q').assertConsumer(this[kMessageHandlers].onRunMessage, {
554
564
  exclusive: true,
555
565
  consumerTag: '_activity-run'
556
566
  });
557
567
  };
568
+ Activity.prototype._pauseRunQ = function pauseRunQ() {
569
+ if (!this[kConsumingRunQ]) return;
570
+ this[kConsumingRunQ] = false;
571
+ this.broker.cancel('_activity-run');
572
+ };
558
573
  Activity.prototype._onRunMessage = function onRunMessage(routingKey, message, messageProperties) {
559
574
  switch (routingKey) {
560
575
  case 'run.outbound.discard':
@@ -709,8 +724,9 @@ Activity.prototype._continueRunMessage = function continueRunMessage(routingKey,
709
724
  break;
710
725
  }
711
726
  case 'run.next':
712
- this._consumeInbound();
713
- break;
727
+ message.ack();
728
+ this._pauseRunQ();
729
+ return this._consumeInbound();
714
730
  }
715
731
  if (!step) message.ack();
716
732
  };
@@ -877,8 +893,7 @@ Activity.prototype._publishEvent = function publishEvent(state, content, propert
877
893
  }), {
878
894
  ...properties,
879
895
  type: state,
880
- mandatory: state === 'error',
881
- persistent: 'persistent' in properties ? properties.persistent : state !== 'stop'
896
+ mandatory: state === 'error'
882
897
  });
883
898
  };
884
899
  Activity.prototype._onStop = function onStop(message) {
@@ -886,14 +901,16 @@ Activity.prototype._onStop = function onStop(message) {
886
901
  this.stopped = true;
887
902
  this[kConsuming] = false;
888
903
  const broker = this.broker;
889
- broker.cancel('_activity-run');
904
+ this._pauseRunQ();
890
905
  broker.cancel('_activity-api');
891
906
  broker.cancel('_activity-execution');
892
907
  broker.cancel('_run-on-inbound');
893
908
  broker.cancel('_format-consumer');
909
+ if (this.extensions) this.extensions.deactivate((0, _messageHelper.cloneMessage)(message));
894
910
  if (running) {
895
- if (this.extensions) this.extensions.deactivate((0, _messageHelper.cloneMessage)(message));
896
- this._publishEvent('stop', this._createMessage());
911
+ this._publishEvent('stop', this._createMessage(), {
912
+ persistent: false
913
+ });
897
914
  }
898
915
  };
899
916
  Activity.prototype._consumeApi = function consumeApi() {
@@ -952,7 +969,7 @@ Activity.prototype._getOutboundSequenceFlowById = function getOutboundSequenceFl
952
969
  Activity.prototype._deactivateRunConsumers = function _deactivateRunConsumers() {
953
970
  const broker = this.broker;
954
971
  broker.cancel('_activity-api');
955
- broker.cancel('_activity-run');
972
+ this._pauseRunQ();
956
973
  broker.cancel('_activity-execution');
957
974
  this[kConsuming] = false;
958
975
  };
@@ -23,7 +23,12 @@ class ActivityError extends Error {
23
23
  }
24
24
  }
25
25
  exports.ActivityError = ActivityError;
26
- class RunError extends ActivityError {}
26
+ class RunError extends ActivityError {
27
+ constructor(...args) {
28
+ super(...args);
29
+ this.type = 'RunError';
30
+ }
31
+ }
27
32
  exports.RunError = RunError;
28
33
  class BpmnError extends Error {
29
34
  constructor(description, behaviour = {}, sourceMessage, inner) {
@@ -5,10 +5,11 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = TimerEventDefinition;
7
7
  var _messageHelper = require("../messageHelper.js");
8
- var _isoDuration = require("../iso-duration.js");
8
+ var _piso = require("@0dep/piso");
9
9
  const kStopped = Symbol.for('stopped');
10
10
  const kTimerContent = Symbol.for('timerContent');
11
11
  const kTimer = Symbol.for('timer');
12
+ const timerTypes = ['timeDuration', 'timeDate', 'timeCycle'];
12
13
  function TimerEventDefinition(activity, eventDefinition) {
13
14
  const type = this.type = eventDefinition.type || 'TimerEventDefinition';
14
15
  this.activity = activity;
@@ -81,7 +82,6 @@ TimerEventDefinition.prototype.execute = function execute(executeMessage) {
81
82
  broker.publish('execution', 'execute.timer', (0, _messageHelper.cloneContent)(timerContent));
82
83
  broker.publish('event', 'activity.timer', (0, _messageHelper.cloneContent)(timerContent));
83
84
  if (this.stopped) return;
84
- if (timerContent.timeout === undefined) return this._debug(`waiting for ${timerContent.timerType || 'signal'}`);
85
85
  if (timerContent.timeout <= 0) return this._completed();
86
86
  const timers = this.environment.timers.register(timerContent);
87
87
  const delay = timerContent.timeout;
@@ -191,25 +191,21 @@ TimerEventDefinition.prototype._stop = function stop() {
191
191
  };
192
192
  TimerEventDefinition.prototype.parse = function parse(timerType, value) {
193
193
  let repeat, delay, expireAt;
194
+ const now = new Date();
194
195
  switch (timerType) {
195
196
  case 'timeCycle':
196
197
  case 'timeDuration':
197
198
  {
198
- const parsed = (0, _isoDuration.parse)(value);
199
+ const parsed = new _piso.ISOInterval(value).parse();
199
200
  if (parsed.repeat) repeat = parsed.repeat;
200
- delay = (0, _isoDuration.toSeconds)(parsed) * 1000;
201
- expireAt = new Date(Date.now() + delay);
201
+ expireAt = parsed.getExpireAt(now, now);
202
+ delay = expireAt.getTime() - now.getTime();
202
203
  break;
203
204
  }
204
205
  case 'timeDate':
205
206
  {
206
- const ms = Date.parse(value);
207
- if (!isNaN(ms)) {
208
- expireAt = new Date(ms);
209
- delay = Date.now() - expireAt;
210
- } else {
211
- throw new TypeError(`invalid timeDate >${value}<`);
212
- }
207
+ expireAt = (0, _piso.getDate)(value);
208
+ delay = now.getTime() - expireAt;
213
209
  break;
214
210
  }
215
211
  }
@@ -226,28 +222,22 @@ TimerEventDefinition.prototype._getTimers = function getTimers(executeMessage) {
226
222
  expireAt: new Date(content.expireAt)
227
223
  })
228
224
  };
229
- let parseErr;
230
- for (const t of ['timeDuration', 'timeDate', 'timeCycle']) {
231
- if (t in content) result[t] = content[t];else if (t in this) result[t] = this.environment.resolveExpression(this[t], executeMessage);else continue;
225
+ for (const timerType of timerTypes) {
226
+ if (timerType in content) result[timerType] = content[timerType];else if (timerType in this) result[timerType] = this.environment.resolveExpression(this[timerType], executeMessage);else continue;
232
227
  let expireAtDate, repeat;
233
- const timerStr = result[t];
228
+ const timerStr = result[timerType];
234
229
  if (timerStr) {
235
- try {
236
- const {
237
- repeat: parsedRepeat,
238
- expireAt: parsedExpireAt
239
- } = this.parse(t, timerStr);
240
- repeat = parsedRepeat;
241
- expireAtDate = parsedExpireAt;
242
- } catch (err) {
243
- parseErr = err;
244
- }
230
+ const {
231
+ repeat: parsedRepeat,
232
+ expireAt: parsedExpireAt
233
+ } = this.parse(timerType, timerStr);
234
+ repeat = parsedRepeat;
235
+ expireAtDate = parsedExpireAt;
245
236
  } else {
246
237
  expireAtDate = new Date();
247
238
  }
248
- if (!expireAtDate) continue;
249
239
  if (!('expireAt' in result) || result.expireAt > expireAtDate) {
250
- result.timerType = t;
240
+ result.timerType = timerType;
251
241
  result.expireAt = expireAtDate;
252
242
  result.repeat = repeat;
253
243
  }
@@ -259,9 +249,6 @@ TimerEventDefinition.prototype._getTimers = function getTimers(executeMessage) {
259
249
  } else if (!Object.keys(result).length) {
260
250
  result.timeout = 0;
261
251
  }
262
- if (!('timeout' in result) && parseErr) {
263
- this.logger.warn(`<${this.activity.id}> failed to parse timer: ${parseErr.message}`);
264
- }
265
252
  if (content.inbound && 'repeat' in content.inbound[0]) {
266
253
  result.repeat = content.inbound[0].repeat;
267
254
  }
package/dist/index.js CHANGED
@@ -9,6 +9,12 @@ Object.defineProperty(exports, "Activity", {
9
9
  return _Activity.default;
10
10
  }
11
11
  });
12
+ Object.defineProperty(exports, "ActivityError", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _Errors.ActivityError;
16
+ }
17
+ });
12
18
  Object.defineProperty(exports, "Association", {
13
19
  enumerable: true,
14
20
  get: function () {
@@ -147,7 +153,6 @@ Object.defineProperty(exports, "Group", {
147
153
  return _Dummy.default;
148
154
  }
149
155
  });
150
- exports.ISODuration = void 0;
151
156
  Object.defineProperty(exports, "InclusiveGateway", {
152
157
  enumerable: true,
153
158
  get: function () {
@@ -238,6 +243,12 @@ Object.defineProperty(exports, "ReceiveTask", {
238
243
  return _ReceiveTask.default;
239
244
  }
240
245
  });
246
+ Object.defineProperty(exports, "RunError", {
247
+ enumerable: true,
248
+ get: function () {
249
+ return _Errors.RunError;
250
+ }
251
+ });
241
252
  Object.defineProperty(exports, "ScriptTask", {
242
253
  enumerable: true,
243
254
  get: function () {
@@ -396,8 +407,5 @@ var _TerminateEventDefinition = _interopRequireDefault(require("./eventDefinitio
396
407
  var _TimerEventDefinition = _interopRequireDefault(require("./eventDefinitions/TimerEventDefinition.js"));
397
408
  var _Transaction = _interopRequireDefault(require("./tasks/Transaction.js"));
398
409
  var _Timers = require("./Timers.js");
399
- var ISODuration = _interopRequireWildcard(require("./iso-duration.js"));
400
- exports.ISODuration = ISODuration;
401
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
402
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
410
+ var _Errors = require("./error/Errors.js");
403
411
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bpmn-elements",
3
- "version": "13.2.0",
3
+ "version": "14.0.0",
4
4
  "description": "Executable workflow elements based on BPMN 2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -86,6 +86,7 @@
86
86
  "prettier": "^3.2.5"
87
87
  },
88
88
  "dependencies": {
89
- "smqp": "^8.2.2"
89
+ "@0dep/piso": "^0.1.3",
90
+ "smqp": "^8.2.4"
90
91
  }
91
92
  }
@@ -8,6 +8,7 @@ import { makeErrorFromMessage, ActivityError } from '../error/Errors.js';
8
8
 
9
9
  const kActivityDef = Symbol.for('activityDefinition');
10
10
  const kConsuming = Symbol.for('consuming');
11
+ const kConsumingRunQ = Symbol.for('run queue consumer');
11
12
  const kCounters = Symbol.for('counters');
12
13
  const kEventDefinitions = Symbol.for('eventDefinitions');
13
14
  const kExec = Symbol.for('exec');
@@ -100,6 +101,8 @@ function Activity(Behaviour, activityDef, context) {
100
101
 
101
102
  this[kEventDefinitions] = eventDefinitions && eventDefinitions.map((ed) => new ed.Behaviour(this, ed, this.context));
102
103
  this[kExtensions] = context.loadExtensions(this);
104
+ this[kConsuming] = false;
105
+ this[kConsumingRunQ] = undefined;
103
106
  }
104
107
 
105
108
  Object.defineProperties(Activity.prototype, {
@@ -230,6 +233,7 @@ Object.defineProperties(Activity.prototype, {
230
233
  });
231
234
 
232
235
  Activity.prototype.activate = function activate() {
236
+ if (this[kActivated]) return;
233
237
  this[kActivated] = true;
234
238
  this.addInboundListeners();
235
239
  return this._consumeInbound();
@@ -267,6 +271,7 @@ Activity.prototype.run = function run(runContent) {
267
271
  broker.publish('run', 'run.enter', content);
268
272
  broker.publish('run', 'run.start', cloneContent(content));
269
273
 
274
+ this[kConsuming] = true;
270
275
  this._consumeRunQ();
271
276
  };
272
277
 
@@ -320,6 +325,8 @@ Activity.prototype.resume = function resume() {
320
325
 
321
326
  const content = this._createMessage();
322
327
  this.broker.publish('run', 'run.resume', content, { persistent: false });
328
+
329
+ this[kConsuming] = true;
323
330
  this._consumeRunQ();
324
331
  };
325
332
 
@@ -332,6 +339,7 @@ Activity.prototype.discard = function discard(discardContent) {
332
339
  const broker = this.broker;
333
340
  broker.getQueue('run-q').purge();
334
341
  broker.publish('run', 'run.discard', cloneContent(this[kStateMessage].content));
342
+ this[kConsuming] = true;
335
343
  this._consumeRunQ();
336
344
  };
337
345
 
@@ -398,6 +406,7 @@ Activity.prototype._runDiscard = function runDiscard(discardContent) {
398
406
  const content = this._createMessage({ ...discardContent, executionId });
399
407
  this.broker.publish('run', 'run.discard', content);
400
408
 
409
+ this[kConsuming] = true;
401
410
  this._consumeRunQ();
402
411
  };
403
412
 
@@ -409,6 +418,7 @@ Activity.prototype._discardRun = function discardRun() {
409
418
  if (execution && !execution.completed) return;
410
419
 
411
420
  switch (status) {
421
+ case 'end':
412
422
  case 'executing':
413
423
  case 'error':
414
424
  case 'discarded':
@@ -417,11 +427,13 @@ Activity.prototype._discardRun = function discardRun() {
417
427
 
418
428
  this._deactivateRunConsumers();
419
429
 
420
- const message = this[kStateMessage];
421
- if (this.extensions) this.extensions.deactivate(cloneMessage(message));
430
+ const stateMessage = this[kStateMessage];
431
+ if (this.extensions) this.extensions.deactivate(cloneMessage(stateMessage));
422
432
  const broker = this.broker;
423
433
  broker.getQueue('run-q').purge();
424
- broker.publish('run', 'run.discard', cloneContent(message.content));
434
+
435
+ broker.publish('run', 'run.discard', cloneContent(stateMessage.content));
436
+ this[kConsuming] = true;
425
437
  this._consumeRunQ();
426
438
  };
427
439
 
@@ -444,12 +456,15 @@ Activity.prototype._consumeInbound = function consumeInbound() {
444
456
  if (!this[kActivated]) return;
445
457
 
446
458
  if (this.status) return;
459
+
447
460
  const inboundQ = this.broker.getQueue('inbound-q');
461
+ const onInbound = this[kMessageHandlers].onInbound;
462
+
448
463
  if (this[kFlags].isParallelJoin) {
449
- return inboundQ.consume(this[kMessageHandlers].onInbound, { consumerTag: '_run-on-inbound', prefetch: 1000 });
464
+ return inboundQ.consume(onInbound, { consumerTag: '_run-on-inbound', prefetch: 1000 });
450
465
  }
451
466
 
452
- return inboundQ.consume(this[kMessageHandlers].onInbound, { consumerTag: '_run-on-inbound' });
467
+ return inboundQ.consume(onInbound, { consumerTag: '_run-on-inbound' });
453
468
  };
454
469
 
455
470
  Activity.prototype._onInbound = function onInbound(routingKey, message) {
@@ -542,12 +557,17 @@ Activity.prototype._onInboundEvent = function onInboundEvent(routingKey, message
542
557
  };
543
558
 
544
559
  Activity.prototype._consumeRunQ = function consumeRunQ() {
545
- if (this[kConsuming]) return;
546
-
547
- this[kConsuming] = true;
560
+ this[kConsumingRunQ] = true;
548
561
  this.broker.getQueue('run-q').assertConsumer(this[kMessageHandlers].onRunMessage, { exclusive: true, consumerTag: '_activity-run' });
549
562
  };
550
563
 
564
+ Activity.prototype._pauseRunQ = function pauseRunQ() {
565
+ if (!this[kConsumingRunQ]) return;
566
+
567
+ this[kConsumingRunQ] = false;
568
+ this.broker.cancel('_activity-run');
569
+ };
570
+
551
571
  Activity.prototype._onRunMessage = function onRunMessage(routingKey, message, messageProperties) {
552
572
  switch (routingKey) {
553
573
  case 'run.outbound.discard':
@@ -695,8 +715,9 @@ Activity.prototype._continueRunMessage = function continueRunMessage(routingKey,
695
715
  break;
696
716
  }
697
717
  case 'run.next':
698
- this._consumeInbound();
699
- break;
718
+ message.ack();
719
+ this._pauseRunQ();
720
+ return this._consumeInbound();
700
721
  }
701
722
 
702
723
  if (!step) message.ack();
@@ -857,7 +878,6 @@ Activity.prototype._publishEvent = function publishEvent(state, content, propert
857
878
  ...properties,
858
879
  type: state,
859
880
  mandatory: state === 'error',
860
- persistent: 'persistent' in properties ? properties.persistent : state !== 'stop',
861
881
  });
862
882
  };
863
883
 
@@ -868,15 +888,16 @@ Activity.prototype._onStop = function onStop(message) {
868
888
 
869
889
  this[kConsuming] = false;
870
890
  const broker = this.broker;
871
- broker.cancel('_activity-run');
891
+ this._pauseRunQ();
872
892
  broker.cancel('_activity-api');
873
893
  broker.cancel('_activity-execution');
874
894
  broker.cancel('_run-on-inbound');
875
895
  broker.cancel('_format-consumer');
876
896
 
897
+ if (this.extensions) this.extensions.deactivate(cloneMessage(message));
898
+
877
899
  if (running) {
878
- if (this.extensions) this.extensions.deactivate(cloneMessage(message));
879
- this._publishEvent('stop', this._createMessage());
900
+ this._publishEvent('stop', this._createMessage(), { persistent: false });
880
901
  }
881
902
  };
882
903
 
@@ -933,7 +954,7 @@ Activity.prototype._getOutboundSequenceFlowById = function getOutboundSequenceFl
933
954
  Activity.prototype._deactivateRunConsumers = function _deactivateRunConsumers() {
934
955
  const broker = this.broker;
935
956
  broker.cancel('_activity-api');
936
- broker.cancel('_activity-run');
957
+ this._pauseRunQ();
937
958
  broker.cancel('_activity-execution');
938
959
  this[kConsuming] = false;
939
960
  };
@@ -16,7 +16,12 @@ class ActivityError extends Error {
16
16
  }
17
17
  }
18
18
 
19
- class RunError extends ActivityError {}
19
+ class RunError extends ActivityError {
20
+ constructor(...args) {
21
+ super(...args);
22
+ this.type = 'RunError';
23
+ }
24
+ }
20
25
 
21
26
  class BpmnError extends Error {
22
27
  constructor(description, behaviour = {}, sourceMessage, inner) {
@@ -38,12 +43,14 @@ export { ActivityError, BpmnError, RunError, makeErrorFromMessage };
38
43
 
39
44
  function makeErrorFromMessage(errorMessage) {
40
45
  const { content } = errorMessage;
46
+
41
47
  if (isKnownError(content)) return content;
42
48
 
43
49
  const { error } = content;
44
50
  if (!error) return new Error(`Malformatted error message with routing key ${errorMessage.fields && errorMessage.fields.routingKey}`);
45
51
 
46
52
  if (isKnownError(error)) return error;
53
+
47
54
  switch (error.type) {
48
55
  case 'ActivityError':
49
56
  return new ActivityError(
@@ -1,10 +1,12 @@
1
1
  import { cloneContent } from '../messageHelper.js';
2
- import { toSeconds, parse as parseIsoDuration } from '../iso-duration.js';
2
+ import { ISOInterval, getDate } from '@0dep/piso';
3
3
 
4
4
  const kStopped = Symbol.for('stopped');
5
5
  const kTimerContent = Symbol.for('timerContent');
6
6
  const kTimer = Symbol.for('timer');
7
7
 
8
+ const timerTypes = ['timeDuration', 'timeDate', 'timeCycle'];
9
+
8
10
  export default function TimerEventDefinition(activity, eventDefinition) {
9
11
  const type = (this.type = eventDefinition.type || 'TimerEventDefinition');
10
12
  this.activity = activity;
@@ -80,7 +82,6 @@ TimerEventDefinition.prototype.execute = function execute(executeMessage) {
80
82
 
81
83
  if (this.stopped) return;
82
84
 
83
- if (timerContent.timeout === undefined) return this._debug(`waiting for ${timerContent.timerType || 'signal'}`);
84
85
  if (timerContent.timeout <= 0) return this._completed();
85
86
 
86
87
  const timers = this.environment.timers.register(timerContent);
@@ -188,23 +189,19 @@ TimerEventDefinition.prototype._stop = function stop() {
188
189
 
189
190
  TimerEventDefinition.prototype.parse = function parse(timerType, value) {
190
191
  let repeat, delay, expireAt;
192
+ const now = new Date();
191
193
  switch (timerType) {
192
194
  case 'timeCycle':
193
195
  case 'timeDuration': {
194
- const parsed = parseIsoDuration(value);
196
+ const parsed = new ISOInterval(value).parse();
195
197
  if (parsed.repeat) repeat = parsed.repeat;
196
- delay = toSeconds(parsed) * 1000;
197
- expireAt = new Date(Date.now() + delay);
198
+ expireAt = parsed.getExpireAt(now, now);
199
+ delay = expireAt.getTime() - now.getTime();
198
200
  break;
199
201
  }
200
202
  case 'timeDate': {
201
- const ms = Date.parse(value);
202
- if (!isNaN(ms)) {
203
- expireAt = new Date(ms);
204
- delay = Date.now() - expireAt;
205
- } else {
206
- throw new TypeError(`invalid timeDate >${value}<`);
207
- }
203
+ expireAt = getDate(value);
204
+ delay = now.getTime() - expireAt;
208
205
  break;
209
206
  }
210
207
  }
@@ -223,29 +220,23 @@ TimerEventDefinition.prototype._getTimers = function getTimers(executeMessage) {
223
220
  ...('expireAt' in content && { expireAt: new Date(content.expireAt) }),
224
221
  };
225
222
 
226
- let parseErr;
227
- for (const t of ['timeDuration', 'timeDate', 'timeCycle']) {
228
- if (t in content) result[t] = content[t];
229
- else if (t in this) result[t] = this.environment.resolveExpression(this[t], executeMessage);
223
+ for (const timerType of timerTypes) {
224
+ if (timerType in content) result[timerType] = content[timerType];
225
+ else if (timerType in this) result[timerType] = this.environment.resolveExpression(this[timerType], executeMessage);
230
226
  else continue;
231
227
 
232
228
  let expireAtDate, repeat;
233
- const timerStr = result[t];
229
+ const timerStr = result[timerType];
234
230
  if (timerStr) {
235
- try {
236
- const { repeat: parsedRepeat, expireAt: parsedExpireAt } = this.parse(t, timerStr);
237
- repeat = parsedRepeat;
238
- expireAtDate = parsedExpireAt;
239
- } catch (err) {
240
- parseErr = err;
241
- }
231
+ const { repeat: parsedRepeat, expireAt: parsedExpireAt } = this.parse(timerType, timerStr);
232
+ repeat = parsedRepeat;
233
+ expireAtDate = parsedExpireAt;
242
234
  } else {
243
235
  expireAtDate = new Date();
244
236
  }
245
237
 
246
- if (!expireAtDate) continue;
247
238
  if (!('expireAt' in result) || result.expireAt > expireAtDate) {
248
- result.timerType = t;
239
+ result.timerType = timerType;
249
240
  result.expireAt = expireAtDate;
250
241
  result.repeat = repeat;
251
242
  }
@@ -259,10 +250,6 @@ TimerEventDefinition.prototype._getTimers = function getTimers(executeMessage) {
259
250
  result.timeout = 0;
260
251
  }
261
252
 
262
- if (!('timeout' in result) && parseErr) {
263
- this.logger.warn(`<${this.activity.id}> failed to parse timer: ${parseErr.message}`);
264
- }
265
-
266
253
  if (content.inbound && 'repeat' in content.inbound[0]) {
267
254
  result.repeat = content.inbound[0].repeat;
268
255
  }
package/src/index.js CHANGED
@@ -48,7 +48,8 @@ import TerminateEventDefinition from './eventDefinitions/TerminateEventDefinitio
48
48
  import TimerEventDefinition from './eventDefinitions/TimerEventDefinition.js';
49
49
  import Transaction from './tasks/Transaction.js';
50
50
  import { Timers } from './Timers.js';
51
- import * as ISODuration from './iso-duration.js';
51
+
52
+ export { ActivityError, RunError } from './error/Errors.js';
52
53
 
53
54
  export {
54
55
  Association,
@@ -108,5 +109,4 @@ export {
108
109
  TimerEventDefinition,
109
110
  Transaction,
110
111
  Timers,
111
- ISODuration,
112
112
  };
@@ -1,87 +0,0 @@
1
- // License MIT @ https://tolu.mit-license.org/
2
-
3
- const numbers = '\\d+';
4
- const fractionalNumbers = ''.concat(numbers, '(?:[\\.,]').concat(numbers, ')?');
5
- const datePattern = '('.concat(numbers, 'Y)?(').concat(numbers, 'M)?(').concat(numbers, 'W)?(').concat(fractionalNumbers, 'D)?');
6
- const timePattern = 'T('.concat(fractionalNumbers, 'H)?(').concat(fractionalNumbers, 'M)?(').concat(fractionalNumbers, 'S)?');
7
-
8
- const rPattern = '(?:R('.concat(numbers).concat(')/)?');
9
- const iso8601 = rPattern.concat('P(?:').concat(datePattern, '(?:').concat(timePattern, ')?)');
10
- const objMap = ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
11
- const defaultDuration = Object.freeze({
12
- years: 0,
13
- months: 0,
14
- weeks: 0,
15
- days: 0,
16
- hours: 0,
17
- minutes: 0,
18
- seconds: 0,
19
- });
20
-
21
- /**
22
- * The ISO8601 regex for matching / testing durations
23
- */
24
- const pattern = new RegExp(iso8601);
25
-
26
- /** Parse PnYnMnDTnHnMnS format to object */
27
- export function parse(durationString) {
28
- const matches = durationString.replace(/,/g, '.').match(pattern);
29
- if (!matches) {
30
- throw new RangeError('invalid duration: ' + durationString);
31
- }
32
-
33
- // Slice away repeat and first entry in match-array (the input string)
34
- const slicedMatches = matches.slice(2);
35
- if (slicedMatches.filter(Boolean).length === 0) {
36
- throw new RangeError('invalid duration: ' + durationString);
37
- }
38
- // Check only one fraction is used
39
- if (
40
- slicedMatches.filter((v) => {
41
- return /\./.test(v || '');
42
- }).length > 1
43
- ) {
44
- throw new RangeError(
45
- 'Fractions are allowed on the smallest unit in the string, e.g. P0.5D or PT1.0001S but not PT0.5M0.1S: ' + durationString,
46
- );
47
- }
48
-
49
- const result = {};
50
- if (matches[1]) result.repeat = Number(matches[1]);
51
-
52
- return slicedMatches.reduce((prev, next, idx) => {
53
- prev[objMap[idx]] = parseFloat(next || '0') || 0;
54
- return prev;
55
- }, result);
56
- }
57
-
58
- /** Convert ISO8601 duration object to an end Date. */
59
- export function end(durationInput, startDate) {
60
- const duration = Object.assign({}, defaultDuration, durationInput);
61
- // Create two equal timestamps, add duration to 'then' and return time difference
62
- const timestamp = startDate.getTime();
63
- const then = new Date(timestamp);
64
- then.setFullYear(then.getFullYear() + duration.years);
65
- then.setMonth(then.getMonth() + duration.months);
66
- then.setDate(then.getDate() + duration.days);
67
- // set time as milliseconds to get fractions working for minutes/hours
68
- const hoursInMs = duration.hours * 3600 * 1000;
69
- const minutesInMs = duration.minutes * 60 * 1000;
70
- then.setMilliseconds(then.getMilliseconds() + duration.seconds * 1000 + hoursInMs + minutesInMs);
71
- // Special case weeks
72
- then.setDate(then.getDate() + duration.weeks * 7);
73
- return then;
74
- }
75
-
76
- /** Convert ISO8601 duration object to seconds */
77
- export function toSeconds(durationInput, startDate) {
78
- if (startDate === void 0) {
79
- startDate = new Date();
80
- }
81
- const duration = Object.assign({}, defaultDuration, durationInput);
82
- const timestamp = startDate.getTime();
83
- const now = new Date(timestamp);
84
- const then = end(duration, now);
85
- const seconds = (then.getTime() - now.getTime()) / 1000;
86
- return seconds;
87
- }