@yuneta/gobj-js 7.13.4 → 7.13.6

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/README.md CHANGED
@@ -589,6 +589,10 @@ set_log_callback(fn) // fn(level, message)
589
589
 
590
590
  // Gate the DIRECT browser-console writes of all the functions above
591
591
  set_console_log_enabled(enabled) // default: true (unchanged behaviour)
592
+
593
+ // ...and a per-LINE say over them, on top of that switch (7.13.6)
594
+ set_console_log_filter(fn) // fn(level, msg) -> true to print
595
+ set_console_log_filter(null) // default: everything prints
592
596
  ```
593
597
 
594
598
  **Three independent output paths.** `set_console_log_enabled(false)` silences
@@ -598,6 +602,15 @@ a GUI route framework output — **including the automata/FSM trace, which arriv
598
602
  as `debug`** — into its own monitor window while keeping the browser console
599
603
  clean (gobj-ui's dev-window "Output" selector does exactly this).
600
604
 
605
+ **Why a filter and not just the switch.** The console write happens **before**
606
+ the sink is called, so nothing downstream can un-print a line: a consumer that
607
+ wants one CLASS of lines kept off the console — the dev monitor hiding the
608
+ `machine` trace's timer traffic, two `EV_TIMEOUT` lines a second for ever — can
609
+ only say so here. The filter decides the **console alone**: the sink still
610
+ receives every line, so a monitor keeps a complete record and decides
611
+ separately what to show. The master switch still wins, and a filter that
612
+ **throws** is ignored — a broken one must never be able to silence the log.
613
+
601
614
  ### Tracing: the `machine` trace, and the levels around it
602
615
 
603
616
  The FSM is where things happen, so the trace of the FSM **is** the execution
@@ -1802,17 +1802,39 @@ function gobj_walk_gobj_children_tree(gobj, walk_type, cb_walking, user_data, us
1802
1802
  /***************************************************************
1803
1803
  * Walking
1804
1804
  ***************************************************************/
1805
+ /***************************************************************
1806
+ * What a WALK CALLBACK answered, as the number the contract is
1807
+ * written in: -1 stop the walk, 0 descend into the children,
1808
+ * >0 do not descend.
1809
+ *
1810
+ * A javascript callback that simply RETURNS NOTHING is the common
1811
+ * case, and `undefined` is neither `< 0` nor `=== 0`, so the walk
1812
+ * used to read it as "do not descend" and skipped the subtree in
1813
+ * silence. Nothing is 0 here — the neutral answer, the one C gives
1814
+ * when an action has nothing to say.
1815
+ *
1816
+ * A boolean is NOT guessed: `true`/`false` could mean either half of
1817
+ * this contract, so it is reported and taken as 0. A walk that
1818
+ * silently loses a subtree is the thing this whole function exists
1819
+ * to stop.
1820
+ ***************************************************************/
1821
+ function walk_ret(ret) {
1822
+ if (typeof ret === "number" && isFinite(ret)) return ret;
1823
+ if (ret === void 0 || ret === null) return 0;
1824
+ log_error(`walk callback must return a NUMBER (-1 stop, 0 descend, >0 skip children), got ${typeof ret}: taken as 0`);
1825
+ return 0;
1826
+ }
1805
1827
  function rc_walk_by_list(iter_, walk_type, cb_walking, user_data, user_data2) {
1806
1828
  let iter = [...iter_];
1807
1829
  let child;
1808
1830
  if (walk_type & walk_type_t.WALK_LAST2FIRST) for (let i = iter.length; i > 0; i--) {
1809
1831
  child = iter[i - 1];
1810
- let ret = cb_walking(child, user_data, user_data2);
1832
+ let ret = walk_ret(cb_walking(child, user_data, user_data2));
1811
1833
  if (ret < 0) return ret;
1812
1834
  }
1813
1835
  else for (let i = 0; i < iter.length; i++) {
1814
1836
  child = iter[i];
1815
- let ret = cb_walking(child, user_data, user_data2);
1837
+ let ret = walk_ret(cb_walking(child, user_data, user_data2));
1816
1838
  if (ret < 0) return ret;
1817
1839
  }
1818
1840
  return 0;
@@ -1865,12 +1887,12 @@ function rc_walk_by_tree(iter, walk_type, cb_walking, user_data, user_data2) {
1865
1887
  child = iter[i - 1];
1866
1888
  ret = rc_walk_by_tree([...child.dl_children], walk_type, cb_walking, user_data, user_data2);
1867
1889
  if (ret < 0) return ret;
1868
- ret = cb_walking(child, user_data, user_data2);
1890
+ ret = walk_ret(cb_walking(child, user_data, user_data2));
1869
1891
  if (ret < 0) return ret;
1870
1892
  }
1871
1893
  else for (let i = 0; i < iter.length; i++) {
1872
1894
  child = iter[i];
1873
- ret = cb_walking(child, user_data, user_data2);
1895
+ ret = walk_ret(cb_walking(child, user_data, user_data2));
1874
1896
  if (ret < 0) return ret;
1875
1897
  else if (ret === 0) {
1876
1898
  ret = rc_walk_by_tree([...child.dl_children], walk_type, cb_walking, user_data, user_data2);
@@ -2994,6 +3016,19 @@ var __console_output_enabled__ = true;
2994
3016
  function set_console_log_enabled(enabled) {
2995
3017
  __console_output_enabled__ = enabled ? true : false;
2996
3018
  }
3019
+ var __console_log_filter__ = null;
3020
+ function set_console_log_filter(fn) {
3021
+ __console_log_filter__ = typeof fn === "function" ? fn : null;
3022
+ }
3023
+ function console_wants(level, msg) {
3024
+ if (!__console_output_enabled__) return false;
3025
+ if (!__console_log_filter__) return true;
3026
+ try {
3027
+ return __console_log_filter__(level, msg) ? true : false;
3028
+ } catch (e) {
3029
+ return true;
3030
+ }
3031
+ }
2997
3032
  var __in_log_callback__ = false;
2998
3033
  function emit_log_callback(level, msg, hora) {
2999
3034
  if (__log_callback__ && !__in_log_callback__) {
@@ -3041,9 +3076,9 @@ function log_error(format) {
3041
3076
  let hora = current_timestamp();
3042
3077
  if (f_error) {
3043
3078
  if (f_error === _console.error) {
3044
- if (__console_output_enabled__) f_error("%c" + hora + " ERROR: " + String(msg), "color:red");
3079
+ if (console_wants("error", msg)) f_error("%c" + hora + " ERROR: " + String(msg), "color:red");
3045
3080
  } else {
3046
- if (__console_output_enabled__) _console.error("%c" + hora + " ERROR: " + String(msg), "color:red");
3081
+ if (console_wants("error", msg)) _console.error("%c" + hora + " ERROR: " + String(msg), "color:red");
3047
3082
  f_error(`${hora} ERROR: ${String(msg)}`);
3048
3083
  }
3049
3084
  }
@@ -3054,9 +3089,9 @@ function log_warning(format) {
3054
3089
  let hora = current_timestamp();
3055
3090
  if (f_warning) {
3056
3091
  if (f_warning === _console.warn) {
3057
- if (__console_output_enabled__) f_warning("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3092
+ if (console_wants("warning", msg)) f_warning("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3058
3093
  } else {
3059
- if (__console_output_enabled__) _console.warn("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3094
+ if (console_wants("warning", msg)) _console.warn("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3060
3095
  f_warning(`${hora} WARNING: ${String(msg)}`);
3061
3096
  }
3062
3097
  }
@@ -3066,7 +3101,7 @@ function log_info(format) {
3066
3101
  let msg = format_log(format);
3067
3102
  let hora = current_timestamp();
3068
3103
  if (f_info) {
3069
- if (__console_output_enabled__) f_info("%c" + hora + " INFO: " + String(msg), "color:cyan");
3104
+ if (console_wants("info", msg)) f_info("%c" + hora + " INFO: " + String(msg), "color:cyan");
3070
3105
  }
3071
3106
  emit_log_callback("info", msg, hora);
3072
3107
  }
@@ -3074,7 +3109,7 @@ function log_debug(format) {
3074
3109
  let msg = format_log(format);
3075
3110
  let hora = current_timestamp();
3076
3111
  if (f_debug) {
3077
- if (__console_output_enabled__) f_debug("%c" + hora + " DEBUG: " + String(msg), "color:silver");
3112
+ if (console_wants("debug", msg)) f_debug("%c" + hora + " DEBUG: " + String(msg), "color:silver");
3078
3113
  }
3079
3114
  emit_log_callback("debug", msg, hora);
3080
3115
  }
@@ -3082,12 +3117,12 @@ function trace_msg(format) {
3082
3117
  let msg = format_log(format);
3083
3118
  let hora = current_timestamp();
3084
3119
  if (f_debug) {
3085
- if (__console_output_enabled__) f_debug("%c" + hora + " MSG: " + String(msg), "color:cyan");
3120
+ if (console_wants("msg", msg)) f_debug("%c" + hora + " MSG: " + String(msg), "color:cyan");
3086
3121
  }
3087
3122
  emit_log_callback("msg", msg, hora);
3088
3123
  }
3089
3124
  function trace_json(jn, msg) {
3090
- if (__console_output_enabled__) {
3125
+ if (console_wants("json", msg)) {
3091
3126
  if (msg) window.console.warn("=====> " + msg);
3092
3127
  window.console.dir(jn);
3093
3128
  }
@@ -6435,7 +6470,7 @@ function ac_on_message(gobj, event, kw, src) {
6435
6470
  let iev_kw = iev_msg.kw;
6436
6471
  if (gobj_current_state(gobj) !== "ST_SESSION") {
6437
6472
  if (gobj_has_event(gobj, iev_event, event_flag_t.EVF_PUBLIC_EVENT)) {
6438
- if (gobj_send_event(gobj, iev_event, iev_kw, gobj) === 0) return 0;
6473
+ if (!(gobj_send_event(gobj, iev_event, iev_kw, gobj) < 0)) return 0;
6439
6474
  } else log_error(`${gobj_short_name(gobj)}: event UNKNOWN in not-session state ${iev_event}`);
6440
6475
  close_websocket(gobj);
6441
6476
  return -1;
@@ -6839,6 +6874,7 @@ exports.safeSrc = safeSrc;
6839
6874
  exports.sdata_flag_t = sdata_flag_t;
6840
6875
  exports.send_http_json_post = send_http_json_post;
6841
6876
  exports.set_console_log_enabled = set_console_log_enabled;
6877
+ exports.set_console_log_filter = set_console_log_filter;
6842
6878
  exports.set_log_callback = set_log_callback;
6843
6879
  exports.set_remote_log_functions = set_remote_log_functions;
6844
6880
  exports.set_timeout = set_timeout;
@@ -6862,5 +6898,6 @@ exports.treedb_encoder_fkey = treedb_encoder_fkey;
6862
6898
  exports.treedb_get_field_desc = treedb_get_field_desc;
6863
6899
  exports.treedb_hook_data_size = treedb_hook_data_size;
6864
6900
  exports.vsprintf = vsprintf;
6901
+ exports.walk_type_t = walk_type_t;
6865
6902
 
6866
6903
  //# sourceMappingURL=gobj-js.cjs.js.map