Skip to content

Commit

Permalink
fix(connect): handle null when performing typeof object (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
vneogi199 authored Jan 12, 2024
1 parent ed9e757 commit b542fdf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
43 changes: 43 additions & 0 deletions libs/ngxtension/connect/src/connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,47 @@ describe(connect.name, () => {
expect(component.count()).toBe(1); // should not change
});
});

describe('connects an observable with single emit to a null signal in injection context', () => {
@Component({ standalone: true, template: '' })
class TestComponent {
text = signal<string | null>(null);

constructor() {
connect(this.text, of('text'));
}
}

let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;

beforeEach(async () => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});
it('works fine', () => {
expect(component.text()).toBe('text');
});
});
describe('connects an observable with multiple emits to a null signal in injection context', () => {
@Component({ standalone: true, template: '' })
class TestComponent {
text = signal<string | null>(null);

constructor() {
connect(this.text, of('text', null, 'text2'));
}
}

let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;

beforeEach(async () => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});
it('works fine', () => {
expect(component.text()).toBe('text2');
});
});
});
7 changes: 6 additions & 1 deletion libs/ngxtension/connect/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ export function connect(signal: WritableSignal<unknown>, ...args: any[]) {
return observable.pipe(takeUntilDestroyed(destroyRef)).subscribe((x) => {
const update = () => {
signal.update((prev) => {
if (typeof prev === 'object' && !Array.isArray(prev)) {
if (
prev !== undefined &&
prev !== null &&
typeof prev === 'object' &&
!Array.isArray(prev)
) {
return { ...prev, ...((reducer?.(prev, x) || x) as object) };
}

Expand Down

1 comment on commit b542fdf

@endlacer
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this broke something. Idk if intentional or not, but when i do sth like this:

connect(this._state, this.update$);
this.update$.next(of(null));

before this, the state did not get updated at all. nothing changed. now the whole state gets nulled. Is this intentional?
(im not sure if this is the commit that caused the changed behavior, but it did change since v1.5)

Please # to comment.