Skip to content

Commit a2e6f31

Browse files
JeanMechethePunderWoman
authored andcommitted
fix(core): allow to set a resource in an error state (#62253)
A resource is error state should still remain writable. fixes #62241 PR Close #62253
1 parent 31a2125 commit a2e6f31

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/core/src/resource/resource.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,17 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
269269
return;
270270
}
271271

272-
const current = untracked(this.value);
272+
const error = untracked(this.error);
273273
const state = untracked(this.state);
274274

275-
if (state.status === 'local' && (this.equal ? this.equal(current, value) : current === value)) {
276-
return;
275+
if (!error) {
276+
const current = untracked(this.value);
277+
if (
278+
state.status === 'local' &&
279+
(this.equal ? this.equal(current, value) : current === value)
280+
) {
281+
return;
282+
}
277283
}
278284

279285
// Enter Local state with the user-defined value.

packages/core/test/resource/resource_spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,34 @@ describe('resource', () => {
729729
expect(res.error()).toEqual(new Error('fail'));
730730
});
731731

732+
it('should allow to set a value on when in error state', async () => {
733+
const appRef = TestBed.inject(ApplicationRef);
734+
let res = resource({
735+
stream: async () => signal({error: new Error('fail')}),
736+
injector: TestBed.inject(Injector),
737+
});
738+
739+
await appRef.whenStable();
740+
expect(res.status()).toBe('error');
741+
742+
res.set('new value');
743+
expect(res.status()).toBe('local');
744+
expect(res.value()).toBe('new value');
745+
746+
// also via setting the value on the signal directly
747+
res = resource({
748+
stream: async () => signal({error: new Error('fail')}),
749+
injector: TestBed.inject(Injector),
750+
});
751+
752+
await appRef.whenStable();
753+
expect(res.status()).toBe('error');
754+
755+
res.value.set('new value');
756+
expect(res.status()).toBe('local');
757+
expect(res.value()).toBe('new value');
758+
});
759+
732760
it('should transition across streamed states', async () => {
733761
const appRef = TestBed.inject(ApplicationRef);
734762
const stream = signal<{value: number} | {error: Error}>({value: 1});

0 commit comments

Comments
 (0)