@stateforward/hsm.ts 0.1.2 → 0.1.3

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.
package/dist/index.cjs CHANGED
@@ -613,7 +613,11 @@ var Queue = class {
613
613
  this.fifo = fifo;
614
614
  }
615
615
  len() {
616
- return this.front.length + (this.fifo ? this.fifo.len() : this.back.length - this.backHead);
616
+ if (this.fifo) {
617
+ const lenOrError = this.fifo.len();
618
+ return typeof lenOrError === "number" ? this.front.length + lenOrError : lenOrError;
619
+ }
620
+ return this.front.length + (this.back.length - this.backHead);
617
621
  }
618
622
  pop() {
619
623
  var event2;
@@ -636,12 +640,21 @@ var Queue = class {
636
640
  if (isKind(event2.kind, kinds.CompletionEvent)) {
637
641
  this.front.push(event2);
638
642
  } else if (this.fifo) {
639
- this.fifo.push(event2);
643
+ return this.fifo.push(event2);
640
644
  } else {
641
645
  this.back.push(event2);
642
646
  }
643
647
  }
644
648
  };
649
+ function isQueueError(value) {
650
+ if (value === void 0 || value === null) {
651
+ return false;
652
+ }
653
+ if (typeof value !== "object") {
654
+ return true;
655
+ }
656
+ return !("name" in value) || !("kind" in value);
657
+ }
645
658
  function buildTransitionTable(model) {
646
659
  for (var stateName in model.members) {
647
660
  var state2 = model.members[stateName];
@@ -1013,7 +1026,7 @@ var HSM = class _HSM {
1013
1026
  qualifiedName: this.model.qualifiedName,
1014
1027
  state: currentStateName,
1015
1028
  attributes: Object.assign({}, this.attributes),
1016
- queueLen: this.queue.len(),
1029
+ queueLen: this.len(),
1017
1030
  events
1018
1031
  };
1019
1032
  }
@@ -1057,7 +1070,7 @@ var HSM = class _HSM {
1057
1070
  if (!event2.kind) {
1058
1071
  event2.kind = kinds.Event;
1059
1072
  }
1060
- this.queue.push(event2);
1073
+ this.push(event2);
1061
1074
  this.notify("dispatched", event2.name);
1062
1075
  if (this.processing) {
1063
1076
  return;
@@ -1069,9 +1082,9 @@ var HSM = class _HSM {
1069
1082
  this.process();
1070
1083
  }
1071
1084
  process() {
1072
- var deferred = new Array(this.queue.len() + 1);
1085
+ var deferred = new Array(this.len() + 1);
1073
1086
  var deferredCount = 0;
1074
- var event2 = this.queue.pop();
1087
+ var event2 = this.pop();
1075
1088
  while (event2) {
1076
1089
  var currentStateName = this.currentState.qualifiedName;
1077
1090
  var eventName = event2.name;
@@ -1079,7 +1092,7 @@ var HSM = class _HSM {
1079
1092
  var isDeferred = deferredLookup && deferredLookup[eventName] === true;
1080
1093
  if (isDeferred) {
1081
1094
  deferred[deferredCount++] = event2;
1082
- event2 = this.queue.pop();
1095
+ event2 = this.pop();
1083
1096
  continue;
1084
1097
  }
1085
1098
  var transitions = this.model.transitionMap[currentStateName][eventName];
@@ -1112,7 +1125,7 @@ var HSM = class _HSM {
1112
1125
  if (nextState.qualifiedName !== this.currentState.qualifiedName) {
1113
1126
  this.currentState = nextState;
1114
1127
  for (var j = 0; j < deferredCount; j++) {
1115
- this.queue.push(deferred[j]);
1128
+ this.push(deferred[j]);
1116
1129
  }
1117
1130
  deferredCount = 0;
1118
1131
  }
@@ -1120,14 +1133,39 @@ var HSM = class _HSM {
1120
1133
  }
1121
1134
  }
1122
1135
  this.notify("processed", event2.name);
1123
- event2 = this.queue.pop();
1136
+ event2 = this.pop();
1124
1137
  }
1125
1138
  for (var i = 0; i < deferredCount; i++) {
1126
- this.queue.push(deferred[i]);
1139
+ this.push(deferred[i]);
1127
1140
  }
1128
1141
  this.processing = false;
1129
1142
  this.notify("processed", "__next__");
1130
1143
  }
1144
+ push(event2) {
1145
+ const error = this.queue.push(event2);
1146
+ if (error && !isKind(event2.kind, kinds.ErrorEvent)) {
1147
+ this.queue.push(Object.create(ErrorEvent, { data: { value: error } }));
1148
+ }
1149
+ }
1150
+ pop() {
1151
+ while (true) {
1152
+ const eventOrError = this.queue.pop();
1153
+ if (!isQueueError(eventOrError)) {
1154
+ return eventOrError;
1155
+ }
1156
+ this.queue.push(Object.create(ErrorEvent, { data: { value: eventOrError } }));
1157
+ }
1158
+ }
1159
+ len() {
1160
+ const lenOrError = this.queue.len();
1161
+ if (typeof lenOrError === "number") {
1162
+ return lenOrError;
1163
+ }
1164
+ if (lenOrError) {
1165
+ this.queue.push(Object.create(ErrorEvent, { data: { value: lenOrError } }));
1166
+ }
1167
+ return 0;
1168
+ }
1131
1169
  transition(current, transition2, event2) {
1132
1170
  var path = transition2.paths[current.qualifiedName];
1133
1171
  if (!path) {