@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.d.cts CHANGED
@@ -89,9 +89,9 @@ type ClockConfig = {
89
89
  now: () => number;
90
90
  };
91
91
  type QueueShape = {
92
- push: (event: EventRecord) => void;
93
- pop: () => EventRecord | undefined;
94
- len: () => number;
92
+ push: (event: EventRecord) => void | unknown;
93
+ pop: () => EventRecord | undefined | unknown;
94
+ len: () => number | unknown;
95
95
  };
96
96
  type StripLeadingSlash<S extends string> = S extends `/${infer Rest}` ? Rest : S;
97
97
  type StripTrailingSlash<S extends string> = S extends `${infer Rest}/` ? Rest : S;
@@ -775,9 +775,9 @@ declare class Queue {
775
775
  backHead: number;
776
776
  private fifo?;
777
777
  constructor(fifo?: QueueShape);
778
- len(): number;
779
- pop(): EventRecord | undefined;
780
- push(event: EventRecord): void;
778
+ len(): number | unknown;
779
+ pop(): EventRecord | undefined | unknown;
780
+ push(event: EventRecord): void | unknown;
781
781
  }
782
782
  type InstanceConstructor = {
783
783
  new (): Instance;
@@ -831,6 +831,9 @@ declare class HSM {
831
831
  private followHistoryDefault;
832
832
  dispatch(event: EventRecord): void;
833
833
  private process;
834
+ private push;
835
+ private pop;
836
+ private len;
834
837
  private transition;
835
838
  private enter;
836
839
  private exit;
package/dist/index.d.ts CHANGED
@@ -89,9 +89,9 @@ type ClockConfig = {
89
89
  now: () => number;
90
90
  };
91
91
  type QueueShape = {
92
- push: (event: EventRecord) => void;
93
- pop: () => EventRecord | undefined;
94
- len: () => number;
92
+ push: (event: EventRecord) => void | unknown;
93
+ pop: () => EventRecord | undefined | unknown;
94
+ len: () => number | unknown;
95
95
  };
96
96
  type StripLeadingSlash<S extends string> = S extends `/${infer Rest}` ? Rest : S;
97
97
  type StripTrailingSlash<S extends string> = S extends `${infer Rest}/` ? Rest : S;
@@ -775,9 +775,9 @@ declare class Queue {
775
775
  backHead: number;
776
776
  private fifo?;
777
777
  constructor(fifo?: QueueShape);
778
- len(): number;
779
- pop(): EventRecord | undefined;
780
- push(event: EventRecord): void;
778
+ len(): number | unknown;
779
+ pop(): EventRecord | undefined | unknown;
780
+ push(event: EventRecord): void | unknown;
781
781
  }
782
782
  type InstanceConstructor = {
783
783
  new (): Instance;
@@ -831,6 +831,9 @@ declare class HSM {
831
831
  private followHistoryDefault;
832
832
  dispatch(event: EventRecord): void;
833
833
  private process;
834
+ private push;
835
+ private pop;
836
+ private len;
834
837
  private transition;
835
838
  private enter;
836
839
  private exit;
package/dist/index.js CHANGED
@@ -349,7 +349,11 @@ var Queue = class {
349
349
  this.fifo = fifo;
350
350
  }
351
351
  len() {
352
- return this.front.length + (this.fifo ? this.fifo.len() : this.back.length - this.backHead);
352
+ if (this.fifo) {
353
+ const lenOrError = this.fifo.len();
354
+ return typeof lenOrError === "number" ? this.front.length + lenOrError : lenOrError;
355
+ }
356
+ return this.front.length + (this.back.length - this.backHead);
353
357
  }
354
358
  pop() {
355
359
  var event2;
@@ -372,12 +376,21 @@ var Queue = class {
372
376
  if (isKind(event2.kind, kinds.CompletionEvent)) {
373
377
  this.front.push(event2);
374
378
  } else if (this.fifo) {
375
- this.fifo.push(event2);
379
+ return this.fifo.push(event2);
376
380
  } else {
377
381
  this.back.push(event2);
378
382
  }
379
383
  }
380
384
  };
385
+ function isQueueError(value) {
386
+ if (value === void 0 || value === null) {
387
+ return false;
388
+ }
389
+ if (typeof value !== "object") {
390
+ return true;
391
+ }
392
+ return !("name" in value) || !("kind" in value);
393
+ }
381
394
  function buildTransitionTable(model) {
382
395
  for (var stateName in model.members) {
383
396
  var state2 = model.members[stateName];
@@ -749,7 +762,7 @@ var HSM = class _HSM {
749
762
  qualifiedName: this.model.qualifiedName,
750
763
  state: currentStateName,
751
764
  attributes: Object.assign({}, this.attributes),
752
- queueLen: this.queue.len(),
765
+ queueLen: this.len(),
753
766
  events
754
767
  };
755
768
  }
@@ -793,7 +806,7 @@ var HSM = class _HSM {
793
806
  if (!event2.kind) {
794
807
  event2.kind = kinds.Event;
795
808
  }
796
- this.queue.push(event2);
809
+ this.push(event2);
797
810
  this.notify("dispatched", event2.name);
798
811
  if (this.processing) {
799
812
  return;
@@ -805,9 +818,9 @@ var HSM = class _HSM {
805
818
  this.process();
806
819
  }
807
820
  process() {
808
- var deferred = new Array(this.queue.len() + 1);
821
+ var deferred = new Array(this.len() + 1);
809
822
  var deferredCount = 0;
810
- var event2 = this.queue.pop();
823
+ var event2 = this.pop();
811
824
  while (event2) {
812
825
  var currentStateName = this.currentState.qualifiedName;
813
826
  var eventName = event2.name;
@@ -815,7 +828,7 @@ var HSM = class _HSM {
815
828
  var isDeferred = deferredLookup && deferredLookup[eventName] === true;
816
829
  if (isDeferred) {
817
830
  deferred[deferredCount++] = event2;
818
- event2 = this.queue.pop();
831
+ event2 = this.pop();
819
832
  continue;
820
833
  }
821
834
  var transitions = this.model.transitionMap[currentStateName][eventName];
@@ -848,7 +861,7 @@ var HSM = class _HSM {
848
861
  if (nextState.qualifiedName !== this.currentState.qualifiedName) {
849
862
  this.currentState = nextState;
850
863
  for (var j = 0; j < deferredCount; j++) {
851
- this.queue.push(deferred[j]);
864
+ this.push(deferred[j]);
852
865
  }
853
866
  deferredCount = 0;
854
867
  }
@@ -856,14 +869,39 @@ var HSM = class _HSM {
856
869
  }
857
870
  }
858
871
  this.notify("processed", event2.name);
859
- event2 = this.queue.pop();
872
+ event2 = this.pop();
860
873
  }
861
874
  for (var i = 0; i < deferredCount; i++) {
862
- this.queue.push(deferred[i]);
875
+ this.push(deferred[i]);
863
876
  }
864
877
  this.processing = false;
865
878
  this.notify("processed", "__next__");
866
879
  }
880
+ push(event2) {
881
+ const error = this.queue.push(event2);
882
+ if (error && !isKind(event2.kind, kinds.ErrorEvent)) {
883
+ this.queue.push(Object.create(ErrorEvent, { data: { value: error } }));
884
+ }
885
+ }
886
+ pop() {
887
+ while (true) {
888
+ const eventOrError = this.queue.pop();
889
+ if (!isQueueError(eventOrError)) {
890
+ return eventOrError;
891
+ }
892
+ this.queue.push(Object.create(ErrorEvent, { data: { value: eventOrError } }));
893
+ }
894
+ }
895
+ len() {
896
+ const lenOrError = this.queue.len();
897
+ if (typeof lenOrError === "number") {
898
+ return lenOrError;
899
+ }
900
+ if (lenOrError) {
901
+ this.queue.push(Object.create(ErrorEvent, { data: { value: lenOrError } }));
902
+ }
903
+ return 0;
904
+ }
867
905
  transition(current, transition2, event2) {
868
906
  var path = transition2.paths[current.qualifiedName];
869
907
  if (!path) {