@akc42/app-utils 3.1.3 → 3.2.1

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
@@ -134,13 +134,9 @@ The purpose of this module is to provide a debugable capability which can be
134
134
  config-promise) is set to a string which is a comma separated list of topics
135
135
  and that list has the topic for this debug call in it.
136
136
 
137
- **Note**: It is normally expected for the server to provide a mechanism to
138
- update the config before it is returned and for the client to be restarted to
139
- enable the appropriate debug topics. An alternative could be for a client side
140
- function to use the `mockConfig` call to replace the promise with one which
141
- contained a different list of topics. `debug` (the function returned by the
142
- call to `Debug('topic')`) checks the list of topics on every call so would
143
- dynamically pick up the changes
137
+ **Note**: the debug function checks with the sessionStorage.getItem('debug) (via an await for the Config Promise)
138
+ to see if the topic is enabled (assumes the result is a ':' separated string of topics). This allows the server to
139
+ change what topics are available via the config api call.
144
140
 
145
141
  # dom-host
146
142
 
package/debug.js CHANGED
@@ -53,10 +53,11 @@
53
53
  would dynamically pick up the changes
54
54
 
55
55
  */
56
+ import config from './config-promise.js';
56
57
 
57
58
  const topicMap = new Map();
58
59
 
59
- import api from './post-api.js';
60
+
60
61
 
61
62
  function Debug (t) {
62
63
  if (typeof t !== 'string' || t.length === 0 || !/^[a-zA-Z]+$/.test(t)) {
@@ -64,7 +65,6 @@ function Debug (t) {
64
65
  throw new Error('Invalid Debug Topic');
65
66
  }
66
67
  const tl = t.toLowerCase();
67
- let timestamp = new Date().getTime();
68
68
  if (topicMap.has(tl) ) {
69
69
  const topic = topicMap.get(tl);
70
70
  return topic.debug;
@@ -73,26 +73,34 @@ function Debug (t) {
73
73
  const topicHandler = {
74
74
  topic: tl,
75
75
  timestamp: new Date().getTime(),
76
- debug: function (...args) {
77
- api(`debugconf/${this.topic}`).then(found => {
78
- if (found) {
79
- const message = args.reduce((cum, arg) => {
80
- return `${cum} ${arg}`.trim();
81
- }, '');
82
- const now = new Date().getTime();
83
- const gap = now - this.timestamp;
84
- this.timestamp = now;
85
- console.log(`+${gap}ms`, this.topic, message);
86
- const blob = new Blob([JSON.stringify({
87
- message: message,
88
- gap: gap
89
- })], { type: 'application/json' })
90
-
91
- navigator.sendBeacon(`/api/debuglog/${this.topic}`, blob);
76
+ defined: false, //has the config been defined yet
77
+ enabled: false, //is this topic enabled
78
+ debug: async function (...args) {
79
+ //do time calc before potential delay to see if we are enabled
80
+ const now = new Date().getTime();
81
+ const gap = now - this.timestamp;
82
+ this.timestamp = now;
83
+ if (!this.defined) {
84
+ await config();
85
+ this.defined = true;
86
+ const debugConf = sessionStorage.getItem('debug');
87
+ if (debugConf) {
88
+ const topics = debugConf.split(':');
89
+ if (topics.includes(this.topic)) this.enabled = true;
92
90
  }
93
-
94
- });
95
-
91
+ }
92
+ if (this.enabled) {
93
+ const message = args.reduce((cum, arg) => {
94
+ return `${cum} ${arg}`.trim();
95
+ }, '');
96
+ console.log(`+${gap}ms`, this.topic, message);
97
+ const blob = new Blob([JSON.stringify({
98
+ message: message,
99
+ gap: gap
100
+ })], { type: 'application/json' })
101
+
102
+ navigator.sendBeacon(`/api/debuglog/${this.topic}`, blob);
103
+ }
96
104
  }
97
105
  }
98
106
  topicHandler.debug = topicHandler.debug.bind(topicHandler);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akc42/app-utils",
3
- "version": "3.1.3",
3
+ "version": "3.2.1",
4
4
  "description": "General Utilities for SPAs",
5
5
  "exports": {
6
6
  ".": "./*.js"
package/post-api.js CHANGED
@@ -47,8 +47,10 @@ export default async function api(url, params, blob, signal) {
47
47
  return {};
48
48
  }
49
49
  } catch (err) {
50
- if (err.type === 'api-error') throw err; //just throw whatever error we had
51
- //we failed to parse the json - the actual code should be in the text near the end;
52
- throw new CustomEvent('api-error', { composed: true, bubbles: true, detail: parseInt((text?? '---502---').substr(-6, 3), 10) });
50
+ if (!options.signal || !options.signal.aborted) {
51
+ if (err.type === 'api-error') throw err; //just throw whatever error we had
52
+ //we failed to parse the json - the actual code should be in the text near the end;
53
+ throw new CustomEvent('api-error', { composed: true, bubbles: true, detail: parseInt((text?? '---502---').slice(-6, -3), 10) });
54
+ }
53
55
  }
54
56
  }