Skip to content

Commit 5b869c1

Browse files
constantantclaude
andcommitted
fix(openapi-resource-mocks): catch mode no longer disrupts existing mock value on enable
Previously, enabling catch mode immediately called setLoading() on the current ref, blowing away any resolved value before a new request had even fired. Now setCatchMode only registers the key — the existing onRequest listener already handles interception when the next actual request fires. Also removed the stale re-intercept logic in the reset control handler, and unified applyBehavior into a single onRequest listener so reload correctly re-applies initialBehavior when catch mode is off. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4987022 commit 5b869c1

2 files changed

Lines changed: 13 additions & 26 deletions

File tree

tools/openapi-resource-mocks/src/lib/mock-resource-bus.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,7 @@ export class MockResourceBus {
6060

6161
setCatchMode(key: string, enabled: boolean): void {
6262
if (enabled) {
63-
if (this.catchModeKeys.has(key)) return; // idempotent — avoids duplicate caught events
64-
this.catchModeKeys.add(key);
65-
const ref = this.refs.get(key);
66-
if (ref) {
67-
ref.setLoading();
68-
this.dispatchDomEvent(key, { type: 'caught', args: [], requestId: this.nextReqId(), ts: Date.now() });
69-
}
63+
this.catchModeKeys.add(key); // idempotent — next request will be caught, current value untouched
7064
} else {
7165
this.catchModeKeys.delete(key);
7266
}
@@ -196,17 +190,7 @@ export class MockResourceBus {
196190
case 'setLoading': ref.setLoading(); break;
197191
case 'fail': ref.fail(detail.value); break;
198192
case 'reload': window.__openApiMocks__?.[detail.key]?.reload(); break;
199-
case 'reset': {
200-
ref.reset();
201-
// If catch mode is still on, immediately re-intercept the resource.
202-
if (this.catchModeKeys.has(detail.key)) {
203-
ref.setLoading();
204-
this.dispatchDomEvent(detail.key, {
205-
type: 'caught', args: [], requestId: this.nextReqId(), ts: Date.now(),
206-
});
207-
}
208-
break;
209-
}
193+
case 'reset': ref.reset(); break;
210194
case 'setProgress': ref.setProgress(detail.progressType!, detail.loaded!, detail.total); break;
211195
case 'simulateProgress': window.__openApiMocks__?.[detail.key]?.simulateProgress(
212196
detail.progressType!,

tools/openapi-resource-mocks/src/lib/provide-mock-resource.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,18 @@ export function provideMockResource<T>(
5757
bus.register(effectiveKey, ref, meta);
5858
const internal = ref as MockResourceRefInternal<T>;
5959

60+
if (initialBehavior) {
61+
// Re-apply on every request (initial, param change, reload) unless catch mode
62+
// is intercepting. The bus's onRequest listener (registered in bus.register)
63+
// runs first, so isCatchMode reflects whether this request was just caught.
64+
ref.onRequest(() => {
65+
if (!bus.isCatchMode(effectiveKey)) applyBehavior(ref, initialBehavior);
66+
});
67+
}
68+
6069
// untracked() prevents thunk signal-reads from leaking into the outer reactive
6170
// context — component field initializers run during Angular change detection.
62-
untracked(() => {
63-
internal._notifyRequest(args);
64-
// Skip initialBehavior when the bus caught the request: catch mode takes
65-
// precedence and the response must come from the DevTools panel.
66-
if (initialBehavior && !bus.isCatchMode(effectiveKey)) applyBehavior(ref, initialBehavior);
67-
});
71+
untracked(() => { internal._notifyRequest(args); });
6872

6973
// When any arg is a reactive thunk, track signal changes so each new set of
7074
// params fires a new request event (mirroring how httpResource re-fires on
@@ -76,9 +80,8 @@ export function provideMockResource<T>(
7680
// Resolve thunks inside the effect body so their signals are tracked.
7781
const resolved = args.map((a) => (typeof a === 'function' ? (a as () => unknown)() : a));
7882
untracked(() => {
79-
if (first) { first = false; return; } // first run already handled above
83+
if (first) { first = false; return; } // first run already handled by _notifyRequest above
8084
internal._notifyRequest(resolved);
81-
if (initialBehavior && !bus.isCatchMode(effectiveKey)) applyBehavior(ref, initialBehavior);
8285
});
8386
});
8487
}

0 commit comments

Comments
 (0)