dcp-client 4.3.8 → 4.4.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.
@@ -17,8 +17,7 @@ self.wrapScriptLoading({ scriptName: 'bootstrap', finalScript: true }, function
17
17
  postMessageSentTime = 0,
18
18
  throttledProgress = 0, // how many progress events were throttled since last update
19
19
  indeterminateProgress = true, // If there hasn't been a determinate call to progress since last update
20
- flushedLastConsoleMessage = false, // flag used to determine if flushedLastLog() was called by client
21
- lastConsoleMessage = null; // cache of the last message received throguh a console event
20
+ lastConsoleLog = null; // cache of the last message received through a console event
22
21
 
23
22
  addEventListener('message', async (event) => {
24
23
  try {
@@ -49,7 +48,7 @@ self.wrapScriptLoading({ scriptName: 'bootstrap', finalScript: true }, function
49
48
  postMessageSentTime = 0;
50
49
  throttledProgress = 0;
51
50
  indeterminateProgress = true;
52
- flushedLastConsoleMessage = false;
51
+ lastConsoleLog = null;
53
52
  ring2PostMessage({ request: 'resetStateDone' });
54
53
  }
55
54
  } catch (error) {
@@ -127,7 +126,7 @@ self.wrapScriptLoading({ scriptName: 'bootstrap', finalScript: true }, function
127
126
  throttledProgress++;
128
127
  }
129
128
 
130
- flushConsoleMessages(null);
129
+ protectedStorage.dispatchSameConsoleMessage();
131
130
  return true;
132
131
  }
133
132
 
@@ -163,13 +162,60 @@ self.wrapScriptLoading({ scriptName: 'bootstrap', finalScript: true }, function
163
162
  reject: workerBootstrap$work$reject,
164
163
  };
165
164
 
166
- function workerBootstrap$console(level, ...args) {
167
- flushConsoleMessages({
168
- level,
169
- message: args,
170
- fileName: undefined,
171
- lineNumber: undefined});
165
+ /**
166
+ * Polyfills the `console[method]` functions in the work function by
167
+ * dispatching 'console' events to the worker to be propogated/emitted to
168
+ * connected clients.
169
+ *
170
+ * Subsequent console messages that are identical are treated as special
171
+ * cases. Their dispatch is delayed for as long as possible. See
172
+ * `protectedStorage.dispatchSameConsoleMessage()` for the events triggering
173
+ * their dispatch.
174
+ */
175
+ function workerBootstrap$console(level, ...args)
176
+ {
177
+ const newConsoleLog = { level, message: args };
178
+
179
+ // The first console message.
180
+ if (lastConsoleLog === null)
181
+ {
182
+ dispatchNewConsoleMessage();
183
+ return;
184
+ }
185
+
186
+ // Subsequent console messages.
187
+ if (
188
+ newConsoleLog.level === lastConsoleLog.level
189
+ && areArraysEqual(newConsoleLog.message, lastConsoleLog.message)
190
+ )
191
+ {
192
+ // Delay/batch the dispatch of the same log(s).
193
+ lastConsoleLog.same += 1;
194
+ return;
195
+ }
196
+
197
+ protectedStorage.dispatchSameConsoleMessage();
198
+ dispatchNewConsoleMessage();
199
+
200
+ function dispatchNewConsoleMessage()
201
+ {
202
+ newConsoleLog.same = 1;
203
+ postMessage({ request: 'console', payload: newConsoleLog });
204
+ lastConsoleLog = newConsoleLog;
205
+ }
206
+
207
+ // Checks to see whether 2 arrays are identical.
208
+ function areArraysEqual(array1, array2)
209
+ {
210
+ if (array1.length !== array2.length)
211
+ return false;
212
+ for (let k = 0; k < array1.length; k++)
213
+ if (array1[k] !== array2[k])
214
+ return false;
215
+ return true;
216
+ }
172
217
  }
218
+
173
219
  // Polyfill console with our own function. Prevents console statements
174
220
  // within a user's work function from being displayed in a worker's console, and
175
221
  // will properly send it back to the user
@@ -181,33 +227,20 @@ self.wrapScriptLoading({ scriptName: 'bootstrap', finalScript: true }, function
181
227
  error: workerBootstrap$console.bind(null, 'error'),
182
228
  };
183
229
 
184
- // Function caches the most recent console message and counts how many identical messages are received
185
- // Once a different message is received (or when the slice completes) it is sent along with the counter value
186
- function flushConsoleMessages(data){
187
- if(lastConsoleMessage != null && data != null && lastConsoleMessage.message == data.message && lastConsoleMessage.level == data.level){
188
- lastConsoleMessage.same++;
189
- } else {
190
- if(lastConsoleMessage != null){
191
- postMessage({
192
- request: 'console',
193
- payload: lastConsoleMessage
194
- });
195
- lastConsoleMessage = null;
196
- }
197
-
198
- if(data != null){
199
- data.same = 1;
200
- lastConsoleMessage = data;
201
- }
202
- }
230
+ /**
231
+ * Dispatches the most recent duplicate console message.
232
+ *
233
+ * Based on the spec, this occurs when a new different message is logged (see
234
+ * `workerBootstrap$console`), the worker terminates (hence
235
+ * `protectedStorage`), or a progress update event is emitted (see
236
+ * `self.progress`); whichever comes first.
237
+ */
238
+ protectedStorage.dispatchSameConsoleMessage = function workerBootstrap$dispatchSameConsoleMessage() {
239
+ if (!(lastConsoleLog?.same > 1))
240
+ return;
241
+ // Avoid sending duplicate console message data over the network.
242
+ delete lastConsoleLog.message;
243
+ postMessage({ request: 'console', payload: lastConsoleLog });
244
+ lastConsoleLog = null;
203
245
  };
204
- // Ensure all console statements will be sent after a job completes
205
- protectedStorage.flushLastLog = function workerBootstrap$flushLastLog(){
206
- if(!flushedLastConsoleMessage){
207
- flushConsoleMessages(null);
208
- flushedLastConsoleMessage = true;
209
- } else{
210
- throw new Error('client should not be calling flushLastLog');
211
- }
212
- }
213
246
  });
@@ -414,11 +414,11 @@ prepPyodide`);
414
414
  rejection = error;
415
415
  }
416
416
 
417
- /* try to flush any pending tasks on the microtask queue, then flush any pending console events,
418
- * especially in the case of a repeating message that hasn't been emitted yet
417
+ /* try to flush any pending tasks on the microtask queue, then flush any
418
+ * repeating message that hasn't been dispatched yet.
419
419
  */
420
420
  try { await tryFlushMicroTaskQueue(); } catch(e) {};
421
- try { protectedStorage.flushLastLog(); } catch(e) {};
421
+ protectedStorage.dispatchSameConsoleMessage();
422
422
  try
423
423
  {
424
424
  protectedStorage.lockTimers(); // lock timers so no new timeouts will be run.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dcp-client",
3
- "version": "4.3.8",
3
+ "version": "4.4.0",
4
4
  "description": "Core libraries for accessing DCP network",
5
5
  "keywords": [
6
6
  "dcp"