@dra2020/baseclient 1.0.174 → 1.0.175

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.
@@ -2261,6 +2261,8 @@ class FsmManager {
2261
2261
  this.bTickSet = false;
2262
2262
  this.theTickList = new Set();
2263
2263
  this.theBusyLoopCount = 0;
2264
+ this.theExceptionCount = 0;
2265
+ this.onException = (fsm, err) => { console.error(`Fsm(${fsm.constructor.name}): exception in tick:`, err); };
2264
2266
  this.doTick = this.doTick.bind(this);
2265
2267
  }
2266
2268
  forceTick(fsm) {
@@ -2277,7 +2279,14 @@ class FsmManager {
2277
2279
  nLoops++;
2278
2280
  let thisTickList = this.theTickList;
2279
2281
  this.theTickList = new Set();
2280
- thisTickList.forEach((f) => f.tick());
2282
+ thisTickList.forEach((f) => {
2283
+ try {
2284
+ f.tick();
2285
+ }
2286
+ catch (err) {
2287
+ this.tickException(f, err);
2288
+ }
2289
+ });
2281
2290
  }
2282
2291
  if (this.theTickList.size == 0)
2283
2292
  this.theBusyLoopCount = 0;
@@ -2285,6 +2294,27 @@ class FsmManager {
2285
2294
  this.theBusyLoopCount++;
2286
2295
  this.theEpoch++;
2287
2296
  }
2297
+ // An exception escaping a tick would otherwise unwind out of setImmediate and take down the whole
2298
+ // process (or, in the browser, leave the tick loop wedged). Instead, report it and let the Fsm
2299
+ // fail itself so anything waiting on it sees a dependent error rather than hanging forever.
2300
+ tickException(f, err) {
2301
+ this.theExceptionCount++;
2302
+ try {
2303
+ this.onException(f, err);
2304
+ }
2305
+ catch (e) { }
2306
+ try {
2307
+ f.tickException(err);
2308
+ }
2309
+ catch (e) {
2310
+ try {
2311
+ this.onException(f, e);
2312
+ }
2313
+ catch (e2) { }
2314
+ if (!f.done)
2315
+ f.setState(exports.FSM_ERROR);
2316
+ }
2317
+ }
2288
2318
  }
2289
2319
  exports.FsmManager = FsmManager;
2290
2320
  class Fsm {
@@ -2364,6 +2394,12 @@ class Fsm {
2364
2394
  }
2365
2395
  tick() {
2366
2396
  }
2397
+ // Called (after FsmManager.onException has reported it) when tick() throws. Override to clean up or
2398
+ // respond, but make sure the Fsm ends up done.
2399
+ tickException(err) {
2400
+ if (!this.done)
2401
+ this.setState(exports.FSM_ERROR);
2402
+ }
2367
2403
  }
2368
2404
  exports.Fsm = Fsm;
2369
2405
  // Launches callback provided when the associated Fsm (or Fsm array) completes.