nanoeffects 0.32.0 → 0.33.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.
package/llms-full.txt CHANGED
@@ -1321,7 +1321,7 @@ Each row is optional. The "when" column says what need brings you to it.
1321
1321
  | `nanoeffects/svelte` | `sveltePlugin`: adds `eventStore` to a bus (needs Svelte 5 or newer) | Svelte components |
1322
1322
  | `nanoeffects/vue` | `vuePlugin`: adds `watchEmitter` and `useEventRef` to a bus (needs Vue 3.3 or newer) | Vue components |
1323
1323
  | `nanoeffects/dev` | `devPlugin` (no-op in production), `getLostSignalsCount` and `getLostSignals` for tests | you look for leaks and lost signals in development and in tests |
1324
- | `nanoeffects/trace` | `createTracer`, the loggers (`graphLogger` finds event loops) and `tracePlugin` (names for the trace) | you want to see what ran and why |
1324
+ | `nanoeffects/trace` | `createTracer`, the loggers (`graphLogger` finds event loops; `mergeGraphs` and `findOrphans` find listeners no run reached) and `tracePlugin` (names for the trace) | you want to see what ran and why |
1325
1325
  | `nanoeffects/testing` | `createMarbleBus`, `fakeTimersClock`: a bus for tests, with emits and phases as diagrams | you test `latest`, `debounce`, `queue`, `retry` or `timeout` with fake timers |
1326
1326
  | `nanoeffects/guard` | `loopGuardPlugin`: refuses the emit that would pass a limit of a chain | a loop must not take the app down |
1327
1327
 
@@ -7852,10 +7852,13 @@ Tracing is optional. A bus works the same without it, and it costs almost nothin
7852
7852
  - [Use built-in loggers](#use-built-in-loggers)
7853
7853
  - [Cut volume in production](#cut-volume-in-production)
7854
7854
  - [Keep the last events for an incident](#keep-the-last-events-for-an-incident)
7855
+ - [Measure time and find open tasks](#measure-time-and-find-open-tasks)
7856
+ - [Find the cause of an error](#find-the-cause-of-an-error)
7855
7857
  - [Configure Redux DevTools](#configure-redux-devtools)
7856
7858
  - [Find loops with `graphLogger`](#find-loops-with-graphlogger)
7857
7859
  - [Send safe data to a tool](#send-safe-data-to-a-tool)
7858
7860
  - [Write your own logger](#write-your-own-logger)
7861
+ - [Stop on an event](#stop-on-an-event)
7859
7862
  - [Drop tracing from a build](#drop-tracing-from-a-build)
7860
7863
 
7861
7864
  ## Attach a tracer
@@ -8086,7 +8089,7 @@ console.log(describeBus(bus));
8086
8089
 
8087
8090
  **When.** You debug in development and want to jump from a log line to the code.
8088
8091
 
8089
- Pass `source: true` to `tracePlugin`. It reads the place of each `bus.on` and `bus.once` call once, when the listener is registered, and never on `emit`. The events of the listener get a `source` (`file:line:column`), `consoleLogger({ source: true })` prints it, and `describeBus(bus, { source: true })` lists it. An anonymous listener is reported too, under the type of its event.
8092
+ Pass `source: true` to `tracePlugin`. It reads the place of each `bus.on` and `bus.once` call once, when the listener is registered, and never on `emit`. The events of the listener get a `source` (`file:line:column`), `consoleLogger({ source: true })` prints it, and `describeBus(bus, { source: true })` lists it. An anonymous listener is reported too, under the type of its event and a number: `name/changed#1`, `name/changed#2`, in the order of registration. Two inline listeners on one event are two entries, and the place stays in `source`. Adding or reordering `bus.on` calls renumbers them, so give a `traceId` to a listener whose name must stay the same between builds.
8090
8093
 
8091
8094
  ```js
8092
8095
  import { createBus } from "nanoeffects";
@@ -8114,7 +8117,7 @@ console.log(describeBus(bus, { source: true }));
8114
8117
 
8115
8118
  ```
8116
8119
  [nanoeffects] saveName · run start (file:///app/src/user.js:13:5)
8117
- [nanoeffects] name/changed · run start (file:///app/src/user.js:14:5)
8120
+ [nanoeffects] name/changed#1 · run start (file:///app/src/user.js:14:5)
8118
8121
  { "name/changed": ["saveName (file:///app/src/user.js:13:5)", "<anonymous> (file:///app/src/user.js:14:5)"] }
8119
8122
  ```
8120
8123
 
@@ -8167,7 +8170,7 @@ const bus = createBus({
8167
8170
  | `consoleLogger()` | Prints one line per phase, indented by task depth |
8168
8171
  | `performanceLogger()` | User Timing measures for the browser Performance panel |
8169
8172
  | `createTraceRecorder()` | Keeps events in `recorder.events`: all of them for tests, or with `{ capacity }` the last N ([see below](#keep-the-last-events-for-an-incident)) |
8170
- | `graphLogger()` | Builds the map of which event leads to which, finds loops while the app runs, and writes the map as Mermaid, Graphviz or JSON ([see below](#find-loops-with-graphlogger)) |
8173
+ | `graphLogger()` | Builds the map of which event leads to which, finds loops while the app runs, and writes the map as Mermaid, Graphviz or JSON ([see below](#find-loops-with-graphlogger)); [`mergeGraphs` and `findOrphans`](debugging-loops.md#find-listeners-that-never-ran) join runs and find listeners that never ran |
8171
8174
 
8172
8175
  ### Cut volume in production
8173
8176
 
@@ -8227,12 +8230,91 @@ window.addEventListener("error", () => {
8227
8230
  What to know before you send the events:
8228
8231
 
8229
8232
  - **`payload` and `error` can hold private data**: what a user typed, tokens, personal text. The recorder keeps them as they are and does not redact. Choose what to send, as `withoutPayload` does.
8230
- - **There is no time on an event.** You get the order, not the duration. To add a time, wrap the recorder in a logger that stamps `performance.now()` on a copy of the event.
8233
+ - **There is no time on an event.** You get the order, not the duration. [`timingLogger`](#measure-time-and-find-open-tasks) measures time.
8231
8234
  - **An old `parentId` can point at an event that was pushed out.** When you build a tree from the events, treat an unknown `parentId` as a root.
8232
8235
  - **`recorder.events` is a snapshot with a `capacity`.** Each read builds a new array, so read it once at the moment you need it. Without a `capacity` it is one live array.
8233
8236
  - **A low `sample` thins the buffer.** `createTracer(recorder, { sample: 0.05 })` records 5% of root tasks, so the buffer may hold little when the incident comes. See [Cut volume in production](#cut-volume-in-production).
8234
8237
  - **A `capacity` that is not a positive integer** (`0`, a negative number, `NaN`) means no limit.
8235
8238
 
8239
+ ### Measure time and find open tasks
8240
+
8241
+ An event has no time, so the recorder cannot tell you which handler is slow, how long a request lived before `cancel`, or whether a test left something running. `timingLogger` stamps the time on each event when it gets it and pairs `start` with the first closing phase (`done`, `error`, `cancel` or `drop`) of the same task.
8242
+
8243
+ Pass it to the tracer next to the recorder: `createTracer` takes an array of loggers.
8244
+
8245
+ <!-- docs-check: new -->
8246
+
8247
+ ```js
8248
+ // bus.js
8249
+ import { createBus } from "nanoeffects";
8250
+ import { createTraceRecorder, createTracer, timingLogger } from "nanoeffects/trace";
8251
+
8252
+ // createTraceRecorder: events in the order they came
8253
+ export const recorder = createTraceRecorder();
8254
+
8255
+ // timingLogger: the time of each event and the duration of tasks
8256
+ export const timing = timingLogger();
8257
+
8258
+ // createTracer: both loggers get every event
8259
+ const trace = createTracer([recorder, timing]);
8260
+
8261
+ export const bus = createBus({ trace });
8262
+ ```
8263
+
8264
+ ```js
8265
+ // check.js
8266
+ import { timing } from "./bus";
8267
+
8268
+ // durations: the tasks that ended, with the closing phase and the time in ms
8269
+ const slow = timing.durations.filter((task) => task.ms > 500);
8270
+ console.log(slow);
8271
+
8272
+ // pending: the tasks that started and have not ended
8273
+ const leftovers = timing.pending();
8274
+ if (leftovers.length > 0) {
8275
+ throw new Error(`${leftovers.length} tasks are still open`);
8276
+ }
8277
+ ```
8278
+
8279
+ What to know:
8280
+
8281
+ - **The clock.** Time comes from `performance.now()`, or `Date.now()` where there is none. In a test pass your own clock: `timingLogger({ now })`.
8282
+ - **`capacity` cuts only `durations`.** `timingLogger({ capacity: 200 })` keeps the last 200 ended tasks. `pending()` is never cut: a leak shows up as a growing list.
8283
+ - **`clear()`** empties both lists. Call it between tests.
8284
+ - **The logger sees what the tracer lets through.** A task whose `start` was filtered out or not sampled gets no pair, and its ending is ignored.
8285
+
8286
+ ### Find the cause of an error
8287
+
8288
+ An error arrives as one event, and you cannot see what led to it. `chainOf` builds the path from the root to a task by `parentId`. Give it the recorder: then the listeners that send no event (a plain handler that calls `ctx.emit`) are in the path too.
8289
+
8290
+ ```js
8291
+ // why.js
8292
+ import { chainOf } from "nanoeffects/trace";
8293
+ import { recorder } from "./bus";
8294
+
8295
+ const failed = recorder.events.find((event) => event.phase === "error");
8296
+
8297
+ // chainOf: the path from the root to the failed task
8298
+ const path = chainOf(recorder, failed.id);
8299
+
8300
+ const text = path.map((step) => step.type).join(" → ");
8301
+ console.log(text); // queryChanged → searchLoaded
8302
+ ```
8303
+
8304
+ A step has `type`, and `effect` and `phase` (a listener with no events has neither), `source` and `traceGroupId` when known. If you pass a list of events (a snapshot, a file) instead of the recorder, the path holds only the tasks that have events.
8305
+
8306
+ To see the path right in the console, turn on `chain` in `consoleLogger`:
8307
+
8308
+ ```js
8309
+ import { consoleLogger, createTracer } from "nanoeffects/trace";
8310
+
8311
+ // consoleLogger: the line of an error ends with the path from the root
8312
+ const trace = createTracer(consoleLogger({ chain: true }));
8313
+ // [nanoeffects] render · run error (queryChanged → render)
8314
+ ```
8315
+
8316
+ `chain` remembers the last 1000 tasks. It is off by default.
8317
+
8236
8318
  ### Configure Redux DevTools
8237
8319
 
8238
8320
  <!-- docs-check: new -->
@@ -8364,6 +8446,26 @@ const errorsOnly = (): TraceLogger => (event) => {
8364
8446
  };
8365
8447
  ```
8366
8448
 
8449
+ ### Stop on an event
8450
+
8451
+ You want the debugger to stop when a certain event comes. No API is needed for that: it is five lines in a logger of your own.
8452
+
8453
+ ```js
8454
+ import { createTracer } from "nanoeffects/trace";
8455
+
8456
+ // A logger that stops in the debugger at the start of a task with this type
8457
+ const stopOn = (type) => (event) => {
8458
+ if (event.type === type && event.phase === "start") {
8459
+ debugger; // the browser stops here when DevTools is open
8460
+ }
8461
+ };
8462
+
8463
+ // createTracer: pass the logger like any other
8464
+ const trace = createTracer(stopOn("search"));
8465
+ ```
8466
+
8467
+ In the debugger read `event.payload` and walk up the stack: the synchronous part of the chain is all there.
8468
+
8367
8469
  ## Drop tracing from a build
8368
8470
 
8369
8471
  Without a tracer the hooks cost almost nothing. `nanoeffects/trace` is only bundled when imported.
@@ -8382,35 +8484,21 @@ conditions: ["notrace"]
8382
8484
  In a build from the sources, `define: { __NANOEFFECTS_TRACE__: "false" }` does the same. With the condition set, that build’s types have no `trace` option: `createBus({ trace })` is a type error. `nanoeffects/trace` has no `notrace` build. The saving is about 0.4 kB gzip of the whole library.
8383
8485
 
8384
8486
  ---
8385
- Source: docs/debugging-compared.md
8487
+ Source: docs/debugging-cases.md
8386
8488
 
8387
- # Debugging compared with RxJS, Effector and mitt
8489
+ # Debugging cases
8388
8490
 
8389
8491
  > **Level F.** Read first: [Trace tasks](tracing.md#attach-a-tracer).
8390
8492
 
8391
- Read this page if you know RxJS, Effector or mitt and want to know what nanoeffects gives you when something goes wrong, and what it does not. It compares what each tool shows, then answers three questions you ask in a debugger. It is not a migration guide: for that, see [RxJS](migrating-from-rxjs.md), [Effector](migrating-from-effector.md) and [mitt](migrating-from-mitt.md).
8493
+ Read this page when something goes wrong and you want to know who listens to an event, what began a chain, or why a request stopped. Each case below is a short snippet that you can run. For loops, see [Find and cut event loops](debugging-loops.md).
8392
8494
 
8393
8495
  ## On this page
8394
8496
 
8395
- - [Compare what you can see](#compare-what-you-can-see)
8396
8497
  - [Find who listens to an event](#find-who-listens-to-an-event)
8397
8498
  - [Find which task began a chain](#find-which-task-began-a-chain)
8398
8499
  - [Find out why a request stopped](#find-out-why-a-request-stopped)
8399
8500
  - [Know what is missing](#know-what-is-missing)
8400
8501
 
8401
- ## Compare what you can see
8402
-
8403
- | Question | nanoeffects | RxJS | Effector | mitt / nanoevents |
8404
- | -------- | ----------- | ---- | -------- | ----------------- |
8405
- | Which task started which? | Yes: every task has `parentId` and `rootId` ([Trace tasks](tracing.md)) | You add `tap` per stream, or a tool such as [rxjs-spy](https://github.com/cartant/rxjs-spy) | Yes, through [`effector-logger`](https://github.com/effector/logger) and the [inspector](https://github.com/effector/inspector) | No |
8406
- | Was a call cancelled or dropped? | Yes: the phases `cancel` and `drop` | An unsubscribe shows no event unless you add one | — | No |
8407
- | A log of every emit | `tracePlugin` reports each emit, also with no listener | `tap` per stream | A logger over the units | A `"*"` listener |
8408
- | A map of the app | `describeBus` before a run, `graphLogger` after ([Find and cut event loops](debugging-loops.md)) | Not built in | The graph of units, with names from the Babel/SWC plugin | `emitter.all` holds the handlers of each event (mitt) |
8409
- | Find a loop | `graphLogger`, then `loopGuardPlugin` cuts it | — | — | No |
8410
- | Test a flow | [`createMarbleBus`](testing.md): emits and phases as diagrams on fake timers; [`createTraceRecorder`](tracing.md#keep-the-last-events-for-an-incident) keeps the events | [Marble tests](https://rxjs.dev/guide/testing/marble-testing) | `fork` gives an isolated scope | You wire it yourself |
8411
-
8412
- A dash means that this page does not compare it. Read the table as a map of where to look, not as a score. Effector and RxJS also hold state and streams, which nanoeffects does not, so their tools cover more ground. nanoeffects covers one thing in depth: why an async handler ran, and why it stopped.
8413
-
8414
8502
  ## Find who listens to an event
8415
8503
 
8416
8504
  **Problem.** You read an emit and want to know what it starts.
@@ -8515,7 +8603,7 @@ Details: [Trace tasks](tracing.md) and [Cancel stale requests](cancel-stale-requ
8515
8603
 
8516
8604
  ## Know what is missing
8517
8605
 
8518
- - **No browser extension or UI of its own.** Use [Redux DevTools](tracing.md#configure-redux-devtools), the console or the Performance panel. Effector has an inspector for this.
8606
+ - **No browser extension or UI of its own.** Use [Redux DevTools](tracing.md#configure-redux-devtools), the console or the Performance panel.
8519
8607
  - **No map from the source code.** `describeBus` lists the listeners registered so far, and `graphLogger` shows the events that happened. A listener that is not registered yet is not on the map.
8520
8608
  - **No click-through from the log to the code.** With `tracePlugin({ source: true })` an event carries the `file:line:column` of its listener ([Find the line of a listener](tracing.md#find-the-line-of-a-listener)), but a link in a UI is yours to build.
8521
8609
  - **No names from a compiler.** Function names give a name for free; a minifier can shorten them, so use `traceId` where a name must stay stable.
@@ -8542,6 +8630,7 @@ A loop is two handlers that emit each other's events, or one handler that emits
8542
8630
  - [Cut a loop at run time](#cut-a-loop-at-run-time)
8543
8631
  - [Fail a test on a loop](#fail-a-test-on-a-loop)
8544
8632
  - [Export the graph and the payloads](#export-the-graph-and-the-payloads)
8633
+ - [Find listeners that never ran](#find-listeners-that-never-ran)
8545
8634
  - [Know how a loop is found](#know-how-a-loop-is-found)
8546
8635
  - [Know the limits](#know-the-limits)
8547
8636
 
@@ -8804,6 +8893,84 @@ console.log(JSON.stringify(data));
8804
8893
 
8805
8894
  A payload can be a class instance, a `Map`, a circular object or an `Error`, which a plain `JSON.stringify` cannot take. The exports make them plain JSON: an `Error` keeps its name, message, stack and cause, a cycle becomes `"[Circular]"`, a `Map` becomes `{ "$map": [...] }`, and an instance of a class keeps its own fields and its name as `$class`. Getters are never called, and a value that is too deep or too big is cut.
8806
8895
 
8896
+ ## Find listeners that never ran
8897
+
8898
+ A graph of one run shows what that run did. It cannot tell you which listeners the app has but no run ever reached: dead code, or a scenario nobody tests. To find them, join the graphs of several runs, then compare the result with the listeners the bus holds.
8899
+
8900
+ Prerequisites: the bus uses `tracePlugin` and `graphLogger` (see [Find a loop](#find-a-loop)), and each listener has a function name or a `traceId`. An anonymous listener has no node in the graph, so it is never reported.
8901
+
8902
+ 1. Each run writes its graph to a file.
8903
+ 2. A script reads the files and joins them with `mergeGraphs`.
8904
+ 3. `findOrphans` compares the joined graph with `describeBus`.
8905
+
8906
+ ```js
8907
+ // bus.js
8908
+ import { createBus } from "nanoeffects";
8909
+ import { createTracer, graphLogger, tracePlugin } from "nanoeffects/trace";
8910
+
8911
+ // graphLogger — the map of what ran
8912
+ export const graph = graphLogger({ name: "cart" });
8913
+ export const bus = createBus({ trace: createTracer(graph) }).use(tracePlugin());
8914
+ ```
8915
+
8916
+ ```js
8917
+ // handlers.js
8918
+ import { bus } from "./bus";
8919
+
8920
+ // the listener is named by its function; findOrphans matches this name
8921
+ bus.on("cart/added", function recalcTotal() {});
8922
+ bus.on("cart/cleared", function resetTotal() {});
8923
+ ```
8924
+
8925
+ ```js
8926
+ // graph-setup.js (the setup file of your test runner)
8927
+ import { mkdirSync, writeFileSync } from "node:fs";
8928
+ import { afterAll } from "vitest";
8929
+ import { graph } from "./bus";
8930
+
8931
+ afterAll(() => {
8932
+ mkdirSync("graphs", { recursive: true });
8933
+
8934
+ // toJSON — the graph of this run as plain data
8935
+ const data = graph.toJSON();
8936
+ writeFileSync(`graphs/${process.pid}.json`, JSON.stringify(data));
8937
+ });
8938
+ ```
8939
+
8940
+ ```js
8941
+ // orphans.js (run it after the tests)
8942
+ import { readdirSync, readFileSync } from "node:fs";
8943
+ import { describeBus, findOrphans, mergeGraphs } from "nanoeffects/trace";
8944
+ import { bus } from "./bus";
8945
+ import "./handlers";
8946
+
8947
+ const files = readdirSync("graphs");
8948
+ const runs = files.map((file) => JSON.parse(readFileSync(`graphs/${file}`, "utf8")));
8949
+
8950
+ // mergeGraphs — counts add up, so a listener that ran in any run is a node
8951
+ const all = mergeGraphs(...runs);
8952
+
8953
+ // describeBus — the listeners the bus holds, by event
8954
+ const described = describeBus(bus);
8955
+
8956
+ // findOrphans — the listeners the joined graph never saw run
8957
+ console.log(findOrphans(described, all));
8958
+ // [{ event: "cart/cleared", listener: "resetTotal", kind: "unemitted" }]
8959
+ ```
8960
+
8961
+ Each item has a `kind`:
8962
+
8963
+ | `kind` | Meaning | What to check |
8964
+ | ------ | ------- | ------------- |
8965
+ | `unemitted` | Nothing emitted the event in any run. | Is the event still used? Is a scenario missing? |
8966
+ | `unrun` | The event was emitted, this listener did not run. | A condition or a [policy](policies.md) that never lets it run. |
8967
+
8968
+ Read the list as a hint, not as a failure. With few runs it holds many false items, and it only becomes a signal when the runs cover the main scenarios. Know the limits:
8969
+
8970
+ - Two listeners with one name are one node, so the second one is never reported. Give each a distinct name or `traceId`.
8971
+ - Merge runs of one build. A listener that was renamed shows up as an orphan.
8972
+ - `mergeGraphs` does not find a loop that only closes when the edges of two runs are joined. Use `graph.cycles()` in each run.
8973
+
8807
8974
  ## Know how a loop is found
8808
8975
 
8809
8976
  `graphLogger` finds loops in two ways. They give two kinds of finding in `graph.cycles()`, told apart by `source`:
package/llms.txt CHANGED
@@ -61,9 +61,9 @@ Rules that generated code often gets wrong:
61
61
  - [Semantics](docs/semantics.md): exact rules of emit, abort and ordering
62
62
  - [Keep the signal through a handler](docs/pass-the-signal.md): every way a policy handler loses `ctx.signal`, and the lint rules and `devPlugin` check that find it
63
63
  - [Test time with diagrams](docs/testing.md): `createMarbleBus` and `fakeTimersClock`; emits and phases as diagrams on fake timers
64
- - [Trace tasks](docs/tracing.md): loggers, Redux DevTools, `graphLogger`, the `notrace` build
65
- - [Debugging compared with RxJS, Effector and mitt](docs/debugging-compared.md): what you can see in each, and three debugging cases
66
- - [Find and cut event loops](docs/debugging-loops.md): symptoms, `graphLogger`, the graph as Mermaid, Graphviz or JSON, Redux DevTools
64
+ - [Trace tasks](docs/tracing.md): loggers, `timingLogger` for durations and open tasks, `chainOf` for the cause of an error, Redux DevTools, `graphLogger`, the `notrace` build
65
+ - [Debugging cases](docs/debugging-cases.md): who listens to an event, what began a chain, why a request stopped
66
+ - [Find and cut event loops](docs/debugging-loops.md): symptoms, `graphLogger`, the graph as Mermaid, Graphviz or JSON, Redux DevTools, `mergeGraphs` and `findOrphans` for listeners no run reached
67
67
  - [Cut a loop with loopGuardPlugin](docs/loop-guard.md): limits on repeats, depth and emits from outside
68
68
  - [Turn events into Observables](docs/observable.md): `createObserve`, `Subject`, `events` (an event as a `for await` loop)
69
69
  - [Hot reload and development diagnostics](docs/hmr.md): `devPlugin`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoeffects",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "description": "Business logic and side effects outside UI components, as plain async functions. Tiny, no dependencies.",
5
5
  "keywords": [
6
6
  "event-bus",
@@ -1,2 +0,0 @@
1
- import{i as e,n as t,r as n}from"./caller-DULg_ZYV.js";import{n as r,t as i}from"./onAbort-BgMfspK4.js";import{a,c as o,n as s,o as c,r as l,s as u}from"./context-Dp_QRXzb.js";import{n as d}from"./promise-CSLpzwSR.js";import{t as f}from"./origin-key-BbWN3gmN.js";import{t as p}from"./scope-key-B7l7mUS5.js";const m=/* @__PURE__ */ new WeakMap,h=e=>{let t=m.get(e);if(t)return t;let n=/* @__PURE__ */ new Map;return m.set(e,n),n},g=e=>m.get(e),_=(e,t,n)=>{let r=e.get(t)??[];r.push(n),e.set(t,r)},v=(e,t,n)=>{let r=e.get(t),i=r?.indexOf(n)??-1;!r||i<0||(r.splice(i,1),r.length===0&&e.delete(t))},y=(e,t,n)=>(e.get(t)??[]).findLast(e=>e.handler===n);let b;const x=()=>b,S=(e,t)=>{let n=b;b=e;try{return t()}finally{b=n}},C=(e,t,n={})=>{let{trace:r}=e;if(!r){e.emit(t);return}let{traceId:i,traceGroupId:a}=n,{ctx:o,unlink:c}=s({event:{type:i??t.type,payload:t,...a===void 0?{}:{traceGroupId:a}},bus:{emit:e.emit,on:e.on,off:e.off,ctx:e.ctx,addContext:e.addContext},tracer:r});u(o,`run`,`start`);try{o.emit(t),u(o,`run`,`done`)}catch(e){throw u(o,`run`,`error`,e),e}finally{c()}},w=e=>/^(.+):(\d+):(\d+)$/.exec(e)?e:void 0,T=e=>{let t=e.slice(3).replace(/^async /,``),n=t.lastIndexOf(` (`),r=n<0?t:t.slice(n+2).replace(/\)$/,``);return w(r)},E=e=>w(e.slice(e.indexOf(`@`)+1)),D=e=>{if(typeof e!=`string`)return[];let t=[];for(let n of e.split(`
2
- `)){let e=n.trim();e.startsWith(`at `)?t.push(T(e)):e.includes(`@`)&&t.push(E(e))}return t},O=(e,t)=>D(e)[t],k=e=>typeof e==`string`?e:Reflect.get(Object(e),`type`),A=(e,t)=>typeof e==`object`?Reflect.get(Object(e),`payload`):t[0],j=(r={})=>i=>{if(!(o&&i.trace))return i;let{trace:s}=i,u=(e,t)=>{let n=s.task({type:k(e),payload:A(e,t),leaf:!0},l());s.report(n,`emit`,`done`)},d=i.emit;i.emit=(e,...t)=>S(e,()=>(u(e,t),d(e,...t))),Reflect.set(i,f,(e,t)=>{C(i,e,t)});let p=h(i),m=(e,t,n)=>(r,i)=>(a(i,t,n),c(i,`run`,()=>e(r,i))),g=(e,t)=>{let n=Reflect.get(Object(t),`traceId`);return typeof n==`string`?n:e.name||void 0},b=r.source===!0;b&&n();let x=(e,t)=>e!==void 0||!b?e:typeof t==`string`?t:void 0,w=(e,t,n)=>{let r=x(e.name,t),i=typeof r==`string`?m(e.handler,r,e.source):e.handler;return n?(n,r)=>(v(p,t,e),i(n,r)):i},T=(n,r)=>{let i=(e,i,a)=>{let o=k(e),s=b?O(t(),2):void 0,c={handler:i,wrapper:i,name:g(i,a),source:s};c.wrapper=w(c,o,r),_(p,o,c);let l=n(e,c.wrapper,a);return()=>{v(p,o,c),l()}};return(t,n,r)=>e(()=>i(t,n,r))},E=i.off,D=(e,t)=>{if(e===void 0)p.clear();else if(t===void 0)p.delete(k(e));else{let n=y(p,k(e),t);n&&v(p,k(e),n),E(e,n?.wrapper??t);return}E(e,t)},j=i.on,M=i.once;return i.on=T(j,!1),i.once=T(M,!0),i.off=D,i},M=e=>{let t=typeof e==`function`?[e]:[...e??[]];return t.length===0?null:e=>{for(let n of t)try{n(e)}catch(e){r(`trace logger failed`,e)}}},N=()=>{},P=e=>{if(typeof e.traceGroupId==`string`)return e.traceGroupId;let t=x();if(typeof t!=`object`&&typeof t!=`function`||t===null)return;let n=Reflect.get(t,p),r=typeof n==`object`&&n?Reflect.get(n,`traceGroupId`):void 0;return typeof r==`string`?r:void 0},F=(e,t={})=>{let n=e,a=M(n),{filter:o,sample:s=1}=t,c=0,u=(Array.isArray(n)?n:[n]).flatMap(e=>e.task?[e.task]:[]),f=e=>{let t={ref:e,id:e.id,type:e.type};e.parentRef&&(t.parent=e.parentRef),e.event!==void 0&&(t.event=e.event),e.leaf&&(t.leaf=!0),t.emitted=e.emitted,e.group!==void 0&&(t.traceGroupId=e.group),e.source!==void 0&&(t.source=e.source);for(let e of u)try{e(t)}catch(e){r(`trace logger failed`,e)}},p=e=>typeof s==`function`?s({type:e.type,payload:e.payload}):Math.random()<s,m={task:(e,t)=>{let n={id:++c,root:t?.root??c,parent:t?.id,parentRef:u.length>0?t:void 0,leaf:e.leaf,emitted:l()===t,type:e.type,payload:e.payload,sampled:t?.sampled??p(e),group:P(e)??t?.group};return u.length>0&&n.sampled&&f(n),n},rename:(e,t,n)=>{e.event??=e.type,e.type=t,n!==void 0&&(e.source=n),u.length>0&&e.sampled&&f(e)},report:(e,t,n,r)=>{if(!e.sampled)return;let i={id:e.id,type:e.type,effect:t,phase:n,payload:e.payload};typeof e.parent==`number`&&(i.parentId=e.parent),e.root!==e.id&&(i.rootId=e.root),e.group!==void 0&&(i.traceGroupId=e.group),e.source!==void 0&&(i.source=e.source),n===`error`&&(i.error=r),(!o||o(i))&&a?.(i)},settler:(e,t)=>{if(!e.sampled)return N;let n=!1;return(r,i)=>{n||(n=!0,m.report(e,t,r,i))}},run:(e,t,n,r)=>{if(!e.sampled)return r();let a=m.settler(e,t);m.report(e,t,`start`),i({signal:n},()=>a(`cancel`));let o;try{o=r()}catch(e){throw a(`error`,e),e}return d(o)?Promise.resolve(o).then(e=>(a(`done`),e),e=>{throw a(`error`,e),e}):(a(`done`),o)}};return m};export{j as n,g as r,F as t};