@timber-js/app 0.2.0-alpha.229 → 0.2.0-alpha.230

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.
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * const rows = await db.select().from(users);
8
8
  * // becomes
9
- * const rows = await __timber_await$(async () => (db.select().from(users)),
9
+ * const rows = await __timber_await$(() => (db.select().from(users)),
10
10
  * "db.select().from(users)", "app/users/page.tsx:12");
11
11
  *
12
12
  * `__timber_await$` is declared once per module and looks the wrapper up on
@@ -15,6 +15,11 @@
15
15
  * global rather than an injected import, so the rewrite needs no resolvable
16
16
  * specifier from a workspace package outside the app.
17
17
  *
18
+ * Vite 8 compiles TS/JSX before any plugin transform runs (`enforce: 'pre'`
19
+ * included), so the module this sees has moved lines and reprinted text.
20
+ * Labels and locations are mapped back through the module's combined source
21
+ * map (`originalFromMap`), so they are the code and lines as written.
22
+ *
18
23
  * Scope: the RSC environment, `vite dev` only; every module outside
19
24
  * node_modules and outside timber itself, so a monorepo's `packages/db`
20
25
  * (where the query actually is) is covered. Skipped: `'use client'`
@@ -28,13 +33,49 @@
28
33
  import MagicString from 'magic-string';
29
34
  import { type Plugin } from 'vite';
30
35
  import type { PluginContext } from '../plugin-context.ts';
36
+ /** Parser dialect for a module path: the transform sees original source. */
37
+ export declare function langFor(file: string): 'ts' | 'tsx' | 'jsx';
38
+ /**
39
+ * The module as the developer wrote it, for labels and line numbers.
40
+ *
41
+ * Vite 8 compiles TS/JSX before any plugin's `transform` runs, `enforce:
42
+ * 'pre'` included, so the code the transform sees has moved lines and
43
+ * reprinted text (a real report: fetcher.ts:12 shown as :11, :13 as :16,
44
+ * single quotes printed as double). The plugin maps positions back through
45
+ * the module's combined source map.
46
+ */
47
+ export interface OriginalSource {
48
+ code: string;
49
+ lang: 'ts' | 'tsx' | 'jsx';
50
+ /** Offset in `code` for an offset in the transformed module, or null if unmapped. */
51
+ offsetOf(generatedOffset: number): number | null;
52
+ }
31
53
  /**
32
54
  * Rewrite the awaits in one module. Returns null when nothing changed.
33
55
  * Exported for tests; `relPath` is the location prefix shown in the pill.
56
+ * With `original`, labels and lines come from the source as written.
34
57
  */
35
- export declare function instrumentAwaits(code: string, relPath: string): {
58
+ export declare function instrumentAwaits(code: string, relPath: string, options?: {
59
+ lang?: 'ts' | 'tsx' | 'jsx';
60
+ original?: () => OriginalSource | null;
61
+ }): {
36
62
  code: string;
37
63
  map: ReturnType<MagicString['generateMap']>;
38
64
  } | null;
65
+ /** The slice of a Rollup/Vite combined source map this reads. */
66
+ interface CombinedMap {
67
+ version: number;
68
+ file?: string | null;
69
+ sources: (string | null)[];
70
+ sourcesContent?: (string | null)[] | null;
71
+ names: string[];
72
+ mappings: string;
73
+ }
74
+ /**
75
+ * Map the transformed module back to its original text through its combined
76
+ * source map, using Node's built-in `module.SourceMap`.
77
+ */
78
+ export declare function originalFromMap(map: CombinedMap, file: string, generated: string): OriginalSource | null;
39
79
  export declare function timberDevAwait(ctx: PluginContext): Plugin;
80
+ export {};
40
81
  //# sourceMappingURL=dev-await.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dev-await.d.ts","sourceRoot":"","sources":["../../src/plugins/dev-await.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAKH,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,EAAY,KAAK,MAAM,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAuI1D;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,GACd;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,UAAU,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC,CAAA;CAAE,GAAG,IAAI,CAiEtE;AAaD,wBAAgB,cAAc,CAAC,GAAG,EAAE,aAAa,GAAG,MA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E,MAAM,EAAE,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,GAChB,cAAc,GAAG,IAAI,CAkCvB;AAaD,wBAAgB,cAAc,CAAC,GAAG,EAAE,aAAa,GAAG,MAAM,CAkBzD"}
@@ -50,7 +50,9 @@ export declare const DEV_SPAN_ATTRIBUTES: readonly ['http.request.method', 'http
50
50
  /** The awaited expression's source text, whitespace collapsed, truncated. */
51
51
  'timber.await_label',
52
52
  /** `path/relative/to/root.ts:line` of the await. */
53
- 'timber.await_location'];
53
+ 'timber.await_location',
54
+ /** The awaited promise was already awaited elsewhere: a wait on shared work, not new work. */
55
+ 'timber.await_shared'];
54
56
  export type DevSpanAttribute = (typeof DEV_SPAN_ATTRIBUTES)[number];
55
57
  /** Attributes that hold a URL; sent without query or fragment. */
56
58
  export declare const DEV_SPAN_URL_ATTRIBUTES: ReadonlySet<DevSpanAttribute>;
@@ -1 +1 @@
1
- {"version":3,"file":"dev-span.d.ts","sourceRoot":"","sources":["../../src/shared/dev-span.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,+DAA+D;AAC/D,eAAO,MAAM,cAAc,gBAAgB,CAAC;AAE5C,uFAAuF;AACvF,eAAO,MAAM,qBAAqB,uBAAuB,CAAC;AAE1D;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,eAAqC,CAAC;AAEpE,0EAA0E;AAC1E,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAEhD;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,eAAiC,CAAC;AAE5D,yDAAyD;AACzD,eAAO,MAAM,eAAe,iBAAiB,CAAC;AAE9C;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAEnC,2CAA2C;AAC3C,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAEpD;;;;;;;GAO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B,CAGhE,CAAC;AAEH,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;AAEzD,oDAAoD;AACpD,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,gCAAgC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,0BAA0B;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,0BAA0B;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,OAAO,CAAC;IACf,kEAAkE;IAClE,UAAU,EAAE,OAAO,CAAC,MAAM,CAAC,gBAAgB,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC;IACzE,gEAAgE;IAChE,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,EAAE,GACvE,MAAM,GAAG,IAAI,CAUf;AAED,yDAAyD;AACzD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAExD;AAID,2EAA2E;AAC3E,KAAK,cAAc,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAQxD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CA+ChG"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@timber-js/app",
3
- "version": "0.2.0-alpha.229",
3
+ "version": "0.2.0-alpha.230",
4
4
  "description": "Vite-native React framework built for Servers and Serverless Platforms — correct HTTP semantics, real status codes, pages that work without JavaScript",
5
5
  "keywords": [
6
6
  "cloudflare-workers",
@@ -41,6 +41,11 @@ export interface TraceSummary {
41
41
  error: boolean;
42
42
  }
43
43
 
44
+ /** Every span, or only the I/O: awaits and fetches. */
45
+ export type WaterfallView = 'all' | 'awaits';
46
+
47
+ const IO_SPANS = new Set(['timber.await', 'timber.fetch']);
48
+
44
49
  export interface WaterfallRow {
45
50
  span: DevSpanPayload;
46
51
  depth: number;
@@ -157,17 +162,32 @@ export class DevPillStore {
157
162
  const status = root?.attributes['http.response.status_code'];
158
163
  return {
159
164
  fetches: spans.filter((s) => s.name === 'timber.fetch').length,
160
- awaits: spans.filter((s) => s.name === 'timber.await').length,
165
+ // Shared awaits waited on work another await started; not new work.
166
+ awaits: spans.filter(
167
+ (s) => s.name === 'timber.await' && s.attributes['timber.await_shared'] !== true
168
+ ).length,
161
169
  durationMs: root
162
170
  ? Math.max(...spans.map((s) => s.endMs)) - Math.min(...spans.map((s) => s.startMs))
163
171
  : null,
164
- slow: spans.some((s) => s !== root && s.endMs - s.startMs > this.slowMs),
172
+ slow: spans.some(
173
+ (s) =>
174
+ s !== root &&
175
+ s.attributes['timber.await_shared'] !== true &&
176
+ s.endMs - s.startMs > this.slowMs
177
+ ),
165
178
  error: spans.some((s) => s.error) || (typeof status === 'number' && status >= 500),
166
179
  };
167
180
  }
168
181
 
169
- /** Spans in tree order (parent before children, siblings by start). */
170
- waterfall(traceId: string): WaterfallRow[] {
182
+ /**
183
+ * Spans in tree order (parent before children, siblings by start).
184
+ *
185
+ * `view: 'awaits'` keeps only awaits and fetches — the I/O — each nested
186
+ * under its nearest kept ancestor, so hiding framework phases leaves no
187
+ * gaps in the indentation. The time axis still starts at the request's
188
+ * first span.
189
+ */
190
+ waterfall(traceId: string, view: WaterfallView = 'all'): WaterfallRow[] {
171
191
  const spans = this.spans(traceId);
172
192
  if (spans.length === 0) return [];
173
193
  const ids = new Set(spans.map((s) => s.spanId));
@@ -186,13 +206,16 @@ export class DevPillStore {
186
206
  const visit = (parent: string | undefined, depth: number) => {
187
207
  const siblings = (children.get(parent) ?? []).sort((a, b) => a.startMs - b.startMs);
188
208
  for (const span of siblings) {
189
- rows.push({
190
- span,
191
- depth,
192
- offsetMs: span.startMs - origin,
193
- durationMs: span.endMs - span.startMs,
194
- });
195
- visit(span.spanId, depth + 1);
209
+ const shown = view === 'all' || IO_SPANS.has(span.name);
210
+ if (shown) {
211
+ rows.push({
212
+ span,
213
+ depth,
214
+ offsetMs: span.startMs - origin,
215
+ durationMs: span.endMs - span.startMs,
216
+ });
217
+ }
218
+ visit(span.spanId, shown ? depth + 1 : depth);
196
219
  }
197
220
  };
198
221
  visit(undefined, 0);
@@ -5,24 +5,27 @@
5
5
  * it and it can't reach the app. See design/50-dev-request-pill.md §"The pill".
6
6
  */
7
7
 
8
- import { devSpanLabel } from '../../shared/dev-span.ts';
9
- import type { DevPillStore, DevRequest, WaterfallRow } from './store.ts';
8
+ import { devSpanLabel, ROOT_SPAN_NAME } from '../../shared/dev-span.ts';
9
+ import type { DevPillStore, DevRequest, WaterfallRow, WaterfallView } from './store.ts';
10
10
 
11
- /** localStorage key remembering a minimized pill, per dev origin. */
11
+ /** localStorage keys for per-browser choices, per dev origin. */
12
12
  const MINIMIZED_KEY = 'timber:dev-pill:minimized';
13
+ const VIEW_KEY = 'timber:dev-pill:view';
13
14
 
14
- function readMinimized(): boolean {
15
+ /** A stored choice, or null. Storage can be blocked; then nothing is remembered. */
16
+ function readPref(key: string): string | null {
15
17
  try {
16
- return localStorage.getItem(MINIMIZED_KEY) === '1';
18
+ return localStorage.getItem(key);
17
19
  } catch {
18
- return false; // Storage blocked: start expanded, as on first visit.
20
+ return null;
19
21
  }
20
22
  }
21
23
 
22
- function writeMinimized(value: boolean): void {
24
+ /** Store a choice; null forgets it. */
25
+ function writePref(key: string, value: string | null): void {
23
26
  try {
24
- if (value) localStorage.setItem(MINIMIZED_KEY, '1');
25
- else localStorage.removeItem(MINIMIZED_KEY);
27
+ if (value === null) localStorage.removeItem(key);
28
+ else localStorage.setItem(key, value);
26
29
  } catch {
27
30
  // Storage blocked: the choice lasts until reload.
28
31
  }
@@ -51,7 +54,7 @@ const STYLES = `
51
54
  @keyframes pulse { 50% { opacity: 0.35; } }
52
55
  .muted { color: #9aa394; }
53
56
  .panel {
54
- width: min(560px, calc(100vw - 24px)); max-height: min(60vh, 520px);
57
+ width: min(820px, calc(100vw - 24px)); max-height: min(70vh, 640px);
55
58
  display: flex; flex-direction: column; background: #171a16; border: 1px solid #2f352c;
56
59
  border-radius: 10px; overflow: hidden; box-shadow: 0 12px 40px rgb(0 0 0 / 0.35);
57
60
  }
@@ -65,12 +68,18 @@ select {
65
68
  }
66
69
  .rows { overflow: auto; padding: 4px 0; }
67
70
  .row {
68
- display: grid; grid-template-columns: minmax(0, 1fr) minmax(80px, 38%) 56px;
71
+ display: grid; grid-template-columns: minmax(0, 1fr) minmax(80px, 32%) 56px;
69
72
  align-items: center; gap: 8px; padding: 3px 10px;
70
73
  }
71
74
  .row:hover { background: #20241e; }
72
- .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
73
- .label .sub { color: #9aa394; margin-left: 6px; }
75
+ .label { min-width: 0; overflow-wrap: anywhere; line-height: 1.35; }
76
+ /* Location and status on their own line, so a long label never hides them. */
77
+ .label .sub { display: block; color: #9aa394; font-size: 11px; }
78
+ .row.shared .label { color: #9aa394; }
79
+ .bar.shared {
80
+ background: repeating-linear-gradient(135deg, #3f9a8f55 0 3px, transparent 3px 6px);
81
+ outline: 1px solid #3f9a8f88; outline-offset: -1px;
82
+ }
74
83
  .track { position: relative; height: 8px; }
75
84
  .bar {
76
85
  position: absolute; top: 0; height: 8px; min-width: 2px; border-radius: 2px;
@@ -82,6 +91,13 @@ select {
82
91
  .bar.error { background: #e5534b; }
83
92
  .dur { text-align: right; font-variant-numeric: tabular-nums; color: #9aa394; }
84
93
  .empty { padding: 16px 10px; color: #9aa394; }
94
+ .toggle { display: flex; flex: none; border: 1px solid #2f352c; border-radius: 6px; overflow: hidden; }
95
+ .toggle button {
96
+ background: transparent; color: #9aa394; border: 0; padding: 4px 10px; font: inherit; cursor: pointer;
97
+ }
98
+ .toggle button + button { border-left: 1px solid #2f352c; }
99
+ .toggle button[aria-pressed="true"] { background: #2b3127; color: #e8ebe6; }
100
+ .toggle button:focus-visible { outline: 2px solid #8fbf5a; outline-offset: -2px; }
85
101
  .bar-row { display: flex; align-items: center; gap: 4px; }
86
102
  .icon {
87
103
  display: inline-grid; place-items: center; width: 24px; height: 24px; padding: 0;
@@ -146,13 +162,16 @@ export function mountDevPill(store: DevPillStore): void {
146
162
  shadow.append(style, root);
147
163
 
148
164
  let open = false;
149
- let minimized = readMinimized();
165
+ let minimized = readPref(MINIMIZED_KEY) === '1';
166
+ // Awaits by default: the I/O is what the pill is for; framework phases are
167
+ // a click away.
168
+ let view: WaterfallView = readPref(VIEW_KEY) === 'all' ? 'all' : 'awaits';
150
169
  let scheduled = false;
151
170
 
152
171
  const setMinimized = (value: boolean) => {
153
172
  minimized = value;
154
173
  open = false;
155
- writeMinimized(value);
174
+ writePref(MINIMIZED_KEY, value ? '1' : null);
156
175
  render();
157
176
  };
158
177
 
@@ -223,10 +242,33 @@ export function mountDevPill(store: DevPillStore): void {
223
242
  picker.addEventListener('change', () => store.select(picker.value));
224
243
  head.append(picker);
225
244
 
245
+ const toggle = el('div', 'toggle');
246
+ toggle.setAttribute('role', 'group');
247
+ toggle.setAttribute('aria-label', 'Spans shown');
248
+ for (const [value, text] of [
249
+ ['awaits', 'Awaits'],
250
+ ['all', 'All'],
251
+ ] as const) {
252
+ const button = el('button', undefined, text);
253
+ button.type = 'button';
254
+ button.setAttribute('aria-pressed', String(view === value));
255
+ button.addEventListener('click', () => {
256
+ view = value;
257
+ writePref(VIEW_KEY, value === 'all' ? 'all' : null);
258
+ render();
259
+ });
260
+ toggle.append(button);
261
+ }
262
+ head.append(toggle);
263
+
226
264
  const rows = el('div', 'rows');
227
- const waterfall = store.waterfall(selected.traceId);
265
+ const waterfall = store.waterfall(selected.traceId, view);
228
266
  if (waterfall.length === 0) {
229
- rows.append(el('div', 'empty', 'Waiting for spans…'));
267
+ const nothing =
268
+ view === 'awaits' && store.spans(selected.traceId).length > 0
269
+ ? 'No awaits or fetches over 1ms. All shows every span.'
270
+ : 'Waiting for spans…';
271
+ rows.append(el('div', 'empty', nothing));
230
272
  }
231
273
  const total = Math.max(1, ...waterfall.map((r) => r.offsetMs + r.durationMs));
232
274
  for (const row of waterfall) {
@@ -234,17 +276,23 @@ export function mountDevPill(store: DevPillStore): void {
234
276
  const { label } = devSpanLabel(row.span.name, row.span.attributes);
235
277
  const labelCell = el('div', 'label');
236
278
  labelCell.style.paddingLeft = `${row.depth * 12}px`;
237
- labelCell.title = label;
238
279
  labelCell.append(document.createTextNode(label));
280
+ // Shared: this await waited on a promise another await already took
281
+ // (React.cache, an in-flight load) — no new work happened here.
282
+ const shared = row.span.attributes['timber.await_shared'] === true;
283
+ if (shared) line.classList.add('shared');
239
284
  const sub = rowSubtitle(row);
240
285
  if (sub) labelCell.append(el('span', 'sub', sub));
241
286
 
242
287
  const track = el('div', 'track');
243
288
  const bar = el('div', 'bar');
244
289
  if (row.span.name === 'timber.fetch') bar.classList.add('fetch');
245
- else if (row.span.name === 'timber.await') bar.classList.add('await');
290
+ else if (row.span.name === 'timber.await') bar.classList.add(shared ? 'shared' : 'await');
246
291
  if (row.span.error) bar.classList.add('error');
247
- else if (row.depth > 0 && row.durationMs > store.slowMs) bar.classList.add('slow');
292
+ // The request's own row spans everything; judge it by name, not
293
+ // depth, which the awaits view flattens (Codex review on #1274).
294
+ else if (!shared && row.span.name !== ROOT_SPAN_NAME && row.durationMs > store.slowMs)
295
+ bar.classList.add('slow');
248
296
  bar.style.left = `${(row.offsetMs / total) * 100}%`;
249
297
  bar.style.width = `${(row.durationMs / total) * 100}%`;
250
298
  track.append(bar);
@@ -12,11 +12,36 @@ import { trace } from '@opentelemetry/api';
12
12
  import { AWAIT_SPAN_NAME, DEV_AWAIT_KEY } from '../shared/dev-span.ts';
13
13
  import { traceCall } from '../server/tracing.ts';
14
14
 
15
- export type DevAwaitWrapper = (
16
- thunk: () => Promise<unknown>,
17
- label: string,
18
- location: string
19
- ) => Promise<unknown>;
15
+ export type DevAwaitWrapper = (thunk: () => unknown, label: string, location: string) => unknown;
16
+
17
+ /**
18
+ * Promises some await has already waited on. A later await of the same
19
+ * promise is waiting on work that started elsewhere: React's `cache()`
20
+ * hands every caller in a request the same promise, so four
21
+ * `await getArtistDirectory()` calls are one load and three waits, and the
22
+ * pill must not draw them as four loads. Weak, so a promise is forgotten
23
+ * with its request.
24
+ */
25
+ const awaited = new WeakSet<object>();
26
+
27
+ /**
28
+ * Mark the active await span shared when its operand is a promise another
29
+ * await already took.
30
+ *
31
+ * Plain native promises only: one of those settles once, whoever awaits it.
32
+ * A thenable may start its work on every `then()` — Drizzle's query builders
33
+ * run the query each time they are awaited — so a second await of one is new
34
+ * work, not a wait (Codex review on #1274).
35
+ */
36
+ function noteShared(value: unknown): void {
37
+ if (typeof value !== 'object' || value === null) return;
38
+ if (Object.getPrototypeOf(value) !== Promise.prototype) return;
39
+ if (awaited.has(value)) {
40
+ trace.getActiveSpan()?.setAttribute('timber.await_shared', true);
41
+ } else {
42
+ awaited.add(value);
43
+ }
44
+ }
20
45
 
21
46
  /**
22
47
  * Install the wrapper on `globalThis[DEV_AWAIT_KEY]`. Transformed modules
@@ -28,12 +53,16 @@ export function installDevAwait(): void {
28
53
  // Outside a request (module init, background work) there is no trace
29
54
  // to nest under; a span would only become an orphan trace.
30
55
  if (!trace.getActiveSpan()) return thunk();
31
- return Promise.resolve(
32
- traceCall(
33
- AWAIT_SPAN_NAME,
34
- { 'timber.await_label': label, 'timber.await_location': location },
35
- thunk
36
- )
56
+ // The span opens before the operand runs, so work it starts nests under
57
+ // it; the operand's value then says whether this await is new work.
58
+ return traceCall(
59
+ AWAIT_SPAN_NAME,
60
+ { 'timber.await_label': label, 'timber.await_location': location },
61
+ () => {
62
+ const value = thunk();
63
+ noteShared(value);
64
+ return value;
65
+ }
37
66
  );
38
67
  };
39
68
  (globalThis as Record<symbol, unknown>)[DEV_AWAIT_KEY] = wrapper;