File
Blob: samples/nodejs-compat-diagnosticschannel/worker.js
| 1 | import { |
| 2 | subscribe, |
| 3 | channel, |
| 4 | tracingChannel, |
| 5 | Channel, |
| 6 | } from "node:diagnostics_channel"; |
| 7 | import { doSomething } from "library"; |
| 8 | |
| 9 | import { AsyncLocalStorage } from "node:async_hooks"; |
| 10 | |
| 11 | // The library will send a diagnostic message when the doSomething |
| 12 | // method is called. We listen for that event here. When using |
| 13 | // Tail Workers, the event will also be included in the tail events. |
| 14 | subscribe('test', (message) => { |
| 15 | console.log('Diagnostic message received:', message); |
| 16 | }); |
| 17 | |
| 18 | const als1 = new AsyncLocalStorage(); |
| 19 | const als2 = new AsyncLocalStorage(); |
| 20 | const c = channel('bar'); |
| 21 | c.bindStore(als1); |
| 22 | c.bindStore(als2, (data) => {return { data }}); |
| 23 | |
| 24 | const tc = tracingChannel('foo'); |
| 25 | tc.start.bindStore(als1); |
| 26 | tc.start.subscribe(() => console.log('start', als1.getStore())); |
| 27 | tc.end.subscribe(() => console.log('end')); |
| 28 | tc.asyncStart.subscribe(() => console.log('async start')); |
| 29 | tc.asyncEnd.subscribe(() => console.log('async end')); |
| 30 | |
| 31 | export default { |
| 32 | async fetch(request) { |
| 33 | doSomething(); |
| 34 | |
| 35 | // prettier-ignore |
| 36 | console.log(c.runStores(1, (...args) => { |
| 37 | console.log(this, ...args, als1.getStore(), als2.getStore()); |
| 38 | return 1; |
| 39 | }, {}, 1, 2, 3)); |
| 40 | |
| 41 | await tc.tracePromise(async function() { |
| 42 | console.log('....', this); |
| 43 | }, {a:1}); |
| 44 | |
| 45 | return new Response("ok"); |
| 46 | } |
| 47 | }; |