jssm 5.93.0 → 5.94.0

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.
@@ -101,7 +101,16 @@ declare class Machine<mDT> {
101
101
  _start_state_style: JssmStateConfig;
102
102
  _end_state_style: JssmStateConfig;
103
103
  _state_labels: Map<string, string>;
104
- constructor({ start_states, end_states, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, property_definition, state_property, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout, instance_name, history, data, default_state_config, default_active_state_config, default_hooked_state_config, default_terminal_state_config, default_start_state_config, default_end_state_config, allows_override, config_allows_override, rng_seed }: JssmGenericConfig<StateType, mDT>);
104
+ _time_source: () => number;
105
+ _create_started: number;
106
+ _created: number;
107
+ _after_mapping: Map<string, [string, number]>;
108
+ _timeout_source: (Function: any, number: any) => number;
109
+ _clear_timeout_source: (h: any) => void;
110
+ _timeout_handle: number | undefined;
111
+ _timeout_target: string | undefined;
112
+ _timeout_target_time: number | undefined;
113
+ constructor({ start_states, end_states, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, property_definition, state_property, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout, instance_name, history, data, default_state_config, default_active_state_config, default_hooked_state_config, default_terminal_state_config, default_start_state_config, default_end_state_config, allows_override, config_allows_override, rng_seed, time_source, timeout_source, clear_timeout_source }: JssmGenericConfig<StateType, mDT>);
105
114
  /********
106
115
  *
107
116
  * Internal method for fabricating states. Not meant for external use.
@@ -694,6 +703,7 @@ declare class Machine<mDT> {
694
703
  */
695
704
  override(newState: StateType, newData?: mDT | undefined): void;
696
705
  transition_impl(newStateOrAction: StateType, newData: mDT | undefined, wasForced: boolean, wasAction: boolean): boolean;
706
+ auto_set_state_timeout(): void;
697
707
  /*********
698
708
  *
699
709
  * Get a truncated history of the recent states and data of the machine.
@@ -1065,6 +1075,13 @@ declare class Machine<mDT> {
1065
1075
  valid_transition(newState: StateType, _newData?: mDT): boolean;
1066
1076
  valid_force_transition(newState: StateType, _newData?: mDT): boolean;
1067
1077
  instance_name(): string | undefined;
1078
+ get creation_date(): Date;
1079
+ get creation_timestamp(): number;
1080
+ get create_start_time(): number;
1081
+ set_state_timeout(next_state: StateType, after_time: number): void;
1082
+ clear_state_timeout(): void;
1083
+ state_timeout_for(which_state: StateType): [StateType, number] | undefined;
1084
+ current_state_timeout(): [StateType, number] | undefined;
1068
1085
  sm(template_strings: TemplateStringsArray, ...remainder: any[]): Machine<mDT>;
1069
1086
  }
1070
1087
  /*********
package/dist/es6/jssm.js CHANGED
@@ -126,7 +126,9 @@ function state_style_condense(jssk) {
126
126
  // TODO add a lotta docblock here
127
127
  class Machine {
128
128
  // whargarbl this badly needs to be broken up, monolith master
129
- constructor({ start_states, end_states = [], complete = [], transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, property_definition, state_property, fsl_version, dot_preamble = undefined, arrange_declaration = [], arrange_start_declaration = [], arrange_end_declaration = [], theme = ['default'], flow = 'down', graph_layout = 'dot', instance_name, history, data, default_state_config, default_active_state_config, default_hooked_state_config, default_terminal_state_config, default_start_state_config, default_end_state_config, allows_override, config_allows_override, rng_seed }) {
129
+ constructor({ start_states, end_states = [], complete = [], transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, property_definition, state_property, fsl_version, dot_preamble = undefined, arrange_declaration = [], arrange_start_declaration = [], arrange_end_declaration = [], theme = ['default'], flow = 'down', graph_layout = 'dot', instance_name, history, data, default_state_config, default_active_state_config, default_hooked_state_config, default_terminal_state_config, default_start_state_config, default_end_state_config, allows_override, config_allows_override, rng_seed, time_source, timeout_source, clear_timeout_source }) {
130
+ this._time_source = () => new Date().getTime();
131
+ this._create_started = this._time_source();
130
132
  this._instance_name = instance_name;
131
133
  this._state = start_states[0];
132
134
  this._states = new Map();
@@ -214,6 +216,12 @@ class Machine {
214
216
  this._state_labels = new Map();
215
217
  this._rng_seed = rng_seed !== null && rng_seed !== void 0 ? rng_seed : new Date().getTime();
216
218
  this._rng = gen_splitmix32(this._rng_seed);
219
+ this._timeout_source = timeout_source !== null && timeout_source !== void 0 ? timeout_source : ((f, a) => setTimeout(f, a));
220
+ this._clear_timeout_source = clear_timeout_source !== null && clear_timeout_source !== void 0 ? clear_timeout_source : ((h) => clearTimeout(h));
221
+ this._timeout_handle = undefined;
222
+ this._timeout_target = undefined;
223
+ this._timeout_target_time = undefined;
224
+ this._after_mapping = new Map();
217
225
  // consolidate the state declarations
218
226
  if (state_declaration) {
219
227
  state_declaration.map((state_decl) => {
@@ -275,6 +283,10 @@ class Machine {
275
283
  this._named_transitions.set(tr.name, thisEdgeId);
276
284
  }
277
285
  }
286
+ // set up the after mapping, if any
287
+ if (tr.after_time) {
288
+ this._after_mapping.set(tr.from, [tr.to, tr.after_time]);
289
+ }
278
290
  // set up the mapping, so that edges can be looked up by endpoint pairs
279
291
  const from_mapping = this._edge_map.get(tr.from) || new Map();
280
292
  if (!(this._edge_map.has(tr.from))) {
@@ -382,6 +394,8 @@ class Machine {
382
394
  if (!(start_states.length === this._start_states.size)) {
383
395
  throw new JssmError(this, `Start states cannot be repeated`);
384
396
  }
397
+ this._created = this._time_source();
398
+ this.auto_set_state_timeout();
385
399
  }
386
400
  /********
387
401
  *
@@ -1511,6 +1525,8 @@ class Machine {
1511
1525
  };
1512
1526
  if (valid) {
1513
1527
  if (this._has_hooks) {
1528
+ // once validity is known, clear old 'after' timeout clause
1529
+ this.clear_state_timeout();
1514
1530
  function update_fields(res) {
1515
1531
  if (res.hasOwnProperty('data')) {
1516
1532
  hook_args.data = res.data;
@@ -1700,8 +1716,17 @@ class Machine {
1700
1716
  }
1701
1717
  }
1702
1718
  }
1719
+ // possibly re-establish new 'after' clause
1720
+ this.auto_set_state_timeout();
1703
1721
  return true;
1704
1722
  }
1723
+ auto_set_state_timeout() {
1724
+ const after_res = this._after_mapping.get(this._state);
1725
+ if (after_res !== undefined) {
1726
+ const [next_state, after_time] = after_res;
1727
+ this.set_state_timeout(next_state, after_time);
1728
+ }
1729
+ }
1705
1730
  /*********
1706
1731
  *
1707
1732
  * Get a truncated history of the recent states and data of the machine.
@@ -2233,6 +2258,45 @@ class Machine {
2233
2258
  instance_name() {
2234
2259
  return this._instance_name;
2235
2260
  }
2261
+ get creation_date() {
2262
+ return new Date(Math.floor(this.creation_timestamp));
2263
+ }
2264
+ get creation_timestamp() {
2265
+ return this._created;
2266
+ }
2267
+ get create_start_time() {
2268
+ return this._create_started;
2269
+ }
2270
+ set_state_timeout(next_state, after_time) {
2271
+ if (this._timeout_handle !== undefined) {
2272
+ throw new JssmError(this, `Asked to set a state timeout to ${next_state}:${after_time}, but already timing out to ${this._timeout_target}:${this._timeout_target_time}`);
2273
+ }
2274
+ this._timeout_handle = this._timeout_source(
2275
+ // it seems like istanbul can't see this line being followed, even though it is, actively
2276
+ // this is enforced by the "after mapping runs normally with very short time" tests in after_mapping.spec
2277
+ // we'll mark it no-check so that our coverage numbers aren't wrecked
2278
+ /* istanbul ignore next */
2279
+ () => this.go(next_state), after_time);
2280
+ this._timeout_target = next_state;
2281
+ this._timeout_target_time = after_time;
2282
+ }
2283
+ clear_state_timeout() {
2284
+ if (this._timeout_handle === undefined) {
2285
+ return; // calling with no timeout is a no-op, means it can be called glad-handedly
2286
+ }
2287
+ this._clear_timeout_source(this._timeout_handle);
2288
+ this._timeout_handle = undefined;
2289
+ this._timeout_target = undefined;
2290
+ this._timeout_target_time = undefined;
2291
+ }
2292
+ state_timeout_for(which_state) {
2293
+ return this._after_mapping.get(which_state);
2294
+ }
2295
+ current_state_timeout() {
2296
+ return (this._timeout_target !== undefined)
2297
+ ? [this._timeout_target, this._timeout_target_time]
2298
+ : undefined;
2299
+ }
2236
2300
  /* eslint-disable no-use-before-define */
2237
2301
  /* eslint-disable class-methods-use-this */
2238
2302
  sm(template_strings, ...remainder /* , arguments */) {
@@ -21,6 +21,7 @@ function makeTransition(this_se, from, to, isRight, _wasList, _wasIndex) {
21
21
  from,
22
22
  to,
23
23
  kind,
24
+ after_time: isRight ? this_se.r_after : this_se.l_after,
24
25
  forced_only: kind === 'forced',
25
26
  main_path: kind === 'main'
26
27
  };
@@ -48,6 +48,8 @@ declare type JssmTransitionPermitterMaybeArray<DataType> = JssmTransitionPermitt
48
48
  declare type JssmTransition<StateType, DataType> = {
49
49
  from: StateType;
50
50
  to: StateType;
51
+ after_time?: number;
52
+ se?: JssmCompileSe<StateType, DataType>;
51
53
  name?: StateType;
52
54
  action?: StateType;
53
55
  check?: JssmTransitionPermitterMaybeArray<DataType>;
@@ -220,6 +222,9 @@ declare type JssmGenericConfig<StateType, DataType> = {
220
222
  default_terminal_state_config?: JssmStateStyleKeyList;
221
223
  default_active_state_config?: JssmStateStyleKeyList;
222
224
  rng_seed?: number | undefined;
225
+ time_source?: () => number;
226
+ timeout_source?: (Function: any, number: any) => number;
227
+ clear_timeout_source?: (number: any) => void;
223
228
  };
224
229
  declare type JssmCompileRule<StateType> = {
225
230
  agg_as: string;
@@ -227,12 +232,14 @@ declare type JssmCompileRule<StateType> = {
227
232
  };
228
233
  declare type JssmCompileSe<StateType, mDT> = {
229
234
  to: StateType;
230
- se: JssmCompileSe<StateType, mDT>;
235
+ se?: JssmCompileSe<StateType, mDT>;
231
236
  kind: JssmArrow;
232
237
  l_action?: StateType;
233
238
  r_action?: StateType;
234
239
  l_probability: number;
235
240
  r_probability: number;
241
+ l_after?: number;
242
+ r_after?: number;
236
243
  };
237
244
  declare type JssmCompileSeStart<StateType, DataType> = {
238
245
  from: StateType;
@@ -1,2 +1,2 @@
1
- const version = "5.93.0", build_time = 1703723662470;
1
+ const version = "5.94.0", build_time = 1703979205400;
2
2
  export { version, build_time };