@stateforward/hsm.ts 0.1.0 → 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
@@ -608,13 +608,23 @@ var Queue = class {
608
608
  front = [];
609
609
  back = [];
610
610
  backHead = 0;
611
+ fifo;
612
+ constructor(fifo) {
613
+ this.fifo = fifo;
614
+ }
611
615
  len() {
616
+ if (this.fifo) {
617
+ const lenOrError = this.fifo.len();
618
+ return typeof lenOrError === "number" ? this.front.length + lenOrError : lenOrError;
619
+ }
612
620
  return this.front.length + (this.back.length - this.backHead);
613
621
  }
614
622
  pop() {
615
623
  var event2;
616
624
  if (this.front.length > 0) {
617
625
  event2 = this.front.pop();
626
+ } else if (this.fifo) {
627
+ event2 = this.fifo.pop();
618
628
  } else if (this.backHead < this.back.length) {
619
629
  event2 = this.back[this.backHead];
620
630
  this.back[this.backHead] = void 0;
@@ -626,17 +636,25 @@ var Queue = class {
626
636
  }
627
637
  return event2;
628
638
  }
629
- push(...events) {
630
- for (var i = 0; i < events.length; i++) {
631
- var event2 = events[i];
632
- if (isKind(event2.kind, kinds.CompletionEvent)) {
633
- this.front.push(event2);
634
- } else {
635
- this.back.push(event2);
636
- }
639
+ push(event2) {
640
+ if (isKind(event2.kind, kinds.CompletionEvent)) {
641
+ this.front.push(event2);
642
+ } else if (this.fifo) {
643
+ return this.fifo.push(event2);
644
+ } else {
645
+ this.back.push(event2);
637
646
  }
638
647
  }
639
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
+ }
640
658
  function buildTransitionTable(model) {
641
659
  for (var stateName in model.members) {
642
660
  var state2 = model.members[stateName];
@@ -812,7 +830,7 @@ var HSM = class _HSM {
812
830
  this.ctx = ctxOrInstance;
813
831
  this.model = maybeModelOrConfig;
814
832
  this.currentState = this.model;
815
- this.queue = new Queue();
833
+ this.queue = new Queue(maybeConfig?.queue);
816
834
  this.active = {};
817
835
  this.processing = false;
818
836
  this.id = id2;
@@ -1008,7 +1026,7 @@ var HSM = class _HSM {
1008
1026
  qualifiedName: this.model.qualifiedName,
1009
1027
  state: currentStateName,
1010
1028
  attributes: Object.assign({}, this.attributes),
1011
- queueLen: this.queue.len(),
1029
+ queueLen: this.len(),
1012
1030
  events
1013
1031
  };
1014
1032
  }
@@ -1052,7 +1070,7 @@ var HSM = class _HSM {
1052
1070
  if (!event2.kind) {
1053
1071
  event2.kind = kinds.Event;
1054
1072
  }
1055
- this.queue.push(event2);
1073
+ this.push(event2);
1056
1074
  this.notify("dispatched", event2.name);
1057
1075
  if (this.processing) {
1058
1076
  return;
@@ -1064,9 +1082,9 @@ var HSM = class _HSM {
1064
1082
  this.process();
1065
1083
  }
1066
1084
  process() {
1067
- var deferred = new Array(this.queue.len() + 1);
1085
+ var deferred = new Array(this.len() + 1);
1068
1086
  var deferredCount = 0;
1069
- var event2 = this.queue.pop();
1087
+ var event2 = this.pop();
1070
1088
  while (event2) {
1071
1089
  var currentStateName = this.currentState.qualifiedName;
1072
1090
  var eventName = event2.name;
@@ -1074,7 +1092,7 @@ var HSM = class _HSM {
1074
1092
  var isDeferred = deferredLookup && deferredLookup[eventName] === true;
1075
1093
  if (isDeferred) {
1076
1094
  deferred[deferredCount++] = event2;
1077
- event2 = this.queue.pop();
1095
+ event2 = this.pop();
1078
1096
  continue;
1079
1097
  }
1080
1098
  var transitions = this.model.transitionMap[currentStateName][eventName];
@@ -1107,7 +1125,7 @@ var HSM = class _HSM {
1107
1125
  if (nextState.qualifiedName !== this.currentState.qualifiedName) {
1108
1126
  this.currentState = nextState;
1109
1127
  for (var j = 0; j < deferredCount; j++) {
1110
- this.queue.push(deferred[j]);
1128
+ this.push(deferred[j]);
1111
1129
  }
1112
1130
  deferredCount = 0;
1113
1131
  }
@@ -1115,14 +1133,39 @@ var HSM = class _HSM {
1115
1133
  }
1116
1134
  }
1117
1135
  this.notify("processed", event2.name);
1118
- event2 = this.queue.pop();
1136
+ event2 = this.pop();
1119
1137
  }
1120
1138
  for (var i = 0; i < deferredCount; i++) {
1121
- this.queue.push(deferred[i]);
1139
+ this.push(deferred[i]);
1122
1140
  }
1123
1141
  this.processing = false;
1124
1142
  this.notify("processed", "__next__");
1125
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
+ }
1126
1169
  transition(current, transition2, event2) {
1127
1170
  var path = transition2.paths[current.qualifiedName];
1128
1171
  if (!path) {