@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.d.cts CHANGED
@@ -81,12 +81,18 @@ type Config = {
81
81
  clearTimeout?: CancelFunction;
82
82
  now?: () => number;
83
83
  };
84
+ queue?: QueueShape;
84
85
  };
85
86
  type ClockConfig = {
86
87
  setTimeout: TimerFunction;
87
88
  clearTimeout: CancelFunction;
88
89
  now: () => number;
89
90
  };
91
+ type QueueShape = {
92
+ push: (event: EventRecord) => void | unknown;
93
+ pop: () => EventRecord | undefined | unknown;
94
+ len: () => number | unknown;
95
+ };
90
96
  type StripLeadingSlash<S extends string> = S extends `/${infer Rest}` ? Rest : S;
91
97
  type StripTrailingSlash<S extends string> = S extends `${infer Rest}/` ? Rest : S;
92
98
  type PathParts<S extends string> = S extends '' ? [] : S extends `${infer Head}/${infer Tail}` ? [Head, ...PathParts<Tail>] : [S];
@@ -767,9 +773,11 @@ declare class Queue {
767
773
  front: EventRecord[];
768
774
  back: Array<EventRecord | undefined>;
769
775
  backHead: number;
770
- len(): number;
771
- pop(): EventRecord | undefined;
772
- push(...events: EventRecord[]): void;
776
+ private fifo?;
777
+ constructor(fifo?: QueueShape);
778
+ len(): number | unknown;
779
+ pop(): EventRecord | undefined | unknown;
780
+ push(event: EventRecord): void | unknown;
773
781
  }
774
782
  type InstanceConstructor = {
775
783
  new (): Instance;
@@ -823,6 +831,9 @@ declare class HSM {
823
831
  private followHistoryDefault;
824
832
  dispatch(event: EventRecord): void;
825
833
  private process;
834
+ private push;
835
+ private pop;
836
+ private len;
826
837
  private transition;
827
838
  private enter;
828
839
  private exit;
package/dist/index.d.ts CHANGED
@@ -81,12 +81,18 @@ type Config = {
81
81
  clearTimeout?: CancelFunction;
82
82
  now?: () => number;
83
83
  };
84
+ queue?: QueueShape;
84
85
  };
85
86
  type ClockConfig = {
86
87
  setTimeout: TimerFunction;
87
88
  clearTimeout: CancelFunction;
88
89
  now: () => number;
89
90
  };
91
+ type QueueShape = {
92
+ push: (event: EventRecord) => void | unknown;
93
+ pop: () => EventRecord | undefined | unknown;
94
+ len: () => number | unknown;
95
+ };
90
96
  type StripLeadingSlash<S extends string> = S extends `/${infer Rest}` ? Rest : S;
91
97
  type StripTrailingSlash<S extends string> = S extends `${infer Rest}/` ? Rest : S;
92
98
  type PathParts<S extends string> = S extends '' ? [] : S extends `${infer Head}/${infer Tail}` ? [Head, ...PathParts<Tail>] : [S];
@@ -767,9 +773,11 @@ declare class Queue {
767
773
  front: EventRecord[];
768
774
  back: Array<EventRecord | undefined>;
769
775
  backHead: number;
770
- len(): number;
771
- pop(): EventRecord | undefined;
772
- push(...events: EventRecord[]): void;
776
+ private fifo?;
777
+ constructor(fifo?: QueueShape);
778
+ len(): number | unknown;
779
+ pop(): EventRecord | undefined | unknown;
780
+ push(event: EventRecord): void | unknown;
773
781
  }
774
782
  type InstanceConstructor = {
775
783
  new (): Instance;
@@ -823,6 +831,9 @@ declare class HSM {
823
831
  private followHistoryDefault;
824
832
  dispatch(event: EventRecord): void;
825
833
  private process;
834
+ private push;
835
+ private pop;
836
+ private len;
826
837
  private transition;
827
838
  private enter;
828
839
  private exit;
package/dist/index.js CHANGED
@@ -344,13 +344,23 @@ var Queue = class {
344
344
  front = [];
345
345
  back = [];
346
346
  backHead = 0;
347
+ fifo;
348
+ constructor(fifo) {
349
+ this.fifo = fifo;
350
+ }
347
351
  len() {
352
+ if (this.fifo) {
353
+ const lenOrError = this.fifo.len();
354
+ return typeof lenOrError === "number" ? this.front.length + lenOrError : lenOrError;
355
+ }
348
356
  return this.front.length + (this.back.length - this.backHead);
349
357
  }
350
358
  pop() {
351
359
  var event2;
352
360
  if (this.front.length > 0) {
353
361
  event2 = this.front.pop();
362
+ } else if (this.fifo) {
363
+ event2 = this.fifo.pop();
354
364
  } else if (this.backHead < this.back.length) {
355
365
  event2 = this.back[this.backHead];
356
366
  this.back[this.backHead] = void 0;
@@ -362,17 +372,25 @@ var Queue = class {
362
372
  }
363
373
  return event2;
364
374
  }
365
- push(...events) {
366
- for (var i = 0; i < events.length; i++) {
367
- var event2 = events[i];
368
- if (isKind(event2.kind, kinds.CompletionEvent)) {
369
- this.front.push(event2);
370
- } else {
371
- this.back.push(event2);
372
- }
375
+ push(event2) {
376
+ if (isKind(event2.kind, kinds.CompletionEvent)) {
377
+ this.front.push(event2);
378
+ } else if (this.fifo) {
379
+ return this.fifo.push(event2);
380
+ } else {
381
+ this.back.push(event2);
373
382
  }
374
383
  }
375
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
+ }
376
394
  function buildTransitionTable(model) {
377
395
  for (var stateName in model.members) {
378
396
  var state2 = model.members[stateName];
@@ -548,7 +566,7 @@ var HSM = class _HSM {
548
566
  this.ctx = ctxOrInstance;
549
567
  this.model = maybeModelOrConfig;
550
568
  this.currentState = this.model;
551
- this.queue = new Queue();
569
+ this.queue = new Queue(maybeConfig?.queue);
552
570
  this.active = {};
553
571
  this.processing = false;
554
572
  this.id = id2;
@@ -744,7 +762,7 @@ var HSM = class _HSM {
744
762
  qualifiedName: this.model.qualifiedName,
745
763
  state: currentStateName,
746
764
  attributes: Object.assign({}, this.attributes),
747
- queueLen: this.queue.len(),
765
+ queueLen: this.len(),
748
766
  events
749
767
  };
750
768
  }
@@ -788,7 +806,7 @@ var HSM = class _HSM {
788
806
  if (!event2.kind) {
789
807
  event2.kind = kinds.Event;
790
808
  }
791
- this.queue.push(event2);
809
+ this.push(event2);
792
810
  this.notify("dispatched", event2.name);
793
811
  if (this.processing) {
794
812
  return;
@@ -800,9 +818,9 @@ var HSM = class _HSM {
800
818
  this.process();
801
819
  }
802
820
  process() {
803
- var deferred = new Array(this.queue.len() + 1);
821
+ var deferred = new Array(this.len() + 1);
804
822
  var deferredCount = 0;
805
- var event2 = this.queue.pop();
823
+ var event2 = this.pop();
806
824
  while (event2) {
807
825
  var currentStateName = this.currentState.qualifiedName;
808
826
  var eventName = event2.name;
@@ -810,7 +828,7 @@ var HSM = class _HSM {
810
828
  var isDeferred = deferredLookup && deferredLookup[eventName] === true;
811
829
  if (isDeferred) {
812
830
  deferred[deferredCount++] = event2;
813
- event2 = this.queue.pop();
831
+ event2 = this.pop();
814
832
  continue;
815
833
  }
816
834
  var transitions = this.model.transitionMap[currentStateName][eventName];
@@ -843,7 +861,7 @@ var HSM = class _HSM {
843
861
  if (nextState.qualifiedName !== this.currentState.qualifiedName) {
844
862
  this.currentState = nextState;
845
863
  for (var j = 0; j < deferredCount; j++) {
846
- this.queue.push(deferred[j]);
864
+ this.push(deferred[j]);
847
865
  }
848
866
  deferredCount = 0;
849
867
  }
@@ -851,14 +869,39 @@ var HSM = class _HSM {
851
869
  }
852
870
  }
853
871
  this.notify("processed", event2.name);
854
- event2 = this.queue.pop();
872
+ event2 = this.pop();
855
873
  }
856
874
  for (var i = 0; i < deferredCount; i++) {
857
- this.queue.push(deferred[i]);
875
+ this.push(deferred[i]);
858
876
  }
859
877
  this.processing = false;
860
878
  this.notify("processed", "__next__");
861
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
+ }
862
905
  transition(current, transition2, event2) {
863
906
  var path = transition2.paths[current.qualifiedName];
864
907
  if (!path) {