@alloy-js/core 0.23.0-dev.18 → 0.23.0-dev.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alloy-js/core",
3
- "version": "0.23.0-dev.18",
3
+ "version": "0.23.0-dev.19",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -60,3 +60,7 @@ export async function enableDevtoolsAndConnect(
60
60
  export async function resetDevtoolsServerForTests(): Promise<void> {
61
61
  // No-op in browser
62
62
  }
63
+
64
+ export function refreshDebugState(): void {
65
+ // No-op in browser
66
+ }
@@ -153,6 +153,19 @@ function isNodeEnvironment() {
153
153
  );
154
154
  }
155
155
 
156
+ // Cached once at module load. Stable for production runs. Tests that modify
157
+ // process.env.ALLOY_DEBUG dynamically must call refreshDebugState() afterward.
158
+ let _envDebugEnabled: boolean =
159
+ isNodeEnvironment() && Boolean(process.env.ALLOY_DEBUG);
160
+
161
+ /**
162
+ * Invalidates the cached env-var result for isDevtoolsEnabled(). Call this in
163
+ * test beforeEach hooks after modifying process.env.ALLOY_DEBUG.
164
+ */
165
+ export function refreshDebugState(): void {
166
+ _envDebugEnabled = isNodeEnvironment() && Boolean(process.env.ALLOY_DEBUG);
167
+ }
168
+
156
169
  function getCwd() {
157
170
  if (!isNodeEnvironment()) return undefined;
158
171
  try {
@@ -177,7 +190,7 @@ function resolveDebugPort() {
177
190
  /** Returns true when devtools are enabled (via env var or explicit call). */
178
191
  export function isDevtoolsEnabled() {
179
192
  if (!isNodeEnvironment()) return false;
180
- return devtoolsExplicitlyEnabled || Boolean(process.env.ALLOY_DEBUG);
193
+ return devtoolsExplicitlyEnabled || _envDebugEnabled;
181
194
  }
182
195
 
183
196
  /** Returns true when a devtools client is currently connected. */
@@ -463,6 +476,8 @@ export async function resetDevtoolsServerForTests() {
463
476
  loggedDevtoolsLinks = false;
464
477
  subscribedPromise = null;
465
478
  resolveSubscribed = null;
479
+ // Re-read the env var in case tests modified process.env.ALLOY_DEBUG
480
+ refreshDebugState();
466
481
  // Close the trace DB so each test starts fresh
467
482
  closeTrace();
468
483
  }
package/src/reactivity.ts CHANGED
@@ -255,13 +255,16 @@ export function effect<T>(
255
255
  };
256
256
 
257
257
  const debugInfo = options?.debug;
258
- const effectId = debug.effect.register({
259
- name: debugInfo?.name ?? fn.name,
260
- type: debugInfo?.type,
261
- createdAt: captureSourceLocation(),
262
- contextId: context.id,
263
- ownerContextId: resolveOwnerEffectContextId(context),
264
- });
258
+ const effectId =
259
+ isDebugEnabled() ?
260
+ debug.effect.register({
261
+ name: debugInfo?.name ?? fn.name,
262
+ type: debugInfo?.type,
263
+ createdAt: captureSourceLocation(),
264
+ contextId: context.id,
265
+ ownerContextId: resolveOwnerEffectContextId(context),
266
+ })
267
+ : -1;
265
268
 
266
269
  if (effectId !== -1) {
267
270
  context.meta ??= {};
@@ -458,13 +461,15 @@ export function ref<T>(
458
461
  options?: { isInfrastructure?: boolean },
459
462
  ): Ref<T> {
460
463
  const result = vueRef(value) as Ref<T>;
461
- debug.effect.registerRef({
462
- id: refId(result),
463
- kind: "ref",
464
- createdAt: captureSourceLocation(),
465
- createdByEffectId: globalContext?.meta?.effectId,
466
- isInfrastructure: options?.isInfrastructure,
467
- });
464
+ if (isDebugEnabled()) {
465
+ debug.effect.registerRef({
466
+ id: refId(result),
467
+ kind: "ref",
468
+ createdAt: captureSourceLocation(),
469
+ createdByEffectId: globalContext?.meta?.effectId,
470
+ isInfrastructure: options?.isInfrastructure,
471
+ });
472
+ }
468
473
  return result;
469
474
  }
470
475
 
@@ -492,24 +497,28 @@ export function shallowReactive<T extends object>(
492
497
 
493
498
  export function shallowRef<T>(value?: T, options?: { label?: string }): Ref<T> {
494
499
  const result = vueShallowRef(value) as Ref<T>;
495
- debug.effect.registerRef({
496
- id: refId(result),
497
- kind: "shallowRef",
498
- label: options?.label,
499
- createdAt: captureSourceLocation(),
500
- createdByEffectId: globalContext?.meta?.effectId,
501
- });
500
+ if (isDebugEnabled()) {
501
+ debug.effect.registerRef({
502
+ id: refId(result),
503
+ kind: "shallowRef",
504
+ label: options?.label,
505
+ createdAt: captureSourceLocation(),
506
+ createdByEffectId: globalContext?.meta?.effectId,
507
+ });
508
+ }
502
509
  return result;
503
510
  }
504
511
 
505
512
  export function computed<T>(getter: () => T): Ref<T> {
506
513
  const result = vueComputed(getter) as Ref<T>;
507
- debug.effect.registerRef({
508
- id: refId(result),
509
- kind: "computed",
510
- createdAt: captureSourceLocation(),
511
- createdByEffectId: globalContext?.meta?.effectId,
512
- });
514
+ if (isDebugEnabled()) {
515
+ debug.effect.registerRef({
516
+ id: refId(result),
517
+ kind: "computed",
518
+ createdAt: captureSourceLocation(),
519
+ createdByEffectId: globalContext?.meta?.effectId,
520
+ });
521
+ }
513
522
  return result;
514
523
  }
515
524
 
@@ -522,12 +531,14 @@ export function toRef<T extends object, K extends keyof T>(
522
531
  defaultValue === undefined ?
523
532
  (vueToRef(object, key) as Ref<T[K]>)
524
533
  : (vueToRef(object, key, defaultValue) as Ref<T[K]>);
525
- debug.effect.registerRef({
526
- id: refId(result),
527
- kind: "toRef",
528
- createdAt: captureSourceLocation(),
529
- createdByEffectId: globalContext?.meta?.effectId,
530
- });
534
+ if (isDebugEnabled()) {
535
+ debug.effect.registerRef({
536
+ id: refId(result),
537
+ kind: "toRef",
538
+ createdAt: captureSourceLocation(),
539
+ createdByEffectId: globalContext?.meta?.effectId,
540
+ });
541
+ }
531
542
  return result;
532
543
  }
533
544
 
@@ -535,13 +546,15 @@ export function toRefs<T extends object>(
535
546
  object: T,
536
547
  ): { [K in keyof T]: Ref<T[K]> } {
537
548
  const result = vueToRefs(object) as { [K in keyof T]: Ref<T[K]> };
538
- for (const refValue of Object.values(result) as Ref<unknown>[]) {
539
- debug.effect.registerRef({
540
- id: refId(refValue),
541
- kind: "toRef",
542
- createdAt: captureSourceLocation(),
543
- createdByEffectId: globalContext?.meta?.effectId,
544
- });
549
+ if (isDebugEnabled()) {
550
+ for (const refValue of Object.values(result) as Ref<unknown>[]) {
551
+ debug.effect.registerRef({
552
+ id: refId(refValue),
553
+ kind: "toRef",
554
+ createdAt: captureSourceLocation(),
555
+ createdByEffectId: globalContext?.meta?.effectId,
556
+ });
557
+ }
545
558
  }
546
559
  return result;
547
560
  }
@@ -1,4 +1,5 @@
1
1
  import { afterEach, beforeEach, describe, expect, it } from "vitest";
2
+ import { refreshDebugState } from "../../src/devtools/devtools-server.js";
2
3
  import {
3
4
  getReactiveCreationLocation,
4
5
  shallowReactive,
@@ -10,6 +11,7 @@ describe("shallowReactive creation location", () => {
10
11
  beforeEach(() => {
11
12
  origDebug = process.env.ALLOY_DEBUG;
12
13
  process.env.ALLOY_DEBUG = "1";
14
+ refreshDebugState();
13
15
  });
14
16
 
15
17
  afterEach(() => {
@@ -18,6 +20,7 @@ describe("shallowReactive creation location", () => {
18
20
  } else {
19
21
  process.env.ALLOY_DEBUG = origDebug;
20
22
  }
23
+ refreshDebugState();
21
24
  });
22
25
 
23
26
  it("stores creation location keyed by raw target when debug enabled", () => {
@@ -31,6 +34,7 @@ describe("shallowReactive creation location", () => {
31
34
 
32
35
  it("does not store location when debug is disabled", () => {
33
36
  delete process.env.ALLOY_DEBUG;
37
+ refreshDebugState();
34
38
  const raw = { y: 2 };
35
39
  shallowReactive(raw);
36
40